Java中PageHelper分页后对list操作导致分页无效

网友投稿 527 2022-10-23


Java中PageHelper分页后对list操作导致分页无效

1.问题

阿里巴巴java开发手册

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

@Override

public PageInfo getRecordsByView(int pageNo, int pageSize) {

PageHelper.startPage(pageNo,pageSize);

List hdQueryVosByView = actionMapper.getActionByView();

List 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());

MydwBEXQBhdQueryVoSingle.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 pageViewInfo = new PageInfo<>(hdQueryVos);

return pageViewInfo;

}

可以分页,但是数据量错误,total始终等于每页数据量,即pageSize

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

@Override

public PageInfo getRecordsByView(int pageNo, int pageSize) {

List hdQueryVosByView = actionMapper.getActionByView();

List 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.getHdStarthttp://Date());

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 pageViewInfo = new PageInfo<>(hdQueryVos);

return pageViewInfo;

}

数据可以查询出来,总数total也正确,但是分页功能失效

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

@Override

public PageInfo getRecordsByView(int pageNo, int pageSize) {

PageInfo source = PageHelper.startPage(pageNo, pageSize).doSelectPageInfo(()->{

actionMapper.getActionByView();

});

// 需要转换的对象

PageInfo target = new PageInfo<>();

// 复制分页属性

BeanUtils.copyProperties(source, target);

// 对查询的list进行下一步操作,比如类型转换后

List collect = source.getList().stream().collect(Collectors.toList());

List 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.MydwBEXQBgetHdType().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官方文档

PageHelper分页查询结果后再对数据List操作的方法


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:如何在iPad Numbers 表格中设置表格换行、储存格换行?
下一篇:基于神经网络的实体识别和关系抽取联合学习
相关文章

 发表评论

暂时没有评论,来抢沙发吧~