mybatis中映射一对多关系

本文介绍MyBatis中一对多关联映射的实现方法,通过具体示例展示了如何配置resultMap来实现学生档案及其关联表达详情的查询。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一对多

其中columnPrefix是给字段加前缀,在查出的数据id重命名为express_id加上前缀即可

<resultMap id="getDetailsResultMap" type="StudentArchivesInfoExpressDetailsDTO">
    <id column="id" property="id"></id>
    <result column="student_id" property="studentId"></result>
    <collection property="express" columnPrefix="express_" ofType="ExpressDTO">
        <id column="id" property="id"></id>
        <result column="content" property="content"></result>
        <result column="time" property="time"></result>
    </collection>
</resultMap>
<select id="getDetails" resultMap="getDetailsResultMap">
    SELECT
        a.id,
        a.student_id,
        b.id AS express_id,
        b."content" AS express_content,
        b."time" AS express_time
    FROM
        t_student_archives_info_express AS a 
        LEFT JOIN t_express_message AS b
        ON a.id = b.relation_id AND b.relation_type = 1
</select>
//实体类
public class StudentArchivesInfoExpressDetailsDTO{
	private Long id;
	private Long studentId;
	private List<ExpressDTO> express;
}
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值