我是靠谱客的博主 威武奇异果,最近开发中收集的这篇文章主要介绍添加分页功能时PageInfo返回错误的pages,分页不生效,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

        今天在给别人的查询接口添加分页功能时,发现PageInfo的分页功能失效,pages总是为1,且total值等于当前页面的数据值,不能正确返回数据总数。

这是返回数据错误代码:

public CommonRspVo listCpmDepartmentInfo(String counterpartyName,int pageNo, int pageSize) {
        List<CpmDepartmentInfoVo> list = new ArrayList<>();
        //查询资金对手部门信息放到map 用counterpartyId做key便于组装树取值
        List<CpmDepartmentInfo> cpmDepartmentInfos = cpmDepartmentInfoDao.listByCounterpartyName(counterpartyName);
        Map<Long, List<CpmDepartmentInfo>> cpmDepartmentInfoMap = cpmDepartmentInfos.stream()
                .filter(cpmDepartmentInfo -> cpmDepartmentInfo.getCounterpartyId() != null)
                .collect(Collectors.groupingBy(CpmDepartmentInfoKey::getCounterpartyId));
        //查询交易对手,组装树
        PageHelper.startPage(pageNo,pageSize);
        List<CpmCounterpartyInfo> mapLists = cpmDepartmentInfoDao.selectCounterPartyName(counterpartyName);
        mapLists.forEach(maps -> {
            CpmDepartmentInfoVo cpmDepartmentInfoVo = new CpmDepartmentInfoVo();
            cpmDepartmentInfoVo.setCounterpartyId(maps.getCounterpartyId().toString());
            cpmDepartmentInfoVo.setCounterpartyName(maps.getCounterpartyName());
            cpmDepartmentInfoVo.setParentId("0");
            cpmDepartmentInfoVo.setChildren(cpmDepartmentInfoMap.get(Long.parseLong(maps.getCounterpartyId().toString())));
            list.add(cpmDepartmentInfoVo);
        });
        PageInfo<CpmDepartmentInfoVo> pageInfo = new PageInfo<>(list);
        return ResponseUtil.ok(pageInfo);
    }

对比了之前写过的分页方法,发现并没有缺少语句,page相关的依赖也正确引入了。代码层面上找不到原因,于是去查看源码,在查看PageInfo构造器源码时,发现了其中的问题,由于返回数据是自己封装的Vo,所以只截取相关的源码,如下:

} else if (list instanceof Collection) {
            this.pageNum = 1;
            this.pageSize = list.size();

            this.pages = this.pageSize > 0 ? 1 : 0;
            this.size = list.size();
            this.startRow = 0;
            this.endRow = list.size() > 0 ? list.size() - 1 : 0;
        }

问题就在这里了, PageHelper.startPage(pageNo,pageSize)这段代码只对它之后首次出现的查询语句生效,即mapLists。后面的pageInfo的传参是list,这里的list跟page封装的分页方法已经完全没有关系了,所以有问题。解决方法如下:

 List<CpmDepartmentInfoVo> list = new ArrayList<>();
        //查询资金对手部门信息放到map 用counterpartyId做key便于组装树取值
        List<CpmDepartmentInfo> cpmDepartmentInfos = cpmDepartmentInfoDao.listByCounterpartyName(counterpartyName);
        Map<Long, List<CpmDepartmentInfo>> cpmDepartmentInfoMap = cpmDepartmentInfos.stream()
                .filter(cpmDepartmentInfo -> cpmDepartmentInfo.getCounterpartyId() != null)
                .collect(Collectors.groupingBy(CpmDepartmentInfoKey::getCounterpartyId));

        //查询交易对手,组装树
        PageHelper.startPage(pageNo,pageSize);
        List<CpmCounterpartyInfo> mapLists = cpmDepartmentInfoDao.selectCounterPartyName(counterpartyName);
        PageInfo<CpmCounterpartyInfo> pageInfo = new PageInfo<>(mapLists);
        mapLists.forEach(maps -> {
            CpmDepartmentInfoVo cpmDepartmentInfoVo = new CpmDepartmentInfoVo();
            cpmDepartmentInfoVo.setCounterpartyId(maps.getCounterpartyId().toString());
            cpmDepartmentInfoVo.setCounterpartyName(maps.getCounterpartyName());
            cpmDepartmentInfoVo.setParentId("0");
            cpmDepartmentInfoVo.setChildren(cpmDepartmentInfoMap.get(Long.parseLong(maps.getCounterpartyId().toString())));
            list.add(cpmDepartmentInfoVo);
        });
        PageInfo<CpmDepartmentInfoVo> page = new PageInfo<>(list);
        BeanUtils.copyProperties(pageInfo,page,new String[]{"list"});
        return ResponseUtil.ok(page);

BTW,copyProperties方法要在传参里加上要忽略替换的对象,即list,不然的话会把list中的数据也替换掉。

最后

以上就是威武奇异果为你收集整理的添加分页功能时PageInfo返回错误的pages,分页不生效的全部内容,希望文章能够帮你解决添加分页功能时PageInfo返回错误的pages,分页不生效所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(80)

评论列表共有 0 条评论

立即
投稿
返回
顶部