上次的文章中我们学习了一对一映射,这次我们来学习一对多映射
我们可以想象这样一个场景,一个班里会有很多学生,但是一名学生只属于一个班,这就是典型的一对多的关系
那我们就来建立一个学生表,tb_student,如下
image.png
依次建立pojo类,修改之前的classesMapper
package com.pojo;
public class Student {
private int id;
private String name;
private int c_id;
public Student() {
}
public Student(int id, String name, int c_id) {
this.id = id;
this.name = name;
this.c_id = c_id;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getC_id() {
return c_id;
}
public void setC_id(int c_id) {
this.c_id = c_id;
}
@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + ", c_id=" + c_id + "]";
}
}
第一种写法,使用ofType
<?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="mapper.classesMapper">
<select id="findClassesById" resultMap="classesMap">
select * from tb_class where id=#{id}
</select>
<select id="findClasses" resultMap="classesMap">
select * from tb_class c
inner join tb_teacher t on t.id = c.t_id
inner join tb_student s on s.c_id = c.id
</select>
<resultMap id="classesMap" type="Classes">
<id property="id" column="id"/>
<result property="name" column="c_name"/>
<association property="teacher" select="mapper.teacherMapper.findTeacherById" column="t_id"/>
<collection property="students" ofType="Student">
<id property="id" column="sid"/>
<result property="name" column="name"/>
</collection>
</resultMap>
</mapper>
第二种写法,使用select
<?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="mapper.classesMapper">
<select id="findClassesById" resultMap="classesMap">
select * from tb_class where id=#{id}
</select>
<select id="findClasses" resultMap="classesMap">
select * from tb_class c
inner join tb_teacher t on t.id = c.t_id
inner join tb_student s on s.c_id = c.id
</select>
<resultMap id="classesMap" type="Classes">
<id property="id" column="id"/>
<result property="name" column="c_name"/>
<association property="teacher" select="mapper.teacherMapper.findTeacherById" column="t_id"/>
<collection property="students" select="mapper.studentMapper.findStudentByClassesId" column="id"/>
</resultMap>
</mapper>
这时就要加上studentMapper
<?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="mapper.studentMapper">
<select id="findStudentByClassesId" resultType="Student">
select * from tb_student where c_id=#{id}
</select>
</mapper>
最后我们进行测试
package com.test;
import com.dao.ClassesDao;
import com.pojo.Classes;
import java.util.List;
public class Test {
public static void main(String[] args) {
ClassesDao classesDao = new ClassesDao();
List<Classes> list = classesDao.findClasses();
int i = 0;
for (Classes classes:list
) {
System.out.println("班级名称:"+classes.getName()+" 教师名称"+classes.getTeacher().getName()+" 学生名称"+classes.getStudents().get(i).getName());
i++;
}
}
}
输出结果
image.png
网友评论