controller:
public PageInfo<UserFromVO> listUser(User user, CommonQo commonQo) {
PageHelper.startPage(commonQo.getPageNum(), commonQo.getPageSize());
List<User> list = userService.findList(user);
List<UserFromVO> voList = new ArrayList();
for(User u : list) {
if(u.type==1) {
***
voList.add
}
}
return new PageInfo<>(volist);
}
很简单的几行代码,就是自己写了一个vo用来封装要回显页面的数据,很普通的做法,但是现实的时候数据是有,一页也显示了10条,但总条数总是不对(56条),total的值和pageSize的值是一样的
解决:
public PageInfo<UserFromVO> listUser(User user, CommonQo commonQo) {
PageHelper.startPage(commonQo.getPageNum(), commonQo.getPageSize());
List<User> list = userService.findList(user);
//debug断点
PageInfo<User> pageInfo = new PageInfo<>(list);
List<UserFromVO> voList = new ArrayList();
for(User u : list) {
if(u.type==1) {
***
voList.add
}
}
//
PageInfo<UserFromVO> page= new PageInfo<>(volist);
BeanUtils.copyProperties(pageInfo,page);
return page;
}
打断点看了一下,原因是自己封装的vo把PageInfo原有的属性破坏了,看一下源码:
/**
* 包装Page对象
*
* @param list page结果
* @param navigatePages 页码数量
*/
public PageInfo(List<T> list, int navigatePages) {
if (list instanceof Page) {
Page page = (Page) list;
this.pageNum = page.getPageNum();
this.pageSize = page.getPageSize();
this.orderBy = page.getOrderBy();
this.pages = page.getPages();
this.list = page;
this.size = page.size();
this.total = page.getTotal();
//由于结果是>startRow的,所以实际的需要+1
if (this.size == 0) {
this.startRow = 0;
this.endRow = 0;
} else {
this.startRow = page.getStartRow() + 1;
//计算实际的endRow(最后一页的时候特殊)
this.endRow = this.startRow - 1 + this.size;
}
} else if (list instanceof Collection) {
this.pageNum = 1;
this.pageSize = list.size();
this.pages = 1;
this.list = list;
this.size = list.size();
this.total = list.size();
this.startRow = 0;
this.endRow = list.size() > 0 ? list.size() - 1 : 0;
}
if (list instanceof Collection) {
this.navigatePages = navigatePages;
//计算导航页
calcNavigatepageNums();
//计算前后页,第一页,最后一页
calcPage();
//判断页面边界
judgePageBoudary();
}
}
相信你也看出了关键: