Mybatis的collection三層嵌套查詢方式(驗證通過)
Mybatis collection三層嵌套查詢
在Mybatis中存在很多1對N查詢的場景,比如在打開一個編輯頁面時,需要帶出對應(yīng)的新增時添加的數(shù)據(jù),如果頁面有一些多個層級一對多的情況,那么在編輯時就需要查詢出所有的層級。
當(dāng)然這種情況,先查詢出最外面的情況,再循環(huán)查詢數(shù)據(jù)庫查詢出里面的數(shù)據(jù)也是沒問題的,但是這樣性能會非常差。
最好的方式就是直接在Mybatis中直接做好1對多的映射直接查詢出來。
一般情況下,我們都是兩層的嵌套
類似這樣的:
@Data public class Class { ? ?? ? ? private String classId; ? ? ? private String className; ? ? ? pirvate List<Student> studentList; } ? ? @Data public class Student { ? ?? ? ? private String studentId; ? ? ? private String ?studentName; ? ?? ? ? private String classId; }
這種兩層嵌套的查詢比較好處理,網(wǎng)上的方案也比較多,大家可以自行百度。
但是如果是三層,乃至于多層的嵌套就不太好處理了。
找到一個方案
是否可行,沒有嘗試。
下面是我自己的方案,在項目中親測可行。
實體類的映射關(guān)系
# 一個班級有多個學(xué)生 @Data public class Class { ? ?? ? ? private String classId; ? ? ? private String className; ? ? ? pirvate List<Student> studentList; } ? # 一個學(xué)生有多個愛好 @Data public class Student { ? ?? ? ? private String studentId; ? ? ? private String ?studentName; ? ?? ? ? private String classId; ? ? ? pirvate List<Hobby> hobbyList; } ? # 愛好 @Data public class Hobby { ? ?? ? ? private String hobbyId; ? ? ? private String hobbyName; ? ?? ? ? private String studentId; }
SQL映射關(guān)系如下
其中第一個resultMap是一種分開SQL查詢的處理方式,第二個resultMap是一種單個SQL解決嵌套問題的寫法。這里我把它們?nèi)诤蠟橐粋€了。
如果是只有兩層嵌套的話,基本上一個SQL的寫法就可以搞定,或者說簡潔明了的方式就使用兩個SQL的寫法。
解析如下
classMap:
查詢class的信息,以及對應(yīng)的學(xué)生列表,采用2個SQL的寫法處理,其中select是查詢這個studentList的SQL的id,即queryStudentInfo。
傳遞的column是兩張表關(guān)聯(lián)的字段,也就是說將第一層的班級id傳遞到下一個SQL中作為參數(shù)。
studentMap:
查詢學(xué)生的信息,以及愛好列表,采用單個SQL的查詢方式,直接把愛好的字段直接放在了collection中。
<resultMap id="classMap" type="com.xxx.Class"> ? ? ? ? <id column="class_id" property="classId"/> ? ? ? ? <result column="class_name" property="className"/> ? ? ? ? <collection property="studentList" javaType="java.util.List" ? ? ? ? ? ?? ? ? ? ? ? ? ofType="com.xxx.StudentPO" ? ? ? ? ? ? ? ? ? ? select="queryStudentInfo" column="class_id"> ? ? ? ? </collection> </resultMap> ? <resultMap id="studentMap" type="com.xxx.Student"> ? ? ? ? <id column="student_id" property="studentId"/> ? ? ? ? <result column="student_name" property="studentName"/> ? ? ? ? <collection property="hobbyList" javaType="java.util.List"? ? ? ? ? ? ? ofType="com.xxx.Hobby"> ? ? ? ? ? ? <id column="hobby_id" property="hobbyId"/> ? ? ? ? ? ? <result column="hobby_name" property="hobbyName"/> ? ? ? ? </collection> </resultMap>
SQL如下:
<select id="queryClassInfo" resultMap="classMap"> ? ? ? ? SELECT ? ? ? ? ? ? class_id, ? ? ? ? ? ? class_name ? ? ? ? FROM ? ? ? ? ? ? class ? ? ? ? where ? ? ? ? ? ? class_id = #{classId} </select> ? ? <select id="queryStudentInfo" resultMap="studentMap"> ? ? ? ? SELECT ? ? ? ? ? ? sutdent_id, ? ? ? ? ? ? sutdent_name, ? ? ? ? ? ? hobby_id, ? ? ? ? ? ? hobby_name ? ? ? ? FROM ? ? ? ? ? ? student t1 ? ? ? ? LEFT JOIN ? ? ? ? ? ? hobby t2 ? ? ? ? ON ? ? ? ? ? ? t1.sutdent_id = t2.sutdent_id ? ? ? ? where ? ? ? ? ? ? class_id = #{classId} </select>
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
- mybatis collection關(guān)聯(lián)查詢多個參數(shù)方式
- MyBatis使用嵌套查詢collection和association的實現(xiàn)
- mybatis?resultMap之collection聚集兩種實現(xiàn)方式
- mybatis中association和collection的使用與區(qū)別
- MyBatis的collection和association的使用解讀
- Mybatis中一對多(collection)和一對一(association)的組合查詢使用
- Mybatis使用Collection屬性的示例代碼
- mybatis?collection和association的區(qū)別解析
- MyBatis中<collection>標(biāo)簽的多種用法
相關(guān)文章
SpringBoot中定時任務(wù)@Scheduled注解的使用解讀
這篇文章主要介紹了SpringBoot中定時任務(wù)@Scheduled注解的使用解讀,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-09-09Java中使用BeanMap將對象轉(zhuǎn)為Map詳解
這篇文章主要介紹了Java中使用BeanMap將對象轉(zhuǎn)為Map詳解,BeanMap?是?Apache?Commons?BeanUtils?庫中的一個類,BeanMap?可以將?Java?對象的屬性作為鍵,屬性值作為對應(yīng)的值,存儲在一個?Map?中,它提供了一種將?Java?對象轉(zhuǎn)換為?Map?的方式,需要的朋友可以參考下2024-01-01