1.问题
阿里巴巴Java开发手册:
1.1.PageHelper先开启分页,后对list数据操作
@Overridepublic 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
1.2.先对list数据进行操作,后开启分页
@Overridepublic 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 开发手册可以关注公众号Java核心技术,回复:手册)
2.原因
PageHelper中startPage开启分页方法只对后面的sql查询起作用
1.1 错误原因是提前开启分页后,对list操作,即PageInfo pageViewInfo = new PageInfo<>(hdQueryVos);
/*** 包装Page对象** @param list*/public PageInfo(List list) { this(list, 8);}
只是把list转为PageInfo对象,不影响前面分页数据的操作
1.2 错误原因是先对list操作后,开启翻页后没有sql语句
即sql语句没有参与分页查询
3.解决方案
直接对分页后的PageInfo对象中的数据进行操作
- 对list集合操作,先取出PageInfo里的list集合数据,再对数据进行相关操作
- 将操作完后的list集合再次存到PageInfo里,进行return
@Overridepublic 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;}
参考资料:
https://apidoc.gitee.com/free/Mybatis_PageHelper/
https://blog.csdn.net/shanglezeichaun/article/details/105039460
原文链接:https://blog.csdn.net/qq_35080796/article/details/105508744
版权声明:本文为CSDN博主「He Ain't Hero」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
近期热文推荐:
1.600+ 道 Java面试题及答案整理(2021最新版)
2.终于靠开源项目弄到 IntelliJ IDEA 激活码了,真香!
3.阿里 Mock 工具正式开源,干掉市面上所有 Mock 工具!
4.Spring Cloud 2020.0.0 正式发布,全新颠覆性版本!
5.《Java开发手册(嵩山版)》最新发布,速速下载!
觉得不错,别忘了随手点赞+转发哦!
原文转载:http://www.shaoqun.com/a/785191.html
淘粉吧怎么返利:https://www.ikjzd.com/w/1725
landing:https://www.ikjzd.com/w/2368
邮乐网:https://www.ikjzd.com/w/1776
pocket:https://www.ikjzd.com/w/1903
1.问题阿里巴巴Java开发手册:1.1.PageHelper先开启分页,后对list数据操作@OverridepublicPageInfo<HdQueryVo>getRecordsByView(intpageNo,intpageSize){PageHelper.startPage(pageNo,pageSize);List<HdQueryVo>hdQueryVosByVi
绩效:https://www.ikjzd.com/w/31
Lunapic :https://www.ikjzd.com/w/765
运营思维:https://www.ikjzd.com/w/1521
Helicap:https://www.ikjzd.com/w/2038
NARF计划:https://www.ikjzd.com/w/2583
aeo:https://www.ikjzd.com/w/2356
国际标准书号:https://www.ikjzd.com/w/174
口述:我的母性再多也无法维持爱情(3/3):http://lady.shaoqun.com/m/a/41992.html
口述:闺蜜未婚先孕居然要听我男友意见:http://lady.shaoqun.com/m/a/127872.html
最后一次缠绵 他竟拍下我的艳照:http://lady.shaoqun.com/a/273218.html
已婚少妇的混乱情史让我纠结:http://lady.shaoqun.com/a/270220.html
宝贝,我忍不了了,给我 那晚高冷的白领少妇疯狂向我索取:http://lady.shaoqun.com/a/274499.html
口述:奇葩老公赚钱后各种品性原形毕露:http://www.30bags.com/a/254268.html
缠绵时发现女友胸口有不明吻痕:http://www.30bags.com/a/251730.html
女性口述_男人爽到不行是什么感觉:http://www.30bags.com/a/254298.html
刚搬来的漂亮女邻居 口述已婚男和离异女的暧昧经历:http://www.30bags.com/m/a/250447.html
儋州海花岛童世界水上乐园门票2021年6月价格:http://www.30bags.com/a/370317.html
No comments:
Post a Comment