mybatis之resultMap

在项目中我们很多时候会遇到需要用到多表连接查询的时候,通常这种时候我们的mybatis的
<select>
中并没有一个合适的resultType去接收查询得到的结果,这个时候就需要用到
<resultMap>

示例:

JavaBean(此处省略getter/setter方法)

public class ActivityUserTask  {

    private Long id;
    private Long activityId;
    private Long taskId;
    private Long matchId;
    private Long bUserId;
    private Long gUserId;
    private Date createTime;
    private String bUserName;
    private String gUserName;
    private Integer type;
    private List<ActivityUserTaskPhoto> photos;
}


public class ActivityUserTaskPhoto  {
    private Long taskId;
    private String picId;
    private Integer status;
}

XML

<sql id="select_column_sql">
        select 
        a.id as id,
        a.task_id as taskId,
        a.activity_id as activityId,
        a.match_id as matchId,
        a.b_user_id as bUserId,
        a.g_user_id as gUserId,
        a.create_time as createTime,
        a.update_time as updateTime,
        b1.name as bUserName,
        b2.name as gUserName,
        c.type as type,
        d.pic_id as picId
        from m_activity_user_task a
        left join m_user_info b1 on a.b_user_id = b1.id
        left join m_user_info b2 on a.g_user_id = b2.id
        left join m_activity_user_match c on a.match_id = c.id
        left join m_activity_user_task_photo d on a.id = d.task_id
    </sql>

    <resultMap id="userTask" type="ActivityUserTask">
        <id property="id" column="id"/>
        <result property="taskId" column="taskId"/>
        <result property="bUserId" column="bUserId"/>
        <result property="gUserId" column="gUserId"/>
        <result property="createTime" column="createTime"/>
        <result property="bUserName" column="bUserName"/>
        <result property="gUserName" column="gUserName"/>
        <result property="type" column="type"/>
        <collection property="photos" ofType="ActivityUserTaskPhoto">
            <result property="picId" column="picId"/>
        </collection>
    </resultMap>

    <select id="findList" parameterType="ActivityUserTask"
        resultMap="userTask">
        <include refid="select_column_sql" />
        order by id desc
    </select>

这里有几点需要注意:
1.由于没有一个合适的resultType所以我只有自己定义一个resultMap去接收select到的结果。
2.由于JAVA类中的属性名和数据库中的字段名不一样需要用到别名去与数据库中的表进行映射
3.由于用到了别名所以resultMap中的<result property="taskId" column="taskId"/>中column也必须等于别名。
4.<collection property="photos" ofType="ActivityUserTaskPhoto">
用于一对多映射,property属性名,ofType为属性所属类。
其中还有id表示主键,result表示非主键字段。
5.<association>用于一对一映射。

resultMapMyBatis 中用于自定义查询结果的映射方式。通过 resultMap,我们可以定义字段名和属性名的对应关系,并且可以选择性地指定要显示的列,使结果映射更加灵活和方便。使用 resultMap 可以实现字段名与属性名不一致的映射,以及关联查询时的结果处理。 需要注意的是,resultType 和 resultMap 在 select 元素中是互斥的,即不能同时存在,只能选择其中一个来定义返回类型。resultType 直接表示返回的类型,而 resultMap 则是对外部 resultMap 的引用。 使用 resultMap 的场景多种多样,可以根据实际需求灵活选择。推荐使用 resultMap,因为它更加灵活,并且在自定义结果映射方面功能更强大。 总结来说,resultMapMyBatis 中用于自定义查询结果的映射方式,可以实现字段名与属性名的对应关系,并且可以选择性地指定要显示的列,使结果映射更加灵活和方便。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [MyBatisresultMap详解](https://blog.csdn.net/weixin_49707895/article/details/109564527)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值