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

11 篇文章 0 订阅

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

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

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

}
1
2
3
4
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);
    }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
关于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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
属性: 
1. size=numberOfElements=pageSize 分页大小 
2. number=pageNumber 分页页码 
3. totalElements 总共有多少对象 
4. totalPages 一共分多少页
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值