PageHelper分页后,对list操作会导致分页无效

沙海 2021年5月18日04:57:53Java评论46字数 5842阅读19分28秒阅读模式
摘要

PageHelper分页后,对list操作会导致分页无效 点击关注 ? 小哈学Java

PageHelper分页后,对list操作会导致分页无效

点击关注 ? 小哈学Java 文章源自JAVA秀-https://www.javaxiu.com/23290.html

点击上方蓝色“小哈学Java”,选择“设为星标”回复“资源”获取独家整理的学习资料!

来源:blog.csdn.net/qq_35080796/article/details/105508744文章源自JAVA秀-https://www.javaxiu.com/23290.html

1.问题

PageHelper分页后,对list操作会导致分页无效文章源自JAVA秀-https://www.javaxiu.com/23290.html

阿里巴巴Java开发手册文章源自JAVA秀-https://www.javaxiu.com/23290.html

1.1.PageHelper先开启分页,后对list数据操作

@Override    public PageInfo<HdQueryVo> getRecordsByView(int pageNo, int pageSize) {        PageHelper.startPage(pageNo,pageSize);        List<HdQueryVo> hdQueryVosByView = actionMapper.getActionByView();        List<HdQueryVo> hdQueryVos = new ArrayList<>();        for (HdQueryVo hdQueryVo : hdQueryVosByView) {            HdQueryVo hdQueryVoSingle = new HdQueryVo();            hdQueryVoSingle.setHdId(hdQueryVo.getHdId());            hdQueryVoSingle.setHdType(hdQueryVo.getHdType());            hdQueryVoSingle.setHdTitle(hdQueryVo.getHdTitle());            hdQueryVoSingle.setHdStartDate(hdQueryVo.getHdStartDate());            hdQueryVoSingle.setHdEndDate(hdQueryVo.getHdEndDate());            hdQueryVoSingle.setHdStatus(hdQueryVo.getHdStatus());            hdQueryVoSingle.setHdImage(hdQueryVo.getHdImage());            hdQueryVoSingle.setHdNumber(hdQueryVo.getHdNumber());            hdQueryVoSingle.setGmtCreate(hdQueryVo.getGmtCreate());            hdQueryVoSingle.setGmtModified(hdQueryVo.getGmtModified());            hdQueryVoSingle.setUserId(hdQueryVo.getUserId());            if (hdQueryVo.getHdType().equals(0)) {                hdQueryVoSingle.setHdJoinUsers(onlineWorksMapper.getOnlineJoinUsers(hdQueryVo.getHdId()));            } else {                hdQueryVoSingle.setHdJoinUsers(offlineUsersMapper.getOfflineJoinUsers(hdQueryVo.getHdId()));            }            hdQueryVos.add(hdQueryVoSingle);        }        PageInfo<HdQueryVo> pageViewInfo = new PageInfo<>(hdQueryVos);        return pageViewInfo;    }

可以分页,但是数据量错误,total始终等于每页数据量,即pageSize文章源自JAVA秀-https://www.javaxiu.com/23290.html

PageHelper分页后,对list操作会导致分页无效文章源自JAVA秀-https://www.javaxiu.com/23290.html

PageHelper分页后,对list操作会导致分页无效文章源自JAVA秀-https://www.javaxiu.com/23290.html

1.2.先对list数据进行操作,后开启分页

@Override    public PageInfo<HdQueryVo> getRecordsByView(int pageNo, int pageSize) {                List<HdQueryVo> hdQueryVosByView = actionMapper.getActionByView();        List<HdQueryVo> hdQueryVos = new ArrayList<>();        for (HdQueryVo hdQueryVo : hdQueryVosByView) {            HdQueryVo hdQueryVoSingle = new HdQueryVo();            hdQueryVoSingle.setHdId(hdQueryVo.getHdId());            hdQueryVoSingle.setHdType(hdQueryVo.getHdType());            hdQueryVoSingle.setHdTitle(hdQueryVo.getHdTitle());            hdQueryVoSingle.setHdStartDate(hdQueryVo.getHdStartDate());            hdQueryVoSingle.setHdEndDate(hdQueryVo.getHdEndDate());            hdQueryVoSingle.setHdStatus(hdQueryVo.getHdStatus());            hdQueryVoSingle.setHdImage(hdQueryVo.getHdImage());            hdQueryVoSingle.setHdNumber(hdQueryVo.getHdNumber());            hdQueryVoSingle.setGmtCreate(hdQueryVo.getGmtCreate());            hdQueryVoSingle.setGmtModified(hdQueryVo.getGmtModified());            hdQueryVoSingle.setUserId(hdQueryVo.getUserId());            if (hdQueryVo.getHdType().equals(0)) {                hdQueryVoSingle.setHdJoinUsers(onlineWorksMapper.getOnlineJoinUsers(hdQueryVo.getHdId()));            } else {                hdQueryVoSingle.setHdJoinUsers(offlineUsersMapper.getOfflineJoinUsers(hdQueryVo.getHdId()));            }            hdQueryVos.add(hdQueryVoSingle);        }        PageHelper.startPage(pageNo,pageSize);        PageInfo<HdQueryVo> pageViewInfo = new PageInfo<>(hdQueryVos);        return pageViewInfo;    }

数据可以查询出来,总数total也正确,但是分页功能失效。(搜索公众号Java知音,回复“2021”,送你一份Java面试题宝典)文章源自JAVA秀-https://www.javaxiu.com/23290.html

PageHelper分页后,对list操作会导致分页无效文章源自JAVA秀-https://www.javaxiu.com/23290.html

PageHelper分页后,对list操作会导致分页无效文章源自JAVA秀-https://www.javaxiu.com/23290.html

PageHelper分页后,对list操作会导致分页无效文章源自JAVA秀-https://www.javaxiu.com/23290.html

2.原因

PageHelper中startPage开启分页方法只对后面的sql查询起作用文章源自JAVA秀-https://www.javaxiu.com/23290.html

1.1 错误原因是提前开启分页后,对list操作,即PageInfo pageViewInfo = new PageInfo<>(hdQueryVos);文章源自JAVA秀-https://www.javaxiu.com/23290.html

PageHelper分页后,对list操作会导致分页无效文章源自JAVA秀-https://www.javaxiu.com/23290.html

/*** 包装Page对象** @param list*/public PageInfo(List list) {this(list, 8);}

只是把list转为PageInfo对象,不影响前面分页数据的操作文章源自JAVA秀-https://www.javaxiu.com/23290.html

1.2 错误原因是先对list操作后,开启翻页后没有sql语句文章源自JAVA秀-https://www.javaxiu.com/23290.html

PageHelper分页后,对list操作会导致分页无效文章源自JAVA秀-https://www.javaxiu.com/23290.html

即sql语句没有参与分页查询文章源自JAVA秀-https://www.javaxiu.com/23290.html

3.解决方案

直接对分页后的PageInfo对象中的数据进行操作文章源自JAVA秀-https://www.javaxiu.com/23290.html

  • 对list集合操作,先取出PageInfo里的list集合数据,再对数据进行相关操作文章源自JAVA秀-https://www.javaxiu.com/23290.html

  • 将操作完后的list集合再次存到PageInfo里,进行return文章源自JAVA秀-https://www.javaxiu.com/23290.html

PageHelper分页后,对list操作会导致分页无效文章源自JAVA秀-https://www.javaxiu.com/23290.html

PageHelper分页后,对list操作会导致分页无效文章源自JAVA秀-https://www.javaxiu.com/23290.html

