好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

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>

结果映射

面试高频点

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

小结

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

注意点:

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

总结

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

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

查看更多关于Mybatis一对多和多对一处理的深入讲解的详细内容...

  阅读:12次