Mybatis- 一对多的处理
按结果嵌套处理
<!--第一种查询方法:按结果嵌套处理-->
<select id="getTeacher" resultMap="TeacherStudent">
select s.id sid, s.name sname, t.name tname, t.id tid
from mybatis.student s, mybatis.teacher t
where s.tid = t.id and t.id=#{id};
</select>
<resultMap id="TeacherStudent" 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="getTeacher1" resultMap="TeacherStudent2">
select * from mybatis.teacher where id = #{id};
</select>
<select id="getStudent" resultType="Student">
select * from mybatis.student where tid = #{tid};
</select>
<resultMap id="TeacherStudent2" type="Teacher">
<collection property="students" javaType="ArrayList" ofType="Student" select="getStudent" column="id"/>
</resultMap>
我从小到大没有什么梦想,一直在路上不问远方,只是衷心希望自己成为一个很厉害的人。你问我什么算厉害,大概就是能有朝一日,保护好我爱的人,赡养好家人。