@Override    public PageInfo<HdQueryVo> getRecordsByView(int pageNo, int pageSize) {        PageInfo<HdQueryVo> source = PageHelper.startPage(pageNo, pageSize).doSelectPageInfo(()->{            actionMapper.getActionByView();        });        // 需要转换的对象        PageInfo<HdQueryVo> target = new PageInfo<>();        // 复制分页属性        BeanUtils.copyProperties(source, target);        // 对查询的list进行下一步操作,比如类型转换后        List<HdQueryVo> collect = source.getList().stream().collect(Collectors.toList());        List<HdQueryVo> hdQueryVos = new ArrayList<>();        for (HdQueryVo hdQueryVo : collect) {            HdQueryVo hdQueryVoSingle = new HdQueryVo();            hdQueryVoSingle.setHdId(hdQueryVo.getHdId());            hdQueryVoSingle.setHdType(hdQueryVo.getHdType());            hdQueryVoSingle.setHdTitle(hdQueryVo.getHdTitle());            hdQueryVoSingle.setHdStartDate(hdQueryVo.getHdStartDate());            hdQueryVoSingle.setHdEndDate(hdQueryVo.getHdEndDate());            hdQueryVoSingle.setHdStatus(hdQueryVo.getHdStatus());            hdQueryVoSingle.setHdImage(hdQueryVo.getHdImage());            hdQueryVoSingle.setHdNumber(hdQueryVo.getHdNumber());            hdQueryVoSingle.setGmtCreate(hdQueryVo.getGmtCreate());            hdQueryVoSingle.setGmtModified(hdQueryVo.getGmtModified());            hdQueryVoSingle.setUserId(hdQueryVo.getUserId());            if (hdQueryVo.getHdType().equals(0)) {                hdQueryVoSingle.setHdJoinUsers(onlineWorksMapper.getOnlineJoinUsers(hdQueryVo.getHdId()));            } else {                hdQueryVoSingle.setHdJoinUsers(offlineUsersMapper.getOfflineJoinUsers(hdQueryVo.getHdId()));            }            hdQueryVos.add(hdQueryVoSingle);        }        // 加工后的数据放入新的pageinfo        target.setList(hdQueryVos);        return target;    }

PageHelper分页后,对list操作会导致分页无效文章源自JAVA秀-https://www.javaxiu.com/23290.html

PageHelper分页后,对list操作会导致分页无效文章源自JAVA秀-https://www.javaxiu.com/23290.html

PageHelper分页后,对list操作会导致分页无效文章源自JAVA秀-https://www.javaxiu.com/23290.html

参考资料:

  • PageHelper官方文档文章源自JAVA秀-https://www.javaxiu.com/23290.html

  • PageHelper分页查询结果后再对数据List操作的方法文章源自JAVA秀-https://www.javaxiu.com/23290.html

1. 在 SQL 中该如何处理NULL值,你真的清楚么?2. 美团实习面试:熟悉红黑树是吧?能不能写一下?3. CTO 说了,如果发现谁用 kill -9 关闭程序就开除4. 你可能用错了 kafka 的重试机制最近面试BAT,整理一份面试资料《Java面试BATJ通关手册》,覆盖了Java核心技术、JVM、Java并发、SSM、微服务、数据库、数据结构等等。获取方式:点“在看”,关注公众号并回复 Java 领取,更多内容陆续奉上。

文章有帮助的话,在看,转发吧。文章源自JAVA秀-https://www.javaxiu.com/23290.html

谢谢支持哟 (*^__^*)文章源自JAVA秀-https://www.javaxiu.com/23290.html

继续阅读
速蛙云 - 极致体验,强烈推荐!!!购买套餐就免费送各大视频网站会员!快速稳定、独家福利社、流媒体稳定解锁!速度快,全球上网、视频、游戏加速、独立IP均支持!基础套餐性价比很高!这里不多说,我一直正在使用,推荐购买:https://www.javaxiu.com/59919.html
weinxin
资源分享QQ群
本站是JAVA秀团队的技术分享社区, 会经常分享资源和教程; 分享的时代, 请别再沉默!
沙海
匿名

发表评论

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定