记录mybatisplus多表联合查询分页操作
某个页面表格字段需多表查询,一开始基于基础表分页查询,然后records里面set其他表查询出来的字段
IPage<Do> page = this.page(iPage, qw);
Page<DTO> pageRecords = new Page<>();
BeanUtils.copyProperties(page, pageRecords);
if (!CollectionUtils.isEmpty(page.getRecords())){
List<DTO> DTOList = Convert.do2DtoList(page.getRecords());
DTOList.forEach(issueManagerDTO -> {
DTO.setProjectName(DoMap.get(DTO.getProjectId()).getProjectName());
});
pageRecords.setRecords(issueManagerDTOList);
}
return pageRecords;
项目其他大佬配置了基于ipage的工具类,前端根据工具类传参Order排序,排序参数DO中没有时就会报错,因此参考selectPage自定义多表联合查询分页:
mapper:
/**
* 自定义多表联合查询分页
* @param managerQw
* @param iPage
* @return
*/
Page<DTO> selectManagePage(IPage<DTO> iPage,@Param("ew") QueryWrapper<DTO> managerQw);
service
QueryWrapper<> managerQw = new QueryWrapper<>();
ManagerQuery query = queryInfo.getQueryInfo();
managerQw.eq(ObjectUtils.isNotNull(),"", );
IPage<> iPage = PageUtil.page();
if (CollectionUtils.isEmpty(iPage.orders())){
managerQw.orderByDesc("update_time");
}
Page<> managerDTOPage = this.baseMapper.selectManagePage(iPage,managerQw);
需传page参数和querywrapper。PageUtil.page()即项目大佬配置的工具类。自行new page(pageNum,pageSize);
xml:
<!-- 通用查询映射结果 -->
<resultMap id="BaseResultMap" type="">
<result column="id" property="id" />
<result column="deleted_status" property="deletedStatus" />
<result column="creator" property="creator" />
<result column="creator_id" property="creatorId" />
<result column="create_time" property="createTime" />
<result column="editor" property="editor" />
<result column="editor_id" property="editorId" />
<result column="update_time" property="updateTime" />
<result column="remark" property="remark" />
</resultMap>
<resultMap id="" type="DTO" extends="BaseResultMap">
<result column="project_name" property="projectName" />
<result column="" property="" />
<result column="" property="" />
</resultMap>
<! 这里是多表联合查询其他表的字段>
<select id="selectManagePage" resultMap="DTO">
</select>
<!--分页查询方法-->
BaseResultMap 映射Do实体, ManagerDTO映射DTIO实体。
更新:此种方法分页第一页查询没有问题,但测试测试后发现,若翻页后再查询筛选,查询参数无效…
基于此,之前的方法是仿照selectPage()直接传 QueryWrapper<>,将之改为传query查询参数进入:
Page<> selectManagePage(IPage<> iPage, @Param("query") ManagerQuery query, @Param("order") String order);
xml中用动态拼接语句拼接query中的查询参数,返回类型等不变
<select id="selectManagePage" resultMap="ManagerDTO">
<if test="query.Id != null and query.Id != '' ">
and id= #{query.Id}
</if>
<if test="order != null and order != '' ">
order by ${order} desc
</if>
</select>
这样就相当于只用了page方法,经测试分页查询功能正常了。
第一种方法翻页后为什么不可以条件查询,目前还不太清楚…有大佬知道的可以教教小弟,拜谢!
版权声明:本文为weixin_43707988原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。