使用场景:
数据库中的多张表,表之间通过一个相同的属性关联,一张表中的多条记录对应另一张表的一条记录,称作多对一;反之,一对多
如图:
student和teacher之间通过关联
//学生类
package com.LinXiaoDe.pojo;
public class Student {
private int id;
private String name;
Teacher teacher;//注意这里和数据库中的tid不一致
}
//老师类
package com.LinXiaoDe.pojo;
public class Teacher {
private int id;
private String name;
}
查询所有学生的信息,并且获取学生老师的信息,sql可以如下查询:
> select s.id,s.name,t.name from student s,teacher t where s.tid = t.id;
> select * from student s,teacher t where s.tid = t.id;
如何用mybatis的mapper实现?
//StudentMapper 类
public interface StudentMapper {
public List<Student> getStudents();
}
//TeacherMapper 类
public interface TeacherMapper {
List<Teacher> getTeachers();
}
//StudentMapper.xml中
<select id="getStudents" resultType="student">
select * from student;
</select>
//如果只执行上面这一句,只能查找到学生的信息(id,name,null),因为最后一个teacher属性在数据库中字段是int tid,是无法直接查询到的,考虑下面这句
//下面的select可以通过id查找到teacher,因此我们的思路是通过getStudents找到tid,然后嵌套getTeacherById获取teacher
<select id="getTeacherById" resultType="teacher">
select * from teacher where id=#{id};
</select>
//用 将两个select嵌套起来,其中:
//javaType="teacher" 指定类未teacher
//select="getTeacherById"指定嵌套的select语句
<resultMap id="studentTeacherResult1" type="student">
<result property="id" column="id"/>
<result property="name" column="name"/>
<association property="teacher" column="tid" javaType="teacher" select="getTeacherById"/>
</resultMap>
<select id="getStudents" resultMap="studentTeacherResult1">
select * from student;
</select>
<select id="getTeacherById" resultType="teacher">
select * from teacher where id=#{id};
</select>
<resultMap id="studentTeacherResult2" 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="getStudents2" resultMap="studentTeacherResult2">
select s.id as sid,s.name as sname,t.name as tname
from student s,teacher t
where s.tid = t.id;
select>
///可以简化如下:
<resultMap id="studentTeacherResult2" type="student">
<result property="id" column="id"/>
<result property="name" column="name"/>
<association property="teacher" javaType="teacher">
<result property="name" column="name"/>
association>
resultMap>
<select id="getStudents2" resultMap="studentTeacherResult2">
select *
from student s,teacher t
where s.tid = t.id;
select>
数据库不变,但是将Teacher增加属性private List
,Student中将Teacher 改为tid
//student
public class Student {
private int id;
private String name;
int tid;
}
//teacher
public class Teacher {
private int id;
private String name;
private List<Student> students;
}
<resultMap id="getTeacherResult" 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="getTeachers" resultMap="getTeacherResult">
select t.id as tid, t.name tname,s.name as sname,s.id sid
from teacher t,student s
where t.id=s.tid and t.id=#{id};
select>
<resultMap id="getTeachersResult2" type="teacher">
<collection property="students" javaType="ArrayList" ofType="student" column="id" select="getStudentsById"/>
</resultMap>
<select id="getTeachers2" resultMap="getTeachersResult2">
select * from teacher where id=#{id}
</select>
<select id="getStudentsById" resultType="student">
select * from student where tid=#{id};
</select>