1、多对一 多个学生对应一个老师
方法一 按照查询嵌套处理(对应子查询)
通过student表关联的tid字段,去查询teacher表的teacher信息。简单的说就是套娃,在student查询里套进了resultMap,resultMap关联了teacher对象和tid表字段,并且套娃了teacher查询。resultMap="StudentTeacher"是结果集映射。
mapper接口:
public interface StudentMapper {
public List<Student> getStudent();
}
mapper.xml :
<mapper namespace="com.kuang.dao.StudentMapper">
<select id="getStudent" resultMap="StudentTeacher">
select * from mybatis.student
</select>
<resultMap id="StudentTeacher" type="Student">
<result property="id" column="id"/>
<result property="name" column="name"/>
<association property="teacher" column="tid" javaType="Teacher" select="getTeacher"/>
</resultMap>
<select id="getTeacher" resultType="Teacher">
select * from mybatis.teacher where id = #{id}
</select>
</mapper>