PageHelper分页总数、页数错误问题

问题描述

在使用PageHelper进行分页的时候,大部分时候都是正常的,但是有一个接口的分页总数一直有问题(为当前页的数量、页数一直为1)。先看看目前的代码:

Controller层代码(修改前):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public PageResponseMsg queryPolicyTypePageList(@RequestBody PageRequest<PolicyTypePageReq> noticePageReq){
if(noticePageReq == null || noticePageReq.getPage() == null || noticePageReq.getRows() == null){
return this.getPageResponseMsg(MessageCode.Param_NULL.code);
}
// 设置分页
PageHelper.startPage(noticePageReq.getPage(),noticePageReq.getRows());
//查询列表
List<PolicyTypeResp> noticeList = policyTypeService.queryPolicyTypeList(noticePageReq);
if(noticeList == null || noticeList.isEmpty()){
return this.getPageResponseMsg(MessageCode.Data_Not_Found.code);
}
//获取分页数据
PageInfo<PolicyTypeResp> pageInfo = new PageInfo<>(noticeList,noticePageReq.getPage());
return this.getPageResponseMsg(MessageCode.RSP_CODE_SUCCED.code,pageInfo.getList(),pageInfo.getPageNum(),pageInfo.getTotal(),pageInfo.getPages());
}

Service层代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
@Override
public List<PolicyTypeResp> queryPolicyTypeList(PageRequest<PolicyTypePageReq> noticePageReq) {
List<Map<String, Object>> list = policyTypeMapper.queryPolicyTypeList(BeanMap.create(noticePageReq));
List<PolicyTypeResp> policyTypeRespList = new ArrayList<>();
if(list == null || list.size()<=0){
return null;
}
for(Map<String, Object> map: list){
try {
PolicyTypeResp resp = MapTransformUtil.mapToObject(map,PolicyTypeResp.class);
policyTypeRespList.add(resp);
} catch (Exception e) {
log.error("PolicyTypeServiceImpl.queryPolicyTypeList转换异常:"+e.getMessage());
e.printStackTrace();
}
}
return policyTypeRespList;
}

controller代码与其他分页查询接口完全一样,service也没做其他数据库的操作,真是奇怪了。

问题原因

搜了几篇博客,发现问题出现在Map转实体这个步骤。原因如下:

  • PageHelper.startPage(pageIndex, pageSize);返回Page对象(继承ArrayList),里面有总数、当前页、页数等信息。
  • policyTypeService.queryPolicyTypePageList(pageReq);返回的实际也是Page对象,所以在进行Map转换的时候会丢失Page的分页信息。

PageHelper的部分源码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
public PageInfo(List<T> list, int navigatePages) {
super(list);
this.isFirstPage = false;
this.isLastPage = false;
this.hasPreviousPage = false;
this.hasNextPage = false;
if (list instanceof Page) {// 本来应该走这个逻辑
Page page = (Page)list;
this.pageNum = page.getPageNum();
this.pageSize = page.getPageSize();
this.pages = page.getPages();
this.size = page.size();
if (this.size == 0) {
this.startRow = 0L;
this.endRow = 0L;
} else {
this.startRow = page.getStartRow() + 1L;
this.endRow = this.startRow - 1L + (long)this.size;
}
} 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 = 0L;
this.endRow = list.size() > 0 ? (long)(list.size() - 1) : 0L;
}

if (list instanceof Collection) {
this.calcByNavigatePages(navigatePages);
}

}

问题解决

把服务层代码修改一下,核心就是数据库查出list后,直接创建PageInfo对象,具体代码如下:

1
2
3
4
5
6
7
8
// 设置分页
PageHelper.startPage(pageReq.getPage(),pageReq.getRows());
List<Map<String, Object>> list = policyTypeMapper.queryPolicyTypeList(BeanMap.create(pageReq));
if(list == null || list.size()<=0){
return null;
}
//获取分页数据
PageInfo<Map<String, Object>> pageInfo = new PageInfo<>(list,pageReq.getPage());
-------------本文结束感谢您的阅读-------------
失败是成功之母,打赏是成功支付