实例:一个学生拥有多个老师
实体类:
public class Student {
private int Studengid;
private String name;
//一个学生对应多个老师
private List<Teacher> teachers;
@Override
public String toString() {
return "Student{" +
"Studengid=" + Studengid +
", name='" + name + '\'' +
", teachers=" + teachers +
'}';
}
public List<Teacher> getTeachers() {
return teachers;
}
public void setTeachers(List<Teacher> teachers) {
this.teachers = teachers;
}
public Student(int studengid, String name, List<Teacher> teachers) {
Studengid = studengid;
this.name = name;
this.teachers = teachers;
}
public Student() {
}
public Student(int studengid, String name) {
Studengid = studengid;
this.name = name;
}
public int getStudengid() {
return Studengid;
}
public void setStudengid(int studengid) {
Studengid = studengid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public class Teacher {
private int Id;
private int age;
private String name;
private String sex;
private int Studentid;
public Teacher() {
}
public Teacher(int id, int age, String name, String sex, int studentid) {
this.Id = id;
this.age = age;
this.name = name;
this.sex = sex;
this.Studentid = studentid;
}
@Override
public String toString() {
return "Teacher{" +
"Id=" + Id +
", age=" + age +
", name='" + name + '\'' +
", sex='" + sex + '\'' +
", Studentid=" + Studentid +
'}';
}
public int getId() {
return Id;
}
public void setId(int id) {
Id = id;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getStudentid() {
return Studentid;
}
public void setStudentid(int studentid) {
Studentid = studentid;
}
}
两种方法:
一、按照结果映射处理
<resultMap id="studentMap" type="student">
<result property="Studengid" column="Studentid"></result>
<result property="name" column="sname"></result>
<collection property="teachers" ofType="teacher">
<result column="Id" property="Id"></result>
<result column="age" property="age"></result>
<result column="name" property="name"></result>
<result column="sex" property="sex"></result>
<result column="Studentid" property="Studentid"></result>
</collection>
</resultMap>
<select id="getStudent" resultMap="studentMap">
select s.Studentid ,s.name sname,t.Id,t.Studentid,t.name,t.sex,t.age
from tab_teacher t,tab_student s
where s.Studentid=t.Studentid and s.Studentid = #{id}
</select>
二、按照查询嵌套处理
<select id="getStudent2" resultMap="studentMap2">
select *
from tab_student s
where s.Studentid = #{id}
</select>
<resultMap id="studentMap2" type="student">
<result property="Studengid" column="Studentid"></result>
<!--ofType映射集合中的泛型-->
<collection property="teachers" javaType="ArrayList" ofType="teacher" select="getteacher" column="Studentid"></collection>
</resultMap>
<select id="getteacher" resultType="teacher">
select *
from tab_teacher t
where t.Studentid = #{Studengid}
</select>