pageInfo 的 total 属性是当前页记录数,如何配置使 total 设置成总记录数
原
PageHelper.startPage(page, pageSize);
LambdaQueryWrapper<Credit> queryWrapper = new QueryWrapper<Credit>().
lambda().eq(Credit::getUserId, dto.getUid());
List<Credit> credits = this.baseMapper.selectList(queryWrapper);
List<CreditsDto> infoDtos = credits.stream().map(item -> {
CreditsDto manageInfoDto = new CreditsDto();
BeanUtils.copyProperties(item, manageInfoDto);
return manageInfoDto;
}).collect(Collectors.toList());
PageInfo<CreditsDto> pageInfoVo = new PageInfo<>(credits);
return pageInfoVo;
分析原因
到同样的问题,各种配置无果后,发现代码寄几写的有问题。是PageInfo构造参数传了List实例的原因,一定要传Page实例
解决方案:
Page page = PageHelper.startPage(xx,xx);
// todo:your query
PageInfo pageInfo = new PageInfo<>(page);
!!!重点这里:page 参数一定要传Page的实例,才会得到正确的total,传List的实例会调用size();
查看了源码才知道:
public PageSerializable(List list) {
this.list = list;
if(list instanceof Page){
this.total = ((Page)list).getTotal();
} else {
this.total = list.size(); //!!!如果传List的实例,这里调用的了size
}
}
点击查看代码
PageHelper.startPage(page, pageSize);
LambdaQueryWrapper<Credit> queryWrapper = new QueryWrapper<Credit>().
lambda().eq(Credit::getUserId, dto.getUid());
List<Credit> credits = this.baseMapper.selectList(queryWrapper);
// 1.在此处修改接收page参数
PageInfo<Credit> pageInfo = new PageInfo<>(credits);
List<CreditsDto> infoDtos = credits.stream().map(item -> {
CreditsDto manageInfoDto = new CreditsDto();
BeanUtils.copyProperties(item, manageInfoDto);
return manageInfoDto;
}).collect(Collectors.toList());
PageInfo<CreditsDto> pageInfoVo = new PageInfo<>(infoDtos);
// 2.在此处page 信息复制到 vo page
//entity page 信息复制到 vo page
BeanUtils.copyProperties(pageInfo ,pageInfoVo );
return pageInfoVo;
参考:
https://github.com/pagehelper/pagehelper-spring-boot/issues/21