pageHelper 插件一對多查詢分頁問題

1.首先先確定我們需要返回的數據數據結構,這裏我的貼出實體類(set/get方法自己生成)

public class BillInfoAndStudentInfoBean {
    private String id;
    private String billId;
    private BigDecimal moneyTotal;
    private List<ItemsBean> items;
}

2.然後我們在mapper.xml建立對應的關係,要實現分頁正確我們需要建立兩個 resultMap,一個用於子查詢用
注意,在主查詢中的 collection裏面要配置子查詢sql查詢id的方法名要對應,裏面的 column 就是子查詢你需要的查詢條件如果子查詢需要多個條件column就這樣寫 column={id = billId,money = moneyTotal}
然後去子查詢裏面取值就好

主查詢 resultMap
<resultMap id="BillInfoAndItemsInfo" type="com.lxd.domain.param.BillInfoAndStudentInfoBean">
    <result column="id" property="id"></result>
    <result column="billId" property="billId"></result>
    <result column="moneyTotal" property="moneyTotal"></result>
    <collection property="items"  javaType="java.util.List" ofType="com.lxd.domain.param.ItemsBean" select="queryItemInfoById" column="billId">
    </collection>
</resultMap>
子查詢 resultMap    
<resultMap id="ItemBeans" type="com.lxd.domain.param.ItemsBean">
    <result column="item_name" property="item_name"></result>
    <result column="item_price" property="item_price"></result>
    <result column="is_sure" property="item_mandatory"></result>
</resultMap>

3.接下來可以開始寫sql了

,注意這裏的 方法名要和主查詢 resultMap collection 裏面 select 的方法名對應起來,這裏傳入的參數就是, collection 裏面的 column

<單個參數>

<select id="queryItemInfoById" resultMap="ItemBeans">
    SELECT     ati.item_name,ati.item_price,ati.is_sure, atd.id FROM app_tuition_data atd
    left JOIN app_tuition_item ati on atd.id=ati.tuition_id WHERE atd.id = #{billId}
</select>

<多個參數>

<select id="queryItemInfoById" resultMap="ItemBeans">
    SELECT     ati.item_name,ati.item_price,ati.is_sure, atd.id FROM app_tuition_data atd
    left JOIN app_tuition_item ati on atd.id=ati.tuition_id WHERE atd.id = #{id} and #{money}
</select>

4.這樣就好了,主查詢sql 和原來一樣就好,把之前子查詢所關聯的刪除就好

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章