Springboot集成SpringData JPA

StringData JPA 是微服务框架下一款ORM框架,在微服务体系架构下,数据持久化框架,主要为SpringData JPA及Mybatis两种,这两者的具体比较,本文不做阐述,本文只简单阐述SpringData JPA的使用方法。

简介

SpringData JPA的Repository接口介绍,本文主要介绍CrudRepository、PagingAndSortingRepository、JpaSpecificationExecutor。

示例

pom.xml

<!--Springboot-web-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--SpringData-JPA-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!--mysql启动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!--测试驱动-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

 entity:

/*
* 数据实体,表名test_emp
*/
@Entity
@Table(name="test_emp")
public class Emp implements Serializable{
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Integer id;
@Column
private Integer age;
@Column
private String name;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Emp [id=" + id + ", age=" + age + ", name=" + name + "]";
}
}

CrudRepository实现:

public interface EmpCrudRepository extends CrudRepository<Emp, Integer> {
/*
*Query高级查询
*/
@Query("select e from Emp e")
public List<Emp> queryAllTest();
}

PagingAndSortingRepository实现:

public interface EmpPagingAndSortingRepository extends PagingAndSortingRepository<Emp, Integer> {

}

EmpJpaSpecificationExecutor实现:

public interface EmpJpaSpecificationExecutor extends JpaSpecificationExecutor<Emp>,Repository<Emp, Integer>{

}

 CurdRepository测试:

@SpringBootTest
@RunWith(SpringRunner.class)
public class EmpCurdRepositoryTest { @Autowired
private EmpCrudRepository empCrudRepository; @Test
public void testAdd() {
Emp emp = new Emp();
emp.setAge(22);
emp.setId(1);
emp.setName("test1");
empCrudRepository.save(emp);
} @Test
public void testAddAll() {
List<Emp> emps = new ArrayList<>();
for (int i = 0; i < 10; i++) {
Emp emp = new Emp();
emp.setAge(i+20);
emp.setName("test"+i);
emps.add(emp);
}
empCrudRepository.saveAll(emps);
} /**
* Query注解使用
*/
@Test
public void testQueryAll() {
List<Emp> emps = empCrudRepository.queryAllTest();
for(Emp emp:emps) {
System.out.println(emp);
}
}
}

  PagingAndSortingRepository测试:

@SpringBootTest
@RunWith(SpringRunner.class)
public class EmpPagingAndSortingRepositoryTest { @Autowired
private EmpPagingAndSortingRepository empPagingAndSortingRepository;
@Test
public void query() {
Pageable pageable = PageRequest.of(0, 10, Direction.DESC, "id");
Iterable<Emp> emps = empPagingAndSortingRepository.findAll(pageable);
for(Emp emp:emps) {
System.out.println(emp);
}
} @Test
public void queryAll() {
Pageable pageable = PageRequest.of(0, 7, Direction.DESC, "id");
Page<Emp> result = empPagingAndSortingRepository.findAll(pageable); //查询结果是pageable对象
// Iterable<Emp> emps = empPagingAndSortingRepository.findAll(pageable); //查询结果直接是emp对象
System.out.println(result.getContent());
System.out.println(result.getNumber());
System.out.println(result.getNumberOfElements());
System.out.println(result.getTotalElements());
System.out.println(result.getTotalPages());
for(Emp emp:result.getContent()) {
System.out.println(emp);
}
}
}

  JpaSpecificationExecutor测试:

@RunWith(SpringRunner.class)
@SpringBootTest
public class EmpJpaSpecificationExecutorTest { @Autowired
private EmpJpaSpecificationExecutor empJpaSpecificationExecutor; @Test
public void test() {
Specification<Emp> spec = new Specification<Emp>() {
@Override
//多种复杂条件组合查询,属于JPA高级
public Predicate toPredicate(Root<Emp> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
List<Predicate> predicates = new ArrayList<>();
Predicate p1 = cb.gt(root.get("age"), 25);
Predicate p2 = cb.lt(root.get("age"), 28);
Predicate p3 = cb.and(p1,p2);
predicates.add(p3);
return query.where(predicates.toArray(new Predicate[predicates.size()])).getRestriction();
}
}; Pageable pageable = PageRequest.of(0, 5, Direction.ASC, "id"); Page<Emp> emps = empJpaSpecificationExecutor.findAll(spec, pageable);
for (Emp emp: emps.getContent()) {
System.out.println(emp);
} }
}

  

上一篇:day15(mysql 的多表查询,事务)


下一篇:Vc数据库编程基础MySql数据库的表查询功能