概述
本文主要介绍如何使用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。