mybatis两种嵌套查询方式

1,推荐用第一种

<select id="getTeacher2" resultMap="TeacherStudent">
select s.id sid,s.name sname,t.id tid,t.name tname
from teacher t,student s
where s.tid=tid and tid=#{tid}
</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>

2,
<select id="getTeacher2" resultMap="TeacherStudent">
select * from mybatis.teacher where id = #{id}
</select>

<resultMap id="TeacherStudent" type="Teacher">
<collection property="students" javaType="ArrayList" ofType="Student" select="getStudentByTeacherId" column="id"/>
</resultMap>

<select id="getStudentByTeacherId" resultType="Student">
select * from mybatis.student where tid = #{tid}
</select>
 
posted @ 2022-04-12 16:58  枫叶红时  阅读(815)  评论(0)    收藏  举报