<!-- 分页插件 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.5</version>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-autoconfigure</artifactId>
<version>1.2.5</version>
</dependency>
pagehelper: #pagehelper分页插件
auto-runtime-dialect: true
support-methods-arguments: true
reasonable: true
params: count=countSql
public PageInfo<GoodsInfoDto> selectByPageInfo(Integer pageNum, Integer pageSize) throws Exception {
if (pageNum == null || pageSize == null) {
throw new BindException("参数错误!");
}
PageHelper.startPage(pageNum, pageSize);
List<GoodsInfo> goodsInfos = goodsInfoMapper.selectAll();
List<GoodsInfoDto> dtos = goodsInfos.stream().map(goodsInfo -> new GoodsInfoDto().convertGoodsInfo(goodsInfo)).collect(Collectors.toList());
PageInfo<GoodsInfoDto> pageInfo = new PageInfo<>(dtos);
return pageInfo;
错误原因: 在service中,使用dao获取到数据后因为的把数据转成dto之后然后传入的PageInfo对象中的,所以出现了问题。
如果直接把dao获取到的数据直接传递给PageInfo对象中就不会出现该问题。
修改之后的代码:
public PageInfo<GoodsInfoDto> selectByPageInfo(Integer pageNum, Integer pageSize) throws Exception {
if (info.getPageNum() == null || info.getPageSize() == null) {
throw new BindException("参数错误!");
}
PageHelper.startPage(info.getPageNum(), info.getPageSize());
List<GoodsInfo> goodsInfos = goodsInfoMapper.selectAll();
// 如果在获取到数据之后就对数据进行转dto操作的话,会获取不到total数据,所以又定义了一个PageInfo类然后将数据进行属性复制,来获取数据
PageInfo<GoodsInfoDto> pageInfo1 = new PageInfo<>();
BeanUtils.copyProperties(new PageInfo<>(goodsInfos), pageInfo1);
pageInfo1.setList(goodsInfos.stream().map(goodsInfo -> new GoodsInfoDto().convertGoodsInfo(goodsInfo)).collect(Collectors.toList()));
return pageInfo1;
}