Mybatis一對(duì)多和多對(duì)一處理的深入講解
建表
SQL:
create table teacher( id int not null, name varchar(30) default null, primary key (id) ); insert into teacher (id, name) values (1, '蔡老師'); create table student( id int not null , name varchar(30) default null, tid int default null, constraint fk_tid foreign key (tid) references teacher(id) ); insert into student(id, name, tid) VALUES (1, '小名', 1); insert into student(id, name, tid) VALUES (2, '小紅', 1); insert into student(id, name, tid) VALUES (3, '小亮', 1); insert into student(id, name, tid) VALUES (4, '小蘭', 1); insert into student(id, name, tid) VALUES (5, '笑笑', 1);
多對(duì)一處理
- 多個(gè)學(xué)生對(duì)應(yīng)一個(gè)老師
- 對(duì)于學(xué)生這邊而言,關(guān)聯(lián)。即多個(gè)學(xué)生關(guān)聯(lián)一個(gè)老師【多對(duì)一】
- 對(duì)于老師這邊而言,集合。即一個(gè)老師有很多的學(xué)生【一對(duì)多】
mapper
//查詢所有的學(xué)生信息以及對(duì)應(yīng)的老師的信息 List<Student> queryStudentAndTeacher();
實(shí)體類
@Data @AllArgsConstructor @NoArgsConstructor public class Student { private int id; private String name; //學(xué)生需要關(guān)聯(lián)一個(gè)老師 private Teacher teacher; }
@Data @AllArgsConstructor @NoArgsConstructor public class Teacher { private int id; private String name; }
按照查詢嵌套處理
<!--思路: 1.查詢所有的學(xué)生 2.根據(jù)查詢出來的學(xué)生的tid尋找對(duì)應(yīng)的老師 尋找對(duì)應(yīng)的老師,子查詢 --> <resultMap id="rStuAndTea" type="student"> <result property="id" column="id"/> <result property="name" column="name"/> <!-- 復(fù)雜的屬性我們需要單獨(dú)處理 指定屬性的類型 對(duì)象使用association javaType 集合使用collection ofType --> <association property="teacher" column="tid" javaType="Teacher" select="getTeacher"/> </resultMap> <select id="queryStudentAndTeacher" resultMap="rStuAndTea"> select * from student </select> <select id="getTeacher" resultType="teacher"> select * from teacher where id = #{id} </select>
按照結(jié)果嵌套處理
<!--方式二 按照結(jié)果嵌套處理--> <resultMap id="rStuAndTea2" type="student"> <result property="id" column="sid"/> <result property="name" column="sname"/> <association property="teacher" javaType="Teacher"> <result property="name" column="tname"/> </association> </resultMap> <select id="queryStudentAndTeacher2" resultMap="rStuAndTea2"> select s.id sid, s.name sname, t.name tname from student s, teacher t where s.tid = t.id </select>
回顧Mysql多對(duì)一查詢方式
- 子查詢
- 聯(lián)表查詢
一對(duì)多處理
- 一個(gè)老師有多個(gè)學(xué)生
- 對(duì)于老師這邊而言,集合。即一個(gè)老師有很多的學(xué)生【一對(duì)多】
mapper
//查詢指定老師的信息及其所有的學(xué)生 Teacher queryTeaAndStu(@Param("tid") int id);
實(shí)體類
@Data @AllArgsConstructor @NoArgsConstructor public class Teacher { private int id; private String name; //一個(gè)老師擁有多個(gè)學(xué)生 private List<Student> students; }
@Data @AllArgsConstructor @NoArgsConstructor public class Student { private int id; private String name; private int tid; }
按照查詢嵌套處理
<!--按照結(jié)果嵌套查詢--> <resultMap id="rTeaAndStu" type="teacher"> <result property="id" column="tid"/> <result property="name" column="tname"/> <collection property="students" ofType="Student"> <result property="id" column="sid"/> <result property="name" column="sname"/> <result property="tid" column="tid"/> </collection> </resultMap> <select id="queryTeaAndStu" resultMap="rTeaAndStu"> select s.id sid, s.name sname, t.name tname, t.id tid from student s, teacher t where s.tid = t.id and t.id = #{tid} </select>
按照查詢嵌套處理
<!--按照查詢嵌套處理--> <select id="queryTeaAndStu2" resultMap="rTeaAndStu2"> select * from teacher where id = #{tid} </select> <resultMap id="rTeaAndStu2" type="teacher"> <collection property="students" javaType="ArrayList" ofType="Student" select="queryStudentByTeacherId" column="id"/> </resultMap> <select id="queryStudentByTeacherId" resultType="Student"> select * from student where tid = #{tid} </select>
結(jié)果映射
面試高頻點(diǎn)
- MySQL引擎
- InnoDB底層原理
- 索引
- 索引優(yōu)化
小結(jié)
- 關(guān)聯(lián) - association 【多對(duì)一】
- 集合 - collection 【一對(duì)多】
- javaType & ofType
- javaType 用來指定實(shí)體類中屬性的類型
- ofType 用來指定映射到List或者集合中的entity類型,泛型中的約束類型
注意點(diǎn):
- 保證SQL的可讀性,盡量保證通俗易懂
- 注意一對(duì)多和多對(duì)一中屬性名和字段的問題
- 如果問題不好排查錯(cuò)誤,可以使用LOG4J日志
總結(jié)
到此這篇關(guān)于Mybatis一對(duì)多和多對(duì)一處理的文章就介紹到這了,更多相關(guān)Mybatis一對(duì)多和多對(duì)一處理內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring?MVC??接受請(qǐng)求參數(shù)的方法
了解HTTP請(qǐng)求的GET和POST方法中如何攜帶參數(shù),以及SpringMVC中如何接收這些參數(shù),GET方法通過URL傳遞參數(shù),而POST方法通常在請(qǐng)求體中傳遞,SpringMVC使用注解如@RequestParam和@RequestBody來綁定參數(shù)到控制器方法2024-09-09淺談Hibernate中的三種數(shù)據(jù)狀態(tài)(臨時(shí)、持久、游離)
下面小編就為大家?guī)硪黄獪\談Hibernate中的三種數(shù)據(jù)狀態(tài)(臨時(shí)、持久、游離)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-09-09FasfDFS整合Java實(shí)現(xiàn)文件上傳下載功能實(shí)例詳解
這篇文章主要介紹了FasfDFS整合Java實(shí)現(xiàn)文件上傳下載功能實(shí)例詳解,需要的朋友可以參考下2017-08-08java.net.ConnectException異常的正確解決方法(親測(cè)有效!)
java.net.ConnectException異常是與網(wǎng)絡(luò)相關(guān)的最常見的Java異常之一,建立從客戶端應(yīng)用程序到服務(wù)器的TCP連接時(shí),我們可能會(huì)遇到它,這篇文章主要給大家介紹了關(guān)于java.net.ConnectException異常的正確解決方法,需要的朋友可以參考下2024-01-01springboot starter自定義實(shí)現(xiàn)公共模塊方式
這篇文章主要介紹了springboot starter自定義實(shí)現(xiàn)公共模塊方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08