Mybatis的collection三層嵌套查詢方式(驗證通過)
Mybatis collection三層嵌套查詢
在Mybatis中存在很多1對N查詢的場景,比如在打開一個編輯頁面時,需要帶出對應的新增時添加的數據,如果頁面有一些多個層級一對多的情況,那么在編輯時就需要查詢出所有的層級。
當然這種情況,先查詢出最外面的情況,再循環(huán)查詢數據庫查詢出里面的數據也是沒問題的,但是這樣性能會非常差。
最好的方式就是直接在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;
}這種兩層嵌套的查詢比較好處理,網上的方案也比較多,大家可以自行百度。
但是如果是三層,乃至于多層的嵌套就不太好處理了。
找到一個方案
是否可行,沒有嘗試。
下面是我自己的方案,在項目中親測可行。
實體類的映射關系
# 一個班級有多個學生
@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;
?
? ? pirvate List<Hobby> hobbyList;
}
?
# 愛好
@Data
public class Hobby {
? ??
? ? private String hobbyId;
?
? ? private String hobbyName;
? ??
? ? private String studentId;
}SQL映射關系如下
其中第一個resultMap是一種分開SQL查詢的處理方式,第二個resultMap是一種單個SQL解決嵌套問題的寫法。這里我把它們融合為一個了。
如果是只有兩層嵌套的話,基本上一個SQL的寫法就可以搞定,或者說簡潔明了的方式就使用兩個SQL的寫法。
解析如下
classMap:
查詢class的信息,以及對應的學生列表,采用2個SQL的寫法處理,其中select是查詢這個studentList的SQL的id,即queryStudentInfo。
傳遞的column是兩張表關聯的字段,也就是說將第一層的班級id傳遞到下一個SQL中作為參數。
studentMap:
查詢學生的信息,以及愛好列表,采用單個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>總結
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
- mybatis collection關聯查詢多個參數方式
- MyBatis使用嵌套查詢collection和association的實現
- mybatis?resultMap之collection聚集兩種實現方式
- mybatis中association和collection的使用與區(qū)別
- MyBatis的collection和association的使用解讀
- Mybatis中一對多(collection)和一對一(association)的組合查詢使用
- Mybatis使用Collection屬性的示例代碼
- mybatis?collection和association的區(qū)別解析
- MyBatis中<collection>標簽的多種用法
相關文章
SpringBoot中定時任務@Scheduled注解的使用解讀
這篇文章主要介紹了SpringBoot中定時任務@Scheduled注解的使用解讀,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-09-09

