1.问题:
使用 pagehelper ,如果和mybatis版本不匹配,就会出现返回结果错误问题,比如:本来想返回pageNum:1,pageSize:1的数据,但却是全部返回。自己的代码没问题的情况下,最有可能的就是版本不匹配的问题。
2.解决方案:
出现这个问题很可能就是版本不匹配。可以使用下面的版本进行测试,本人测试成功。
org.mybatis
mybatis
3.5.5
org.mybatis.spring.boot
mybatis-spring-boot-starter
2.1.3
com.github.pagehelper
pagehelper-spring-boot-starter
1.4.1
3.pagehelper使用示例如下:
可以看到:使用的时候只需要pagehelper.startPage方法;然后在new Page放入返回列表就行。
3.1.server impl层
@Override
public PageInfo listForAdmin(Integer pageNum, Integer pageSize) {
PageHelper.startPage(pageNum, pageSize, "type, order_num");
List categoryList = categoryMapper.selectList();
PageInfo pageInfo = new PageInfo(categoryList);
return pageInfo;
}
3.2.service层
PageInfo listForAdmin(Integer pageNum, Integer pageSize);
3.3.controller层
@ApiOperation("后台目录列表")
@GetMapping("admin/category/list")
@ResponseBody
public ApiRestResponse listCategoryForAdmin(@RequestParam Integer pageNum,
@RequestParam Integer pageSize) {
PageInfo pageInfo = categoryService.listForAdmin(pageNum, pageSize);
return ApiRestResponse.success(pageInfo);
}
希望能够帮助你,谢谢!
---------------------------------------------------------------------------------------------------------------------------------
看到的一个很好的pagehelper问题,在这里记录,以免下次自己遇到同样的问题:
成功路上并不拥挤,因为坚持的人不多。
blog.csdn.net/qq_35080796
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());
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 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.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 pageViewInfo = new PageInfo<>(hdQueryVos);
return pageViewInfo;
}
数据可以查询出来,总数total也正确,但是分页功能失效。
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语句没有参与分页查询
直接对分页后的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.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;
}
问题看起来不复杂,但初级和中级程序员遇到后基本上都素手无策。不会看源码,不知道钻研,永远都只停留在CRUD上,最多也就算是入了个门。
参考资料:
PageHelper官方文档:apidoc.gitee.com/free/Mybatis_PageHelper
PageHelper分页查询结果后再对数据List操作的方法