1.按照查询嵌套进行查询处理
<select id="getStudent" resultMap="StudentTeacher">
select * from student
</select>
<resultMap id="StudentTeacher" type="Student">
<!-- 复杂的属性,我们需要单独处理 对象属性使用association 集合属性使用collection-->
<association property="teacher" column="tid" javaType="Teacher" select="getTeacher"/>
</resultMap>
<select id="getTeacher" resultType="Teacher">
select * from teacher where id=#{id}
</select>
2.按照结果嵌套处理
select id="getStudent2" resultMap="StudentTeacher2">
select s.id sid ,s.name sname ,t.name tname
from student s,teacher t
where s.tid=t.id;
</select>
<resultMap id="StudentTeacher2" type="Student">
<result property="id" column="sid"/>
<result property="name" column="sname"/>
<association property="teacher" javaType="Teacher">
<result property="name" column="tname"/>
</association>
</resultMap>
本文详细讲解了如何在Spring Boot中使用MyBatis进行查询,涉及嵌套查询(通过`association`处理复杂属性)和结果嵌套(通过`association`和`result`标签处理关联数据)。实例包括`StudentTeacher`和`StudentTeacher2`的 resultMap,展示了如何连接多个表并获取关联数据。
736

被折叠的 条评论
为什么被折叠?



