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了
<!-- 子查詢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 和原來一樣就好,把之前子查詢所關聯的刪除就好




免責聲明!

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



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