【PageHelp分页问题】

问题是如何产生的


	/**
     * @Description: 此为service代码,无法直接使用
     * @Params:
     * @Return:
     * @Author: Mr.myq
     * @Date: 2022/9/2313:42
     */
    @Override
    public ServiceResult<PageInfo<SlnListPojo>> b1bPage(int pageNo, int pageSize, String startTime) {
        PageHelper.startPage(pageNo, pageSize); //分页
		// 查询
        LambdaQueryWrapper<Sln> slnLambdaQueryWrapper = new LambdaQueryWrapper<>();
        slnLambdaQueryWrapper.eq(Sln::getExamineStatus, ExamineStatusEnum.EXAMINATION_PASSED.getKey());
        slnLambdaQueryWrapper.orderByDesc(Sln::getCreatedAt);
        slnLambdaQueryWrapper.ge(Sln::getCreatedAt,startTime);
        List<Sln> slns = this.baseMapper.selectList(slnLambdaQueryWrapper);
        List<Long> slnIds = slns.stream().map(Sln::getId).collect(Collectors.toList());
        //转换后的总数量不正确,此处使用convertPage()
        SlnListPojo slnListPojoPageInfo = BeanPropertiesUtil.convertList(slns, SlnListPojo.class);
              return ServiceResult.ok(slnListPojoPageInfo,"ok");
     }
     

如何解决 使用converPage方案,有兴趣的可以查看俩个方法的区别


```package com.innovation.ic.b1b.util;

import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

/**
 * @Description: 转换工具类
 * @ClassName: BeanPropertiesUtil
 * @Author: myq
 * @Date: 2022/8/5 下午3:54
 * @Version: 1.0
 */
@Slf4j
public class BeanPropertiesUtil {

    /**
     * @return
     * @description obj转换工具
     * @parms
     * @author Mr.myq
     * @datetime 2022/8/5 下午3:59
     */
    public static <T> T convert(Object source, Class<T> targetClazz) throws Exception {
        T t = targetClazz.newInstance();
        BeanUtils.copyProperties(source, t);
        return t;
    }


    /**
     * @return
     * @description list转换方法
     * @parms
     * @author Mr.myq
     * @datetime 2022/8/5 下午4:06
     */
    public static <T> List<T> convertList(Collection<?> source, Class<T> targetClazz) {
        List<T> tList = null;
        try {
            tList = new ArrayList<>(source.size());
            for (Object obj : source) {
                T t = targetClazz.newInstance();
                BeanUtils.copyProperties(obj, t);
                tList.add(t);
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("List<T>转换异常:", e);
            throw new RuntimeException("List<T>转换异常");
        }
        return tList;
    }




    /**
     * @return
     * @description list转换方法
     * @parms
     * @author Mr.myq
     * @datetime 2022/8/5 下午4:06
     */
    public static <T> PageInfo<T> convertPage(List<?> source, Class<T> targetClazz) {
        PageInfo<T> ts = null;
        try {
            if(CollectionUtils.isEmpty(source))
                return new PageInfo<>(Collections.emptyList());
            //获取分页总数
            PageInfo<?> pageInfo = new PageInfo<>(source);
            List<T> tList = new ArrayList<>(source.size());
            for (Object obj : source) {
                T t = targetClazz.newInstance();
                BeanUtils.copyProperties(obj, t);
                tList.add(t);
            }
            ts = new PageInfo<T>(tList);
            ts.setTotal(pageInfo.getTotal());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("convertPage<T>转换异常:", e);
            throw new RuntimeException("convertPage<T>转换异常");
        }
        return ts;
    }

}

jar包:spring-bean、pageHelp

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

心有城府,腹有良谋

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值