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


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

