gpt4 book ai didi

Mybatis一对多和多对一处理的深入讲解

转载 作者:qq735679552 更新时间:2022-09-27 22:32:09 31 4
gpt4 key购买 nike

CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.

这篇CFSDN的博客文章Mybatis一对多和多对一处理的深入讲解由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.

建表

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>

结果映射

Mybatis一对多和多对一处理的深入讲解

面试高频点

  • MySQL引擎
  • InnoDB底层原理
  • 索引
  • 索引优化
  •  

小结 。

  1. 关联 - association 【多对一】
  2. 集合 - collection 【一对多】
  3. javaType & ofType
    1. javaType 用来指定实体类中属性的类型
    2. ofType 用来指定映射到List或者集合中的entity类型,泛型中的约束类型

注意点:

  • 保证SQL的可读性,尽量保证通俗易懂
  • 注意一对多和多对一中属性名和字段的问题
  • 如果问题不好排查错误,可以使用LOG4J日志

总结

到此这篇关于Mybatis一对多和多对一处理的文章就介绍到这了,更多相关Mybatis一对多和多对一处理内容请搜索我以前的文章或继续浏览下面的相关文章希望大家以后多多支持我! 。

原文链接:https://blog.csdn.net/weixin_61200215/article/details/120256339 。

最后此篇关于Mybatis一对多和多对一处理的深入讲解的文章就讲到这里了,如果你想了解更多关于Mybatis一对多和多对一处理的深入讲解的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com