当前位置:主页 > 软件编程 > JAVA代码 >

mybatis-plus 分页类型转换工具类

时间:2023-01-27 10:13:33 | 栏目:JAVA代码 | 点击:

前言

 用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 {
    /**
     * @Description:转换为 IPage 对象
     * @Author: tarzan
     * @Date: 2019/10/31 9:40
     */
    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;
/**
 * @Description:分页工具类
 * @Author: tarzan 
 * @Date: 2019/10/31 9:13
 */
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;
    /**
     * @Description:转换为 PageResultDTO 对象
     * @Author: sunboqiang
     * @Date: 2019/10/31 9:40
     */
    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));
	}

您可能感兴趣的文章:

相关文章