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 @   枫叶红时  阅读(783)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· 清华大学推出第四讲使用 DeepSeek + DeepResearch 让科研像聊天一样简单!
· 实操Deepseek接入个人知识库
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库

阅读目录(Content)

此页目录为空

点击右上角即可分享
微信分享提示