【springboot mybatisplus】pageInfo 的 total 属性是当前页记录数,如何配置使 total 设置成总记录数

前言

在使用mybatisplus时,pageInfo 的 total 属性是当前页记录数(eg:10),如何配置使 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;

总结

别在中间进行列表操作 PageInfo pageInfoVo = new
PageInfo<>(credits);在之前之前不要进行数据操作

参考:

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%7Edefault%7ECTRLIST%7ERate-2-105110324-blog-96873360.235%5Ev38%5Epc_relevant_sort&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7ERate-2-105110324-blog-96873360.235%5Ev38%5Epc_relevant_sort&utm_relevant_index=5

你可能感兴趣的:(Java,sql,spring,boot,后端,java,sql)