public class Student { private int id; private String name; private int tid;}
public class TeacherOnetoMore { private int id; private String name; private List<Student> students;}
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.cyk.dao.TeacherMapper">
<select id="getTeacherOnetoMore" resultType="TeacherOnetoMore">
select * from teacher;
</select>
<select id="getTeacherOnetoMoreById" resultMap="TeacherStudent">
SELECT s.id sid,s.name sname,t.id tid,t.name tname
FROM student s,teacher t
where s.tid = t.id and t.id = #{id}
</select>
<resultMap id="TeacherStudent" type="TeacherOnetoMore">
<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="getTeacherOnetoMoreById2" resultMap="TeacherStudent2">
select * from teacher where id = #{id}
</select>
<resultMap id="TeacherStudent2" type="TeacherOnetoMore">
<collection property="students" column="id" javaType="ArrayList" ofType="Student" select="getStudentByTeacher"/>
</resultMap>
<select id="getStudentByTeacher" resultType="Student">
select * from student where tid = #{id}
</select>
</mapper>
javaType :用来指定实体类中属性的类型
ofType : 用来指定映射list或者集合中的pojo类型,泛型的约束类型;