mybatis plus 自定义多表联合查询分页

本文介绍了使用MyBatisPlus进行多表联合查询分页的操作,包括通过复制属性实现基础表与DTO转换,以及自定义查询方法解决排序问题。在遇到翻页后查询条件失效的问题后,通过调整方法参数和动态SQL解决了问题,确保了分页查询的正确性。
摘要由CSDN通过智能技术生成

记录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方法,经测试分页查询功能正常了。

第一种方法翻页后为什么不可以条件查询,目前还不太清楚…有大佬知道的可以教教小弟,拜谢!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值