Mybatis關聯映射舉例詳解
一、關聯映射
舉例關系說明
數據庫創(chuàng)建表,student,teacher
關系說明:
- 一個老師可以有多個學生
- 一個學生只有一個老師
- 一個老師對學生:一對多的關系
- 一個學生老師:一對一的關系
二、一對一多對一的關系
查詢學生信息及其對應的教師信息
學生實體:用對象來存儲教師信息,因為一個學生對應一個教師對象
public class Student { private Integer id; private String Sname; private String sex; private Integer age; private Integer t_id; //這個是重點 private Teacher teacher; }
教師實體:
public class Teacher { private Integer id; private String Tname; }
1.第一種形式-連表查詢
數據庫查詢sql:
SELECT student.id,student.name,teacher.name FROM student LEFT JOIN teacher on student.t_id = teacher.id
mybatis多表聯查查詢語句:(嵌套其他實體對象)
對于特殊數據:
- 如果是對象:用association :< association property=“teacher” javaType=“com.qcby.entity.Teacher”>,特殊數據特殊處理
- < result property=“id” column=“id”/> :所要查詢的字段,property代表java中實體的屬性名稱,column:表示數據庫的字段
<!-- 按照結果嵌套處理--> <select id="getStudent1" resultMap="StudentTeacher1"> SELECT student.id,student.Sname,teacher.Tname FROM student LEFT JOIN teacher on student.t_id = teacher.id </select> <resultMap id="StudentTeacher1" type="com.qcby.entity.Student"> <result property="id" column="id"/> <result property="Sname" column="Sname"/> <result property="sex" column="sex"/> <result property="age" column="age"/> <result property="t_id" column="t_id"/> <!-- 復雜的屬性我們需要單獨去處理 對象:association 集合:collection --> <!-- property="teacher" student類當中的關聯字段 --> <!-- javaType="com.javen.model.Teacher" 為復雜屬性設置類類型--> <association property="teacher" javaType="com.qcby.entity.Teacher"> <result property="id" column="id"/> <result property="Tname" column="Tname"/> </association> </resultMap>
2.第二種形式-分步查詢
數據庫查詢sql:
SELECT s.id,s.Sname,t.Tname FROM student s,teacher t where s.t_id = t.id
mybatis分布查詢查詢語句:
<select id = "getStudent" resultMap="StudentTeacher"> select * from student; </select> <!--結果映射集--> <resultMap id="StudentTeacher" type="com.qcby.entity.Student"> <result property="id" column="id"/> <result property="Sname" column="Sname"/> <result property="sex" column="sex"/> <result property="age" column="age"/> <result property="t_id" column="t_id"/> <!-- select="getTeacher" :調用下一個查詢語句 --> <!-- column="t_id" 兩個表的關聯字段--> <association property="teacher" column="t_id" javaType="com.qcby.entity.Teacher" select="getTeacher"/> </resultMap> <select id="getTeacher" resultType="com.qcby.entity.Teacher"> select * from teacher where id = #{t_id}; <!-- #{id}; 可以寫任何東西,因為會自動匹配 t_id --> </select>
三、一對多
查詢教師對應的學生信息
設立教師實體:用集合來存儲對應的學生信息,因為一個教師對應多個學生
public class Teacher { private Integer id; private String Tname; //這個一定要有 private List<Student> students; }
第一種形式按照結果嵌套處理
mybatis查詢語句:
<!--按照結果進行查詢--> <select id="getTeacher" resultMap="TeacherStudent"> SELECT teacher.id,teacher.Tname,student.Sname FROM teacher LEFT JOIN student on student.t_id = teacher.id </select> <resultMap id="TeacherStudent" type="com.qcby.entity.Teacher"> <result property="id" column="id"/> <result property="Tname" column="Tname"/> <!-- 復雜的屬性我么需要單獨去處理 對象:association 集合:collection 在集合中的泛型信息,我們使用ofType獲取 --> <collection property="students" ofType="com.qcby.entity.Student"> <!-- 查詢什么寫什么 --> <result property="Sname" column="Sname"/> </collection> </resultMap>
第二種形式按照查詢嵌套處理
mybatis查詢語句: 對于特殊字段集合采用分布查詢的方式,特殊字段特殊處理:< collection property=“students” column=“t_id”
ofType=“com.qcby.entity.Student” select=“getStudentByTeacherId” />,getStudentByTeacherId一個新的查詢語句
<!--按照查詢嵌套處理:分布查詢--> <select id="getTeacher" resultMap="TeacherStudent2"> select * from teacher </select> <resultMap id="TeacherStudent2" type="com.qcby.entity.Teacher"> <!--column="t_id" 傳值--> <collection property="students" column="t_id" ofType="com.qcby.entity.Student" select="getStudentByTeacherId" /> <!--實現分布查詢--> </resultMap> <select id="getStudentByTeacherId" resultType="com.qcby.entity.Student"> select * from student where id = #{t_id} </select
到此這篇關于Mybatis關聯映射舉例詳解的文章就介紹到這了,更多相關Mybatis關聯映射內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Springboot如何設置過濾器及重復讀取request里的body
這篇文章主要介紹了Springboot如何設置過濾器及重復讀取request里的body,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03使用IDEA異常斷點來定位java.lang.ArrayStoreException的問題
這篇文章主要介紹了使用IDEA異常斷點來定位java.lang.ArrayStoreException的問題,平常開發(fā)過程中面對這種描述不夠清楚,無法定位具體原因的問題該如何處理,下面我們來一起學習一下吧2019-06-06