SpringDataJpa使用Pageable+ExampleMatcher进行分页多条件查询

之前的文章讲了SpingDataJPA之ExampleMatcher实例查询,今天再度进行拓展,接着讲SpringDataJpa使用Pageable+ExampleMatcher进行分页多条件查询

SpingDataJPA之ExampleMatcher实例查询
https://blog.csdn.net/moshowgame/article/details/80282813

Repository层

@Repository
public interface LostPropertyRespository extends JpaRepository<LostProperty, Integer> {

}

Controller/Service层调用

    @Autowired
    private LostPropertyRespository lostPropertyRespository;
    /**
     * 新增和修改失物招领信息
     */
    @RequestMapping("/lost/list")
    public ApiReturnObject getLostPropertyList(String materialName,Timestamp registerTime,String status,Integer pageNumber,Integer pageSize) {
        LostProperty obj=new LostProperty();
        obj.setMaterialName(materialName);
        obj.setRegisterTime(registerTime);
        obj.setStatus(status);
        //创建匹配器,即如何使用查询条件
        ExampleMatcher matcher = ExampleMatcher.matching() //构建对象
                .withMatcher("materialName", GenericPropertyMatchers.contains()) //姓名采用“开始匹配”的方式查询
                .withMatcher("registerTime", GenericPropertyMatchers.contains()) //姓名采用“开始匹配”的方式查询
                .withMatcher("status", GenericPropertyMatchers.contains()) //姓名采用“开始匹配”的方式查询
                .withIgnorePaths("id");  //忽略属性:是否关注。因为是基本类型,需要忽略掉

        //创建实例
        Example<LostProperty> ex = Example.of(obj, matcher); 
        //分页
        //Pageable是接口,PageRequest是接口实现
        //PageRequest的对象构造函数有多个,page是页数,初始值是0,size是查询结果的条数,后两个参数参考Sort对象的构造方法
        //以前是用new PageRequest(pageNo,pageSize,Sort.Direction.DESC,"id")的方法,但是那个2。7之后就被遗弃了,现在直接用PageRequest.of的方法。简单粗暴,无需new。
        //并且这里最好做空值判断,不然没传值就容易空指针异常
        if(pageNumber==null) pageNumber=1;
        if(pageSize==null) pageNumber=10;
        Pageable pageable = PageRequest.of(pageNumber,pageSize,Sort.Direction.DESC,"id");

        //查询
        Page<LostProperty> ls = lostPropertyRespository.findAll(ex,pageable);
        return ApiReturnUtil.page(pageNo,pageSize,ls);
    }

关于PageRequest

Pageable pageable = PageRequest.of(pageNo,pageSize,Sort.Direction.DESC,”id”);

以前是用new PageRequest(pageNo,pageSize,Sort.Direction.DESC,”id”)的方法,但是那个2。7之后就被遗弃了,现在直接用PageRequest.of的方法。简单粗暴,无需new。

https://docs.spring.io/spring-data/data-commons/docs/current/api/org/springframework/data/domain/PageRequest.html

Page对象

返回的对象是returnObject里面的内容

{
    "errorCode": "00",
    "errorMessage": "success",
    "pageNo": 1,
    "pageSize": 2,
    "returnObject": [
        {
            "content": [
                {
                    "description": "蓝色",
                    "id": 6,
                    "status": "0"
                },
                {
                    "description": "黑色",
                    "id": 5,
                    "status": "0"
                }
            ],
            "first": false,
            "last": false,
            "number": 1,
            "numberOfElements": 2,
            "pageable": {
                "offset": 2,
                "pageNumber": 1,
                "pageSize": 2,
                "paged": true,
                "sort": {
                    "sorted": true,
                    "unsorted": false
                },
                "unpaged": false
            },
            "size": 2,
            "sort": {
                "$ref": "$.returnObject[0].pageable.sort"
            },
            "totalElements": 8,
            "totalPages": 4
        }
    ]
}

属性:
1. size=numberOfElements=pageSize 分页大小
2. number=pageNumber 分页页码
3. totalElements 总共有多少对象
4. totalPages 一共分多少页

猜你喜欢

转载自blog.csdn.net/moshowgame/article/details/80650641