分页获取信息问题(Spring Data MongoDB)

public QueryResponseResult findList(Integer page, Integer pageSize, QueryPageRequest queryPageRequest) {

        if(page<=0){
            page = 1;
        }
        page = page -1;

        //List<CmsPage> pageList = cmsPageRepository.findAll();
        //设置分页
        Pageable pageable = PageRequest.of(page, pageSize);

        //创建CmsPage对象,存放条件
        CmsPage cmsPage = new CmsPage();

        //创建条件匹配器对象
        ExampleMatcher exampleMatcher = ExampleMatcher.matching();
        //if(queryPageRequest.getSiteId()!=null && !queryPageRequest.getSiteId().equals(""))
        if(StringUtils.isNotEmpty(queryPageRequest.getSiteId())){
            cmsPage.setSiteId(queryPageRequest.getSiteId());
        }

        if (StringUtils.isNotEmpty(queryPageRequest.getPageAliase())){
            cmsPage.setPageAliase(queryPageRequest.getPageAliase());
            //!!!!!!!!!!!!!!!!!!!!!!!!  exampleMatcher.withMatcher()返回值也是ExampleMatcher,会覆盖,要特殊处理
            exampleMatcher.withMatcher("pageAliase", ExampleMatcher.GenericPropertyMatchers.contains());
        }

        Example<CmsPage> example= Example.of(cmsPage,exampleMatcher);
        //分页查询
        Page<CmsPage> pages = cmsPageRepository.findAll(example,pageable);
        //从查询结果中获取列表信息
        List<CmsPage> pageList = pages.getContent();
        //从查询结果中获取总条数
        long totalElements = pages.getTotalElements();
        QueryResult<CmsPage> queryResult = new QueryResult<>();

        queryResult.setList(pageList);
        queryResult.setTotal(totalElements);

        QueryResponseResult result = new QueryResponseResult(CommonCode.SUCCESS, queryResult);
        return result;
    }

在页面分页查询的时候,发现模糊查询查询不到数据,但没有返回任何错误信息,令人十分苦恼...

好在最后找到了这个坑,原因是条件匹配器对象exampleMatcher 在指定查询规则的时候,即下面这行代码

exampleMatcher.withMatcher("pageAliase",ExampleMatcher.GenericPropertyMatchers.contains());

这个方法的返回值仍是一个ExampleMatcher,如果不进行处理,就相当于又重新创建了个新的ExampleMatcher对象

exampleMatcher = exampleMatcher.withMatcher("pageAliase", ExampleMatcher.GenericPropertyMatchers.contains());

这样接收一下就跳出了这个隐藏的坑... 


                
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

岳有才

希望能帮到你

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值