Mybatis關(guān)聯(lián)映射舉例詳解
一、關(guān)聯(lián)映射
舉例關(guān)系說明
數(shù)據(jù)庫創(chuàng)建表,student,teacher


關(guān)系說明:
- 一個老師可以有多個學(xué)生
- 一個學(xué)生只有一個老師
- 一個老師對學(xué)生:一對多的關(guān)系
- 一個學(xué)生老師:一對一的關(guān)系
二、一對一多對一的關(guān)系
查詢學(xué)生信息及其對應(yīng)的教師信息
學(xué)生實體:用對象來存儲教師信息,因為一個學(xué)生對應(yīng)一個教師對象
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.第一種形式-連表查詢
數(shù)據(jù)庫查詢sql:
SELECT student.id,student.name,teacher.name FROM student LEFT JOIN teacher on student.t_id = teacher.id
mybatis多表聯(lián)查查詢語句:(嵌套其他實體對象)
對于特殊數(shù)據(jù):
- 如果是對象:用association :< association property=“teacher” javaType=“com.qcby.entity.Teacher”>,特殊數(shù)據(jù)特殊處理
- < result property=“id” column=“id”/> :所要查詢的字段,property代表java中實體的屬性名稱,column:表示數(shù)據(jù)庫的字段
<!-- 按照結(jié)果嵌套處理-->
<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"/>
<!-- 復(fù)雜的屬性我們需要單獨去處理 對象:association 集合:collection -->
<!-- property="teacher" student類當(dāng)中的關(guān)聯(lián)字段 -->
<!-- javaType="com.javen.model.Teacher" 為復(fù)雜屬性設(shè)置類類型-->
<association property="teacher" javaType="com.qcby.entity.Teacher">
<result property="id" column="id"/>
<result property="Tname" column="Tname"/>
</association>
</resultMap>
2.第二種形式-分步查詢
數(shù)據(jù)庫查詢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>
<!--結(jié)果映射集-->
<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" :調(diào)用下一個查詢語句 -->
<!-- column="t_id" 兩個表的關(guān)聯(lián)字段-->
<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>三、一對多
查詢教師對應(yīng)的學(xué)生信息
設(shè)立教師實體:用集合來存儲對應(yīng)的學(xué)生信息,因為一個教師對應(yīng)多個學(xué)生
public class Teacher {
private Integer id;
private String Tname;
//這個一定要有
private List<Student> students;
}
第一種形式按照結(jié)果嵌套處理
mybatis查詢語句:
<!--按照結(jié)果進行查詢-->
<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"/>
<!-- 復(fù)雜的屬性我么需要單獨去處理 對象: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" /> <!--實現(xiàn)分布查詢-->
</resultMap>
<select id="getStudentByTeacherId" resultType="com.qcby.entity.Student">
select * from student where id = #{t_id}
</select到此這篇關(guān)于Mybatis關(guān)聯(lián)映射舉例詳解的文章就介紹到這了,更多相關(guān)Mybatis關(guān)聯(lián)映射內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Springboot如何設(shè)置過濾器及重復(fù)讀取request里的body
這篇文章主要介紹了Springboot如何設(shè)置過濾器及重復(fù)讀取request里的body,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03
使用IDEA異常斷點來定位java.lang.ArrayStoreException的問題
這篇文章主要介紹了使用IDEA異常斷點來定位java.lang.ArrayStoreException的問題,平常開發(fā)過程中面對這種描述不夠清楚,無法定位具體原因的問題該如何處理,下面我們來一起學(xué)習(xí)一下吧2019-06-06
Mybatis批量插入Oracle數(shù)據(jù)的方法實例
在開發(fā)中或多或少都會遇到數(shù)據(jù)批量插入的功能,最近我在做項目的過程中就遇到了這樣一個問題,下面這篇文章主要給大家介紹了關(guān)于Mybatis批量插入Oracle數(shù)據(jù)的相關(guān)資料,需要的朋友可以參考下2022-01-01

