解決mybatis plus 一對多分頁查詢問題


換了很多姿勢使用mybatis-plus分頁還是有問題

問題

比如: 訂單表跟訂單信息表是一對多關系,分頁每頁查詢出來的數據是以訂單商品表為主的,萬一 一個訂單有十個商品的話,那就只能查詢一條單了,這明顯就存在問題,

我需要的是每頁的條數是以訂單表為主的,如果你遇到跟我同樣的問題請往下看......

 

mapper層

 /** * 分頁獲取訂單數據 * * @param orderPara 訂單傳參數類 * @return
     */
    public IPage<Orders> selectOrdersList(@Param("page") Page<Orders> page, @Param(Constants.WRAPPER) Wrapper<Orders> wrapper, @Param("orderPara") OrderParaVo orderPara);

 

 /** * 獲取訂單商品信息數據 * * @param orderNo 訂單號 * @return 訂單商品數據 */
    public List<OrderItem> selectOrderItemList(String orderNo);

 

xml

使用collection標簽的select屬性來映射

<resultMap id="BaseResultMap" type="xxx.xxx.xxx.Orders">
...
這里訂單表字段忽略
<collection property="orderItemsList" ofType="xxx.xxx.xxx.OrderItem" select="selectOrderItemList" column="order_no"/>
</resultMap>

property:  對應映射的訂單商品類(private List<OrderItem> orderItemsList;)

ofType: 訂單商品類的包名路徑

select: 查詢訂單商品的名稱

column: 兩表關聯的條件字段(數據庫的)

 

sql

 <!--分頁獲取訂單數據-->
    <select id="selectOrdersList" resultMap="BaseResultMap" parameterType="xxx.xxx.xxx.vo.OrderParaVo"> select <include refid="Orders_Base_Column_List"/> from orders <where>
            <if test="orderPara.memberId != null"> orders.member_id = #{orderPara.memberId} </if>
        </where> order by orders.create_time desc ${ew.customSqlSegment} </select>

注意: orderPara是傳值Vo類在mapper層訂單的別名,這樣使用實體傳值的話能傳多個參數進來。

 

<!--獲取訂單商品信息數據-->
    <select id="selectOrderItemList" resultType="xxx.xxx.xxx.OrderItem"> select <include refid="OrderItem_Base_Column_List"/> from order_item <where>
            <if test="orderNo != null"> order_item.order_no = #{orderNo} </if>
        </where>
    </select>

 

controller

這里直接調用mapper層了,需要規范的話,在service層寫個接口調用mapper,然后controller再調用業務層,所有的業務層代碼寫在service實現層就好了......

@GetMapping("listOrdersList") public AjaxResult test(@RequestBody OrderParaVo orderParaVo) { QueryWrapper<Orders> queryWrapper = new QueryWrapper<>(); Page<Orders> page = new Page<Orders>(orderParaVo.getPageNum(), orderParaVo.getPageSize()); IPage<Orders> userPageList = ordersMapper.selectOrdersList( page, queryWrapper,orderParaVo); return AjaxResult.success(userPageList); }

 

如有其它辦法能解決這個問題的話請下方留言哦,非常感謝!

 

 如遇到問題進qq群討論:837146509


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM