假设:我要查询主子表的集合数据。(一对多)
一般查询就先查出主表,在查询字表。
用Mapper xml 中定义语句集合查询一次查询结果。
业务:
假设,要查询 某用户的所有上传记录

代码处理:
mapper:
<resultMap id="photoInfo" type="SurgeryPhotoClassificationInfo">
<result column="id" property="id"/>
<result column="photo_type" property="photoType"/>
<result column="take_date" property="takeDate"/>
<result column="customer_number" property="customerNumber"/>
<result column="name" property="createName"/>
<collection property="surgeryPhotoInfo" javaType="ArrayList" ofType="SurgeryPhotoInfo">
<result column="id" property="id"/>
<result column="photo_path" property="photoPath"/>
<result column="customer_extend_photo_id" property="surPhotoClassificationInfoId"/>
<result column="remarks" property="description"/>
</collection>
</resultMap>

<select id="selectSinglePhoto" resultMap="photoInfo">
SELECT a.create_by,a.customer_number,a.take_date,a.photo_type,a.id ,b.photo_path,b.remarks,su.NAME
from customer_extend_photo a
INNER JOIN customer_extend_photo_chil b on b.customer_extend_photo_id = a.id
INNER JOIN sys_user su on a.create_by = su.id
where a.del_flag = '0'
and a.photo_type =#{photoType}
and a.customer_number=#{customerNumber}
</select>


这样就可以查出所有上传的次数,每次上次的照片了。

本文介绍如何使用MyBatis通过一次查询获取主表及其相关子表的集合数据,采用XML映射文件定义复杂的一对多查询,实现高效的数据检索。具体示例展示了如何配置resultMap和collection节点来关联主子表字段,以及如何编写SQL语句进行内连接查询。
2790

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



