前言
用mybatis-plus 的分页对象的时候,因为用mybatis-puls 查询出来的分页对象的records里的泛型是实体,有时候需要将实体转换为前端展示的对象,所有写了一个分页数据的类型转换工具,解决这个问题。
代码示例
public IPage<TransparentGeologicalVO> pageList(TransparentGeologicalQueryDTO query) {
IPage<TransparentGeological> page=this.lambdaQuery()
.like(StringUtil.isNotBlank(query.getName()),TransparentGeological::getName,query.getName())
.eq(Objects.nonNull(query.getWorkFaceId()),TransparentGeological::getWorkFaceId,query.getWorkFaceId())
.orderByDesc(TransparentGeological::getCreateTime)
.page(Condition.getPage(query));
IPage<TransparentGeologicalVO> result= PageUtil.copy(page,TransparentGeologicalVO.class);
result.getRecords().forEach(e->e.setWorkFaceName(WorkFaceCache.getWorkFaceName(e.getWorkFaceId())));
return result;
}
pom文件引入mybatis-pius依赖
<!-- mybatis-plus -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.2</version>
</dependency>
分页转换工具如下
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springblade.core.tool.utils.BeanUtil;
import java.util.List;
public class PageUtil {
public static <T, E> IPage<T> copy(IPage page, List<E> sourceList, Class<T> targetClazz) {
IPage pageResult = new Page(page.getCurrent(),page.getSize(),page.getTotal());
pageResult.setPages(page.getPages());
List<T> records = BeanUtil.copy(sourceList, targetClazz);
pageResult.setRecords(records);
return pageResult;
}
public static <T, E> IPage<T> copy(IPage page, Class<T> targetClazz) {
return copy(page,page.getRecords(),targetClazz);
}
补充:MYBATIS PLUS 分页工具类
import com.baomidou.mybatisplus.plugins.Page;
import com.sun.collierycommon.domain.PageParamDTO;
import com.sun.collierycommon.domain.PageResultDTO;
import java.util.List;
public class SmartPaginationUtil {
public static <T> PageResultDTO<T> convert2PageInfoDTO(Page<T> page) {
PageResultDTO<T> result = new PageResultDTO<>();
result.setPageNum(page.getCurrent());
result.setPageSize(page.getSize());
result.setTotal(Long.valueOf(page.getTotal()));
result.setPages(page.getPages());
result.setList(page.getRecords());
return result;
}
public static <T> Page<T> convert2PageQueryInfo(PageParamDTO baseDTO) {
Page<T> page = new Page<>();
Boolean sort = baseDTO.getSort();
if (null != sort) {
page.setAsc(sort);
}
if (null != baseDTO.getSortField()){
page.setOrderByField(baseDTO.getSortField());
page.setCurrent(baseDTO.getPageNum());
page.setSize(baseDTO.getPageSize());
if (null != baseDTO.getSearchCount()) {
page.setSearchCount(baseDTO.getSearchCount());
return page;
public static <T, E> PageResultDTO<T> convert2PageInfoDTO(Page page, List<E> sourceList, Class<T> targetClazz) {
PageResultDTO pageResultDTO = setPage(page);
List<T> records = SmartBeanUtil.copyList(sourceList, targetClazz);
page.setRecords(records);
pageResultDTO.setList(records);
return pageResultDTO;
* 转换为 PageResultDTO 对象
public static <T, E> PageResultDTO<T> convert2PageInfoDTO(Page page, List<E> sourceList) {
page.setRecords(sourceList);
pageResultDTO.setList(sourceList);
private static PageResultDTO setPage(Page page) {
PageResultDTO pageResultDTO = new PageResultDTO();
pageResultDTO.setPageNum(page.getCurrent());
pageResultDTO.setPageSize(page.getSize());
pageResultDTO.setTotal(Long.valueOf(page.getTotal()));
pageResultDTO.setPages(page.getPages());
}
serve层中分页方法的使用
public ResponseDTO<PageResultDTO<ProductionDigProgressListVO>> list(ProductionDigProgressParamDTO param,SystemUserVo userVo){
Page page= SmartPaginationUtil.convert2PageQueryInfo(param);
EntityWrapper<ProductionDigProgressEntity> wrapper= new EntityWrapper<>();
wrapper.eq("deleted", DeletedEnum.NO_DELETED.getValue());
wrapper.eq("company_id", userVo.getCompanyId());
wrapper.like(StringUtils.isNotBlank(param.getName()),"name",param.getName());
wrapper.like(StringUtils.isNotBlank(param.getTeamName()),"team_name",param.getTeamName());
List<ProductionDigProgressListVO> pageList= Lists.newArrayList();
List<ProductionDigProgressEntity> list= productionDigProgressDao.selectPage(page,wrapper);
if(CollectionUtils.isNotEmpty(list)){
pageList=SmartBeanUtil.copyList(list,ProductionDigProgressListVO.class);
}
return ResponseDTO.succData(SmartPaginationUtil.convert2PageInfoDTO(page,pageList));
}
到此这篇关于mybatis-plus 分页类型转换工具类的文章就介绍到这了,更多相关mybatis-plus 分页工具类内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!