序
StringData JPA 是微服务框架下一款ORM框架,在微服务体系架构下,数据持久化框架,主要为SpringData JPA及Mybatis两种,这两者的具体比较,本文不做阐述,本文只简单阐述SpringData JPA的使用方法。
简介
SpringData JPA的Repository接口介绍,本文主要介绍CrudRepository、PagingAndSortingRepository、JpaSpecificationExecutor。
示例
pom.xml
org.springframework.boot spring-boot-starter-web
org.springframework.boot spring-boot-starter-data-jpa mysql mysql-connector-java runtime
org.springframework.boot spring-boot-starter-test test
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{ /*
*Query高级查询
*/ @Query("select e from Emp e") public ListqueryAllTest(); }
PagingAndSortingRepository实现:
public interface EmpPagingAndSortingRepository extends PagingAndSortingRepository{ }
EmpJpaSpecificationExecutor实现:
public interface EmpJpaSpecificationExecutor extends JpaSpecificationExecutor,Repository { }
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() { Listemps = 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 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"); Iterableemps = 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 result = empPagingAndSortingRepository.findAll(pageable); //查询结果是pageable对象 // Iterable 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() { Specificationspec = new Specification () { @Override
//多种复杂条件组合查询,属于JPA高级 public Predicate toPredicate(Rootroot, CriteriaQuery> query, CriteriaBuilder cb) { List 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 emps = empJpaSpecificationExecutor.findAll(spec, pageable); for (Emp emp: emps.getContent()) { System.out.println(emp); } } }