建表
SQL:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
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); |
多對一處理
- 多個學生對應一個老師
- 對于學生這邊而言,關聯。即多個學生關聯一個老師【多對一】
- 對于老師這邊而言,集合。即一個老師有很多的學生【一對多】
mapper
1
2
|
//查詢所有的學生信息以及對應的老師的信息 List<Student> queryStudentAndTeacher(); |
實體類
1
2
3
4
5
6
7
8
9
10
|
@Data @AllArgsConstructor @NoArgsConstructor public class Student { private int id; private String name; //學生需要關聯一個老師 private Teacher teacher; } |
1
2
3
4
5
6
7
|
@Data @AllArgsConstructor @NoArgsConstructor public class Teacher { private int id; private String name; } |
按照查詢嵌套處理
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
<!--思路: 1 .查詢所有的學生 2 .根據查詢出來的學生的tid尋找對應的老師 尋找對應的老師,子查詢 --> <resultMap id= "rStuAndTea" type= "student" > <result property= "id" column= "id" /> <result property= "name" column= "name" /> <!-- 復雜的屬性我們需要單獨處理 指定屬性的類型 對象使用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> |
按照結果嵌套處理
1
2
3
4
5
6
7
8
9
10
11
12
13
|
<!--方式二 按照結果嵌套處理--> <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多對一查詢方式
- 子查詢
- 聯表查詢
一對多處理
- 一個老師有多個學生
- 對于老師這邊而言,集合。即一個老師有很多的學生【一對多】
mapper
1
2
|
//查詢指定老師的信息及其所有的學生 Teacher queryTeaAndStu( @Param ( "tid" ) int id); |
實體類
1
2
3
4
5
6
7
8
9
10
|
@Data @AllArgsConstructor @NoArgsConstructor public class Teacher { private int id; private String name; //一個老師擁有多個學生 private List<Student> students; } |
1
2
3
4
5
6
7
8
|
@Data @AllArgsConstructor @NoArgsConstructor public class Student { private int id; private String name; private int tid; } |
按照查詢嵌套處理
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
<!--按照結果嵌套查詢--> <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> |
按照查詢嵌套處理
1
2
3
4
5
6
7
8
9
10
11
|
<!--按照查詢嵌套處理--> <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> |
結果映射
面試高頻點
- MySQL引擎
- InnoDB底層原理
- 索引
- 索引優化
小結
- 關聯 - association 【多對一】
- 集合 - collection 【一對多】
-
javaType & ofType
- javaType 用來指定實體類中屬性的類型
- ofType 用來指定映射到List或者集合中的entity類型,泛型中的約束類型
注意點:
- 保證SQL的可讀性,盡量保證通俗易懂
- 注意一對多和多對一中屬性名和字段的問題
- 如果問題不好排查錯誤,可以使用LOG4J日志
總結
到此這篇關于Mybatis一對多和多對一處理的文章就介紹到這了,更多相關Mybatis一對多和多對一處理內容請搜索服務器之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持服務器之家!
原文鏈接:https://blog.csdn.net/weixin_61200215/article/details/120256339