mybatis一对多关联关系

  在《mybatis一对一关联关系》中,我们以班级和教师的例子介绍了一对一的关联关系,现在引入学生类,一个学生对应一个班级,一个班级对应多个学生,来介绍mybatis如何处理一对多的关联关系。
  
  首先创建表和实体类:

teacher表:

1
2
3
4
5
6
CREATE TABLE teacher(
t_id INT PRIMARY KEY AUTO_INCREMENT,
t_name VARCHAR(20)
);
INSERT INTO teacher(t_name) VALUES('LS1');
INSERT INTO teacher(t_name) VALUES('LS2');

image_1b4d974o2lm7dfiser8qvj159.png-13.4kB

class表:

1
2
3
4
5
6
7
8
CREATE TABLE class(
c_id INT PRIMARY KEY AUTO_INCREMENT,
c_name VARCHAR(20),
teacher_id INT
);
ALTER TABLE class ADD CONSTRAINT fk_teacher_id FOREIGN KEY (teacher_id) REFERENCES teacher(t_id);
INSERT INTO class(c_name, teacher_id) VALUES('bj_a', 1);
INSERT INTO class(c_name, teacher_id) VALUES('bj_b', 2);

image_1b4d4baab1lar1uf61hfj2so1ge613.png-14kB

其中teacher_id是指向teacher表的外键:

image_1b4d98u561fcn30bon314mp1k6vm.png-23.3kB

student表:

1
2
3
4
5
6
7
8
9
10
11
12
CREATE TABLE student(
s_id INT PRIMARY KEY AUTO_INCREMENT,
s_name VARCHAR(20),
class_id INT
);
ALTER TABLE student ADD CONSTRAINT fk_class_id FOREIGN KEY (class_id) REFERENCES class(c_id);
INSERT INTO student(s_name, class_id) VALUES('xs_A', 1);
INSERT INTO student(s_name, class_id) VALUES('xs_B', 1);
INSERT INTO student(s_name, class_id) VALUES('xs_C', 1);
INSERT INTO student(s_name, class_id) VALUES('xs_D', 2);
INSERT INTO student(s_name, class_id) VALUES('xs_E', 2);
INSERT INTO student(s_name, class_id) VALUES('xs_F', 2);

image_1b4d340jksb31n4511659qhbql9.png-12.2kB

其中class_id是指向class表的外键:

image_1b4d3uqo3hmueec1c961m57e7cm.png-17.6kB

实体类Student,Teacher和Classes:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public class Student {
private int id;
private String name;
//getters and setters
@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + "]";
}
public Student(int id, String name) {
super();
this.id = id;
this.name = name;
}
public Student() {
super();
}
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public class Teacher {
private int id;
private String name;
//getters and setters
@Override
public String toString() {
return "Teacher [id=" + id + ", name=" + name + "]";
}
public Teacher(int id, String name) {
super();
this.id = id;
this.name = name;
}
public Teacher() {
super();
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
public class Classes {
private int id;
private String name;
private Teacher teacher;
private List<Student> students;
//getters and setters
@Override
public String toString() {
return "Classes [id=" + id + ", name=" + name + ", teacher=" + teacher + ", students=" + students + "]";
}
public Classes(int id, String name, Teacher teacher, List<Student> students) {
super();
this.id = id;
this.name = name;
this.teacher = teacher;
this.students = students;
}
public Classes() {
super();
}
}

编写映射文件classesMapper.xml(关键)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.mybatis.test6.classesMapper">
<!--
方法一: 嵌套结果: 使用嵌套结果映射来处理重复的联合结果的子集
SELECT * FROM class c, teacher t,student s WHERE c.teacher_id=t.t_id AND c.C_id=s.class_id AND c.c_id=1
-->
<!--
根据classId查询对应的班级信息,包括学生,老师
-->
<select id="getClassesById" parameterType="int" resultMap="classesResultMap">
select * from class c,teacher t,student s where c.teacher_id=t.t_id
and
s.class_id=c.c_id and c.c_id=#{id}
</select>
<resultMap type="Classes" id="classesResultMap">
<id property="id" column="c_id" />
<result property="name" column="c_name" />
<association property="teacher" javaType="Teacher">
<id property="id" column="t_id" />
<result property="name" column="t_name" />
</association>
<!-- ofType指定students集合中的对象类型 -->
<collection property="students" ofType="Student">
<id property="id" column="s_id" />
<result property="name" column="s_name" />
</collection>
</resultMap>
<!--
方法二:嵌套查询:通过执行另外一个SQL映射语句来返回预期的复杂类型
SELECT * FROM class WHERE c_id=1;
SELECT * FROM teacher WHERE t_id=1 //1 是上一个查询得到的teacher_id的值
SELECT * FROM student WHERE class_id=1 //1是第一个查询得到的c_id字段的值
-->
<!--
查询所有的班级信息,包括学生,老师
-->
<select id="getAllClasses" resultMap="allClassesResultMap">
select * from class
</select>
<resultMap type="Classes" id="allClassesResultMap">
<id property="id" column="c_id" />
<result property="name" column="c_name" />
<association property="teacher" column="teacher_id"
select="getTeacherById">
</association>
<collection property="students" column="c_id"
select="getStudentByClassId"></collection>
</resultMap>
<select id="getTeacherById" parameterType="int" resultType="Teacher">
select t_name name from teacher where t_id=#{id}
</select>
<select id="getStudentByClassId" parameterType="int" resultType="Student">
select s_id id,s_name name from student where class_id=#{id}
</select>
</mapper>

这里提供了两种方法处理一对多的关联关系,和一对一中类似,分别是联表查询和分两次查询。关键点在于resultMap节点中的collection节点,注意在联表查询时,需要通过collection节点的ofType属性指定返回的集合的元素类型。