mybatis-plus实现自定义分页

概述

本文主要介绍如何使用mybatis-plus实现复杂自定义分页

使用说明

接口定义

/**
   * 分页查询
   * // 此处最好返回IPAGE
   */
  APIResult<IPage<ResDTO>> getProjectList(QueryDTO QueryDTO);

实现类

 @Override
  public APIResult<IPage<ResDTO>> getProjectList(QueryDTO QueryDTO) {
    // 定义返回类IPAGE
    IPage<ResDTO> page = new Page<ResDTO>(QueryDTO.getCurrentPage(), QueryDTO.getPageSize());
    // 分页查询
    IPage<ResDTO> pageResult = projectMapper.queryProjectList(page, QueryDTO);
    // 对分页数据遍历进行自定义操作
    pageResult.getRecords().stream().forEach(item -> {
      item.setRiskLevel("高");
    });
    return APIResult.success(page);
  }

mapper.java

 /**
   * 查询分页数据
   * @param projectListQueryDTO
   * @return
   */
  IPage<ProjectListSingleResDTO> queryProjectList(IPage<ProjectListSingleResDTO> page, @Param("dto") ProjectListQueryDTO projectListQueryDTO); 

mapper文件

<select id="queryList" parameterType="com.*.QueryDTO" resultType="com.*.ResDTO">
        select  
        id as id,
        sub_project_name as subProjectName,
        project_no as projectNo,
        project_category as projectCategory,
        current_bid_status as currentBidStatus,
        project_type as projectType,
        project_status as projectStatus,
        remark as riskLevel,
        create_time as createTime 
        from project
        <choose>
            <when test="dto.projectStatus == 'ARCHIVED'">
               where project_status = #{dto.projectStatus} and
            </when>
            <otherwise>
               where project_status != #{dto.projectStatus} and
            </otherwise>
        </choose>
        <if test="dto.currentBidStatus != null and dto.currentBidStatus != ''">
            current_bid_status = #{dto.currentBidStatus} and 
        </if>
        (create_user = #{dto.userId} or update_user = #{dto.userId})
        <if test="dto.searchKey != null and dto.searchKey != ''">
            and (project_no like concat('%',#{dto.searchKey},'%') or sub_project_name
            like
            concat('%',#{dto.searchKey},'%'))
        </if>
        order by create_time desc
    </select>

总结

mybatis-plus方便了常用的分页查询,但是在复杂的分页情况下常常误用导致分页失败。
需要注意的是,PAGE参数最好放DTO前面,MAP也是可以支持的。
返回参数直接使用IPAGE。


   Reprint policy


《mybatis-plus实现自定义分页》 by jackromer is licensed under a Creative Commons Attribution 4.0 International License
 Previous
常用问题整理 常用问题整理
概述 近期关于开发过程中常见问题整理。 consul 命令 查看consul健康状态 curl http://localhost:8500/v1/agent/health/service/name/safety-api 删除无用con
2020-06-08 jackromer
Next 
spring boot使用junit Test spring boot使用junit Test
概述 本文主要介绍如何在spring boot项目中使用junit测试模块功能 方法一/使用spring-boot自带的junit测试功能 引入maven依赖 <!-- Test start-->
2020-03-19 jackromer
  目录