controller层 package wenxin.huahua.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; import org.springframework.util.StringUtils; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import wenxin.huahua.entity.Customer; import wenxin.huahua.service.CustomerService; @Controller @RequestMapping("customer") public class CustomerController { @Autowired private CustomerService customerService; @RequestMapping("query") public ResponseEntity<Customer> queryCustomerById(@RequestParam(value = "id")Long id){ Customer customer=this.customerService.queryCustomerById(id); if(StringUtils.isEmpty(customer)){ return ResponseEntity.notFound().build(); } return ResponseEntity.ok(customer); } }
service 层
package wenxin.huahua.service; import org.springframework.stereotype.Service; import wenxin.huahua.entity.Customer; public interface CustomerService { /** * 根据id来查询相应的用户信息 */ Customer queryCustomerById(Long id); }
impl层
package wenxin.huahua.service.impl; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import wenxin.huahua.entity.Customer; import wenxin.huahua.repository.CustomerRespoitory; import wenxin.huahua.service.CustomerService; @Service public class CustomerServiceimpl implements CustomerService { @Autowired private CustomerRespoitory customerRespoitory; @Override public Customer queryCustomerById(Long id) { Customer customer = this.customerRespoitory.findById(id).orElse(null); return customer; } }
dao层
package wenxin.huahua.repository; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaSpecificationExecutor; import org.springframework.stereotype.Component; import wenxin.huahua.entity.Customer; @Component //ssh的dao层实现接口需要继承两个父接口,第一个是JpaRepository,第一个参数是对应的类名称,第二个参数是该类的主键的类型, //另一个接口是JpaSpecificationExcutor接口,他的参数是对应的类的名称 public interface CustomerRespoitory extends JpaRepository<Customer,Long>, JpaSpecificationExecutor<Customer> { }
启动类
package wenxin.huahua; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class,args); } }
对象类
package wenxin.huahua.entity; import javax.persistence.*; @Entity @Table(name = "cst_customer") public class Customer { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name="cust_id") private Long custId; @Column(name="cust_name") private String custName; @Column(name="cust_source") private String custSource; @Column(name="cust_industry") private String custIndustry; @Column(name="cust_level") private String custLevel; @Column(name="cust_address") private String custAddress; @Column(name="cust_phone") private String custPhone; public Long getCustId() { return custId; } public void setCustId(Long custId) { this.custId = custId; } public String getCustName() { return custName; } public void setCustName(String custName) { this.custName = custName; } public String getCustSource() { return custSource; } public void setCustSource(String custSource) { this.custSource = custSource; } public String getCustIndustry() { return custIndustry; } public void setCustIndustry(String custIndustry) { this.custIndustry = custIndustry; } public String getCustLevel() { return custLevel; } public void setCustLevel(String custLevel) { this.custLevel = custLevel; } public String getCustAddress() { return custAddress; } public void setCustAddress(String custAddress) { this.custAddress = custAddress; } public String getCustPhone() { return custPhone; } public void setCustPhone(String custPhone) { this.custPhone = custPhone; } @Override public String toString() { return "Customer{" + "custId=" + custId + ", custName='" + custName + '\'' + ", custSource='" + custSource + '\'' + ", custIndustry='" + custIndustry + '\'' + ", custLevel='" + custLevel + '\'' + ", custAddress='" + custAddress + '\'' + ", custPhone='" + custPhone + '\'' + '}'; } }
对应类的映射表
CREATE TABLE `cst_customer` (
`cust_id` bigint(32) NOT NULL AUTO_INCREMENT COMMENT '客户编号(主键)',
`cust_name` varchar(32) NOT NULL COMMENT '客户名称(公司名称)',
`cust_source` varchar(32) DEFAULT NULL COMMENT '客户信息来源',
`cust_industry` varchar(32) DEFAULT NULL COMMENT '客户所属行业',
`cust_level` varchar(32) DEFAULT NULL COMMENT '客户级别',
`cust_address` varchar(128) DEFAULT NULL COMMENT '客户联系地址',
`cust_phone` varchar(64) DEFAULT NULL COMMENT '客户联系电话',
PRIMARY KEY (`cust_id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
基础的配置
server: port: 10086 spring: datasource: url: jdbc:mysql:///wenxin username: root password: root