<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPEmapperPUBLIC"-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mappernamespace="edu.hqu.mybatis.dao.StudentMapper"><!--只进行一次查询,联表查询--><resultMapid="studentResultMap"type="Student"><resultproperty="id"column="sid"/><resultproperty="name"column="sname"/><associationproperty="teacher"javaType="Teacher"><!--teacher为学生表中的属性--><resultproperty="id"column="tid"/><resultproperty="name"column="tname"/></association></resultMap><selectid="getAllStudents"resultMap="studentResultMap">
select s.id as sid,s.name as sname,s.tid as stid,t.id as tid,t.name as tname
from t_student s,t_teacher t where s.tid=t.id;
</select><!--先查询出所有学生,根据tid查询老师,两次查询--><selectid="getStudents2"resultMap="studentTeacher">
select * from t_student
</select><resultMapid="studentTeacher"type="Student"><resultproperty="id"column="id"/><resultproperty="name"column="name"/><associationproperty="teacher"column="tid"javaType="Teacher"select="getTeacher"><resultproperty="id"column="id"/><resultproperty="name"column="name"/></association></resultMap><selectid="getTeacher"resultType="Teacher">
select * from t_teacher where id=#{tid}
</select></mapper>