jpa自定义条件分页查询

主要依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

这里以我最近自己瞎折腾的项目部分代码为例子(仅展示主要部分):

实体类名称(Confusion)

需要注意的是

类上+ @Entity

主键字段+ @Id

 

package cn.zytao.taosir.disabuse.dao;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;

import cn.zytao.taosir.common.model.disabuse.Confusion;

public interface ConfusionRepository extends JpaRepository<Confusion,String>, JpaSpecificationExecutor<Confusion>{
}

 

JpaRpository这里就不多讲了,需要注意的是,同时继承JpaSpecificationExecutor

我们发现,继承该类后,可以使用相关的API

T findOne(Specification<T> spec);

List<T> findAll(Specification<T> spec);

Page<T> findAll(Specification<T> spec, Pageable pageable);

List<T> findAll(Specification<T> spec, Sort sort);

long count(Specification<T> spec);

注意Specification类 

通过源码可以发现这个方法,关键就是重写它了

在Service层中

    @SuppressWarnings({ "deprecation", "serial" })
    @Override
    public Page<Confusion> findByPageAndParams(Confusion params, int pageNumber, int pageSize) {
        Pageable pageable = new PageRequest(pageNumber, pageSize);
        Specification<Confusion> confusion=new Specification<Confusion>() {
            @Override
            public Predicate toPredicate(Root<Confusion> root, CriteriaQuery<?> query,
                    CriteriaBuilder criteriaBuilder) {
                Path<String> theme = root.get("theme");
                return criteriaBuilder.like(theme, "%"+params.getTheme()+"%");
            }
        };
        return confusionRepository.findAll(confusion, pageable);
    }

如果使用的springboot是2.0X以上的版本,new PageRequest的构造方法已经过时,推荐使用

Pageable pageable = PageRequest.of(pageNumber, pageSize);

以上仅做参考,CriteribBuilder用于构造自定义的查询条件(即条件构造器)

 

通过相关的方法,我们可以用集合来处理多条件的查询,例子

    @Override
    public JSONObject list(Integer page, Integer size, String remark,Integer method,Integer status) {
        Pageable pageable = PageRequest.of(page-1, size);
        Specification<Authority> authorityQuery=new Specification<Authority>() {
            @Override
            public Predicate toPredicate(Root<Authority> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicates=new ArrayList<>();
                if(remark!=null)
                    predicates.add(criteriaBuilder.like(root.get("remark"), "%"+remark+"%"));
                if(method!=null)
                    predicates.add(criteriaBuilder.equal(root.get("method"), method));
                if(status!=null)
                    predicates.add(criteriaBuilder.equal(root.get("status"), status));
                return criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));
            }
        };
        Page<Authority> pageAuthority = authorityMapper.findAll(authorityQuery, pageable);
        return ActionHelper.responseOk(pageAuthority.getContent());
    }
多条件的例子

控制层

    public JSONObject findByParamAndPage(@RequestBody Confusion confusion, int pageNumber, int pageSize) {
        Page<Confusion> result = confusionService.findByPageAndParams(confusion, pageNumber, pageSize);
        JSONObject data=new JSONObject();
        data.put("content", result.getContent());
        data.put("totalNums", result.getTotalElements());
        data.put("totalPages",result.getTotalPages());
        return ActionHelper.responseOk(data);
    }

因为这里我的项目使用的是分布式的一个实现方式,所以这里不提供@RequestMapping,自行编写

下面看看在swagger2下的效果。

 

 

 

 

 

 

posted @ 2018-10-29 23:20  涛先森の日常  阅读(10572)  评论(1编辑  收藏  举报