mybatisplus 一对多(collection)分页解决方案

mybatisplus 一对多(collection)分页解决方案

简单以订单、商品来举例说明,即一个订单包含多个商品

背景1:查询订单表的内容,商品表不参与条件查询,用懒加载形式
<resultMap id="OrderInfoListResultMap" type="*******OrderInfoVo">
        <id column="id" property="id"/>
        <result column="order_name" property="orderName"/>
        <result column="order_no" property="orderNo"/>      
        ...........省略若干
        <collection property="goodsList" javaType="ArrayList" ofType="****OrderGoodsVo"
             select="getOrderGoodsVoByOrderId" column="{orderId = id}"/>
</resultMap>
 <select id="findBuyerOrderListByPage" resultMap="OrderInfoListResultMap">
 	SELECT *FROM `order_info`
</select>
<select id="getOrderGoodsVoByOrderId" resultType="*******OrderGoodsVo">
        SELECT * FROM `order_goods` WHERE `order_id` = #{orderId}
</select>
背景2:查询订单表的内容,商品表要作为查询条件参与查询,例如要根据商品名称查询出订单信息
<resultMap id="OrderInfoListResultMap" type="*******OrderInfoVo">
        <id column="id" property="id"/>
        <result column="order_name" property="orderName"/>
        <result column="order_no" property="orderNo"/>      
        ...........省略若干
        <collection property="goodsList" javaType="ArrayList" ofType="****OrderGoodsVo"
             select="getOrderGoodsVoByOrderId" column="{orderId = id,goodsName = goodsName}"/>
</resultMap>
 <select id="findBuyerOrderListByPage" resultMap="OrderInfoListResultMap">
 	  select temp.* from (
            SELECT distinct (og.order_id) as maskkey,
            oi.*,#{orderInfo.goodsName} as goodsName
            FROM
            `order_info` oi LEFT JOIN `order_goods` og ON oi.`id` = og.`order_id`
             <where>
	            <if test="orderInfo.goodsName != null and orderInfo.goodsName != ''">and og.goods_name like concat('%',#{orderInfo.goodsName},'%')
	            </if>
            </where>
            ) temp
</select>
<select id="getOrderGoodsVoByOrderId" resultType="*******OrderGoodsVo">
        SELECT * FROM `order_goods` WHERE `order_id` = #{orderId}
         <if test="goodsName != null and goodsName != ''">
            and goods_name like concat('%',#{goodsName},'%')
        </if>
</select>

重点:

  1. 将goodsName传递到子查询中
  2. 借用一对多的特性,利用左查询联合筛选订单信息,保证分页条数的准确性
  • 8
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
MybatisPlus 的 collection 一对多查询可以使用子查询或者关联查询两种方式实现,其中关联查询方式最常用。对于一对多关联查询的分页问题,可以按照以下步骤实现: 1. 定义一个包含分页参数的 DTO 类,例如: ```java public class MyDto { private Integer pageNum; private Integer pageSize; // 其他查询条件 // ... } ``` 2. 在 Mapper 接口中定义一个方法,例如: ```java List<MyEntity> selectMyEntityList(@Param("dto") MyDto dto); ``` 3. 在 Mapper.xml 文件中编写关联查询 SQL,例如: ```xml <select id="selectMyEntityList" resultMap="myEntityResultMap"> select me.*, mc.* from my_entity me left join my_child mc on me.id = mc.entity_id <where> <!-- 根据查询条件动态拼接 SQL --> <if test="dto.xxx != null"> and me.xxx = #{dto.xxx} </if> <!-- 其他查询条件 --> <!-- ... --> </where> order by me.create_time desc </select> ``` 4. 在 Service 中调用 Mapper 方法并进行分页处理,例如: ```java public Page<MyEntity> selectMyEntityPage(MyDto dto) { // 设置分页参数 Page<MyEntity> page = new Page<>(dto.getPageNum(), dto.getPageSize()); // 执行 MybatisPlus 的分页查询 IPage<MyEntity> iPage = myMapper.selectMyEntityList(page, dto); // 将 IPage 转换为 Page 并返回 return new Page<>(iPage.getCurrent(), iPage.getSize(), iPage.getTotal()).setRecords(iPage.getRecords()); } ``` 5. 在 Controller 中接收前端传递的分页参数并调用 Service 方法,例如: ```java @GetMapping("/myEntities") public Result<List<MyEntity>> getMyEntityList(MyDto dto) { Page<MyEntity> page = myService.selectMyEntityPage(dto); return Result.success(page.getRecords()).setPageInfo(page); } ``` 以上就是使用 MybatisPlus 实现一对多关联查询的分页处理的基本步骤。需要注意的是,如果是使用子查询方式实现一对多关联查询,则需要将子查询的结果集转换为 List 并进行分页处理。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值