面试中经常会有几轮,机试必不可少,下面就给大家分享一下,基于spring boot+spring data jpa 实现单表的增删改查
开发环境及开发工具:IDEA+SQLyogEnt+jdk1.8+mysql5.5
1.在数据库中创建库:jpa
2.创建表:cst_customer
DDL信息:
create table:
CREATE TABLE `cst_customer` (
`cust_id` bigint(20) NOT NULL AUTO_INCREMENT,
`cust_address` varchar(255) DEFAULT NULL,
`cust_industry` varchar(255) DEFAULT NULL,
`cust_level` varchar(255) DEFAULT NULL,
`cust_name` varchar(255) DEFAULT NULL,
`cust_phone` varchar(255) DEFAULT NULL,
`cust_source` varchar(255) DEFAULT NULL,
PRIMARY KEY (`cust_id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8
测试数据可以自己手动补上,建表成功后,F5刷新一下数据库,即可看到自己创建的表
4.0.0
org.lijialin
jpa-day03-spec
1.0-SNAPSHOT
5.0.2.RELEASE
5.0.1.Final
junit
junit
4.12
test
org.aspectj
aspectjweaver
1.6.8
org.springframework
spring-aop
${spring.version}
org.springframework
spring-context
${spring.version}
org.springframework
spring-context-support
${spring.version}
org.springframework
spring-orm
${spring.version}
org.springframework
spring-beans
${spring.version}
org.springframework
spring-core
${spring.version}
org.hibernate
hibernate-core
${hibernate.version}
org.hibernate
hibernate-entitymanager
${hibernate.version}
org.hibernate
hibernate-validator
5.2.1.Final
c3p0
c3p0
0.9.1.2
log4j
log4j
1.2.17
org.slf4j
slf4j-api
1.6.6
org.slf4j
slf4j-log4j12
1.6.6
mysql
mysql-connector-java
5.1.6
org.springframework.data
spring-data-jpa
1.9.0.RELEASE
org.springframework
spring-test
${spring.version}
test
javax.el
javax.el-api
2.2.4
org.glassfish.web
javax.el
2.2.4
package org.lijialin.dao;
import org.lijialin.domain.Customer;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
/**
* @Author *佳林
* @Date 2019/8/17
* 符合SpringDataJpa的dao层接口规范
* JpaRepository<操作的实体类类型,实体类中主键属性的类型>
* * 封装了基本CRUD操作
* JpaSpecificationExecutor<操作的实体类类型>
* * 封装了复杂查询(分页)
* 程序执行时,动态生成实现类对象
*/
public interface CustomerDao extends JpaRepository,JpaSpecificationExecutor {
}
package org.lijialin.domain;
import javax.persistence.*;
/**
* @Author *佳林
* @Date 2019/8/16
* 1.实体类和表的映射关系
* @Eitity
* @Table
* 2.类中属性和表中字段的映射概念性
* @Id
* @GeneratedValue
* @Column
*/
@Entity
@Table(name = "cst_customer")
public class Customer {
/**
* @Id:声明主键的配置
* @GeneratedValue:配置主键的生成策略
* strategy
* GenerationType.IDENTITY :自增,mysql
* * 底层数据库必须支持自动增长(底层数据库支持的自动增长方式,对id自增)比如MySQL数据库
* GenerationType.SEQUENCE : 序列,oracle
* * 底层数据库(如Oracle数据库)必须支持序列方可使用SEQUENCE
* GenerationType.TABLE : jpa提供的一种机制,通过一张数据库表的形式帮助我们完成主键自增
* GenerationType.AUTO : 由程序自动的帮助我们选择主键生成策略
* @Column:配置属性和字段的映射关系
* name:数据库表中字段的名称
*/
//客户主键
@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_level")
private String custLevel;
//客户的联系方式
@Column(name="cust_industry")
private String custIndustry;
//客户的联系方式
@Column(name="cust_phone")
private String custPhone;
//客户地址
@Column(name="cust_address")
private String custAddress;
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 getCustLevel() {
return custLevel;
}
public void setCustLevel(String custLevel) {
this.custLevel = custLevel;
}
public String getCustIndustry() {
return custIndustry;
}
public void setCustIndustry(String custIndustry) {
this.custIndustry = custIndustry;
}
public String getCustPhone() {
return custPhone;
}
public void setCustPhone(String custPhone) {
this.custPhone = custPhone;
}
public String getCustAddress() {
return custAddress;
}
public void setCustAddress(String custAddress) {
this.custAddress = custAddress;
}
@Override
public String toString() {
return "Customer{" +
"custId=" + custId +
", custName='" + custName + '\'' +
", custSource='" + custSource + '\'' +
", custLevel='" + custLevel + '\'' +
", custIndustry='" + custIndustry + '\'' +
", custPhone='" + custPhone + '\'' +
", custAddress='" + custAddress + '\'' +
'}';
}
}
package org.lijialin.test;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.lijialin.dao.CustomerDao;
import org.lijialin.domain.Customer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import javax.persistence.criteria.*;
import java.util.List;
/**动态查询
* 测试类
* @Author 李佳林
* @Date 2019/8/17
*/
//声明spring提供的单元测试环境
@RunWith(SpringJUnit4ClassRunner.class)
//指定spring容器的配置信息
@ContextConfiguration(locations = "classpath:applicationContext.xml")
public class SpecTest {
@Autowired
private CustomerDao customerDao;
/**
* 根据条件查询单个对象
*/
@Test
public void testSpec1(){
//匿名内部类
/**
* 自定义查询条件
* 1.实现Specification接口(提供泛型,查询对象类型)
* 2.实现toPredicate方法(构造查询条件)
* 3.需要借助方法参数中的两个参数
* root:获取需要查询的对象的属性
* CriteriaBuilder:构造查询条件,内部封住了很多的查询条件(模糊匹配,精准匹配)
* 案例:
* 根据客户名称查询
* 查询条件:
* 1.查询方式
* 2.比较的属性名称
* 存于root对象中
*/
Specification spec = new Specification() {
@Override
public Predicate toPredicate(Root root, CriteriaQuery> query, CriteriaBuilder cb) {
Path
切记:测试类的包结构和接口类的包结构一致.
因为spring data jpa框架可以为我们动态的创建接口的实现类,可以不用在server层做接口的实现.
OK,基于spring boot 和spring data jpa框架快速实现单表的CRUD功能做好了.
有什么不足的地方,欢迎在下方留言