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>
 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM