问题描述
在使用PageHelper进行分页的时候,大部分时候都是正常的,但是有一个接口的分页总数一直有问题(为当前页的数量、页数一直为1)。先看看目前的代码:
Controller层代码(修改前):
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层代码:
@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的部分源码:
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);
}
}
问题解决
把服务层代码修改一下,核心就是PageHelper代码放在操作数据库前后(Mapper层),具体代码如下:
// 设置分页
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());
其他问题
在分页开始后对pageReq进行数据封装也会使分页失效。解决方法一样:PageHelper代码放在操作数据库前后(Mapper层)。
// 获取分页
PageHelper.startPage(pageReq.getPage(),pageReq.getRows());
// 如果对pageReq进行数据封装也会使分页失效
小结
- 用PageHelper分页不能对pageReq对象进行赋值,不能对查询出的结果进行对象转换。
- PageHelper代码要放在service层,且紧跟着查询数据库的代码。