pageinfo-只能读取当前条数不能获取总条数

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

https://blog.csdn.net/lzh754413563/article/details/105110324?spm=1001.2101.3001.6650.2&utm_medium=distribute.pc_relevant.none-task-blog-2~default~CTRLIST~Rate-2-105110324-blog-96873360.235^v38^pc_relevant_sort&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2~default~CTRLIST~Rate-2-105110324-blog-96873360.235^v38^pc_relevant_sort&utm_relevant_index=5

posted @ 2023-08-16 11:33  逐星i  阅读(268)  评论(0编辑  收藏  举报