PageHelp分页查询使用BeanProperties出现total不正确问题如何解决
问题是如何产生的
@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());
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;
@Slf4j
public class BeanPropertiesUtil {
public static <T> T convert(Object source, Class<T> targetClazz) throws Exception {
T t = targetClazz.newInstance();
BeanUtils.copyProperties(source, t);
return t;
}
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;
}
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