1、Jpa是Hibernate的子集(网上前人总结的)
2、对象之间的各种关系(一对一,一对多,多对多等)都是在设置对象时配置好的
3、引入JPA
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
4、JpaRepository接口
public interface JpaRepository<T, ID> extends PagingAndSortingRepository<T, ID>, QueryByExampleExecutor<T>
可以看到继承了PagingAndSortingRepository和QueryByExampleExecutor两个接口
public interface PagingAndSortingRepository<T, ID> extends CrudRepository<T, ID> {
Iterable<T> findAll(Sort var1);
Page<T> findAll(Pageable var1);
}
而且PagingAndSortingRepository是在CrudRepository基础的增删改查基础上扩展了排序和分页;
QueryByExampleExecutor则是加入了查询条件的设置。
这样看来只要继承JpaRepository接口就一切OK了。
5、继承JpaRepository接口
public interface UserRepository extends JpaRepository<User, Long>
6、主要测试一下分页查询和按条件查询
6.1加入查询条件和排序功能
public String getUserListByCondition(User u){
User user = new User();
if(u.getId() != null){
user.setId(u.getId());
}
if(!StringUtils.isEmpty(u.getUser_name())){
user.setUser_name(u.getUser_name());
}
if(!StringUtils.isEmpty(u.getTel())){
user.setTel(u.getTel());
}
//查询条件
Example<User> example = Example.of(user);
//排序条件
Sort sort = Sort.by(Sort.Direction.DESC, "id");
List<User> all = userRepository.findAll(example, sort);
return JSONObject.toJSONString(all);
}
这个查询条件只能是完全匹配,不能灵活查询,如果想要灵活查询,就用到了ExampleMatcher
public String getUserListByCondition(User u){
User user = new User();
if(u.getId() != null){
user.setId(u.getId());
}
if(!StringUtils.isEmpty(u.getUser_name())){
user.setUser_name(u.getUser_name());
}
if(!StringUtils.isEmpty(u.getTel())){
user.setTel(u.getTel());
}
//匹配条件
ExampleMatcher matcher = ExampleMatcher.matching()
.withMatcher("tel", ExampleMatcher.GenericPropertyMatchers.startsWith())//模糊查询匹配开头,即{tel}%
.withMatcher("user_name" ,ExampleMatcher.GenericPropertyMatchers.contains())//全部模糊查询,即%{user_name}%
//.withIgnorePaths("password");//忽略字段,即不管password是什么值都不加入查询条件
//查询条件
Example<User> example = Example.of(user, matcher);
//排序条件
Sort sort = Sort.by(Sort.Direction.DESC, "id");
List<User> all = userRepository.findAll(example, sort);
return JSONObject.toJSONString(all);
}
java1.8环境下使用lambda
.withMatcher("tel", match->match.startsWith())//1.8 lambda
.withMatcher("user_name", match->match.contains());
6.2加入分页查询
public String getUserListByPaging(@PageableDefault(size = 1, page = 0, sort = "id",
direction = Sort.Direction.DESC)
Pageable pageable){
Page<User> allAndPaging = userRepository.findAll(pageable);
return JSONObject.toJSONString(allAndPaging);
}
Pageable中直接可以配置Sort属性
7、使用Specification查询构造器
public Page findAllByPojo(HIP hip, int pageNum, int pageSize){
Pageable pageable = PageRequest.of(pageNum, pageSize, Sort.Direction.DESC, "createTime");
//Specification查询构造器
Specification specification = new Specification() {
@Override
public Predicate toPredicate(Root root, CriteriaQuery query, CriteriaBuilder criteriaBuilder) {
Predicate c1,c2,c3,c4,c5,c6,c7,c8,c9 = null;
if(!StringUtils.isEmpty(hip.getAddress())){
c1 = criteriaBuilder.like(root.get("add"), "%" + hip.getAdd() + "%");
}else{
c1 = criteriaBuilder.like(root.get("add"), "%%");
}
if(hip.getCi() != null){
c2 = criteriaBuilder.equal(root.get("ci"), hip.getCi());
}else{
c2 = criteriaBuilder.isNotNull(root.get("ci"));
}
if(hip.getStatus() != null){
c5 = criteriaBuilder.equal(root.get("status"), hip.getStatus());
}else{
c5 = criteriaBuilder.isNotNull(root.get("status"));
}
if(hip.getTimeFrom() != null && hip.getTimeTo() != null){
c7 = criteriaBuilder.between(root.get("createTime"), hip.getTimeFrom(), hip.getTimeTo());
}else{
c7 = criteriaBuilder.isNotNull(root.get("createTime"));
}
if(hip.getCreateTime() != null){
c8 = criteriaBuilder.greaterThan(root.get("timeFrom"), hip.getCreateTime());
c9 = criteriaBuilder.lessThan(root.get("timeTo"), hip.getCreateTime());
}else{
c8 = criteriaBuilder.isNotNull(root.get("timeFrom"));
c9 = criteriaBuilder.isNotNull(root.get("timeTo"));
}
Predicate x = criteriaBuilder.or(c8, c9);
query.where(c1, c2, c5, c6, c7, x);
return null;
}
};
return hipRepository.findAll(specification, pageable);
}