Mybatis多表关联查询字段值覆盖问题解决

如下xml映射文件所示,数据库联查时两边有列名相同

<resultMap id="bai" type="com.boot.model.entity.ActReDelegateRule">
        <id column="id" property="id" />
        <result column="assignee" property="assignee" />
        <result column="dele_scope" property="deleScope" />
        <result column="trigger_type" property="triggerType" />
        <result column="priority" property="priority" />
        <result column="status" property="status" />
        <result column="int_user" property="intUser" />
        <result column="int_time" property="intTime" />
        <collection property="actReDeleAttorneys" ofType="com.boot.model.entity.ActReDeleAttorney">
            <id column="id" property="id" />
            <result column="dele_id" property="deleId" />
            <result column="attorney_type" property="attorneyType" />
            <result column="attorney" property="attorney" />
            <result column="initiator_rel_org" property="initiatorRelOrg" />
            <result column="attorney_post" property="attorneyPost" />
            <result column="sn" property="sn" />
        </collection>

    </resultMap>

查询展示:

<select id="searchActReDelegateRuleByAssigneeAll" resultMap="bai">
SELECT
	a.id,a.assignee,aa.attorney,aa.dele_id,aa.id
	FROM
	 act_re_delegate_rule a RIGHT JOIN act_re_dele_attorney aa ON a.id = aa.dele_id
	 WHERE  a.id=#{Id}
    </select>

最后会导致两表列名相同的“id”列值被覆盖:
{
“code”: 1,
“msg”: “操作成功”,
“data”: [
{
“id”: “756172726246354944”,
“assignee”: “baibai”,
“deleScope”: a1,
“triggerType”: a2,
“priority”: a3,
“status”: a4,
“intUser”: a5,
“intTime”: a6,
“actReDeleAttorneys”: [
{
“id”: “756172726246354944”,
“deleId”: “756169606771888128”,
“attorneyType”: 1,
“attorney”: “111–1”,
“initiatorRelOrg”: 1,
“attorneyPost”: 1,
“sn”: 1
}
]
},
{
“id”: “756172726246354945”,
“assignee”: “baibai”,
“deleScope”: a1,
“triggerType”: a2,
“priority”: a3,
“status”: a4,
“intUser”: a5,
“intTime”: a6,
“actReDeleAttorneys”: [
{
“id”: “756172726246354945”,
“deleId”: “756169606771888128”,
“attorneyType”: 2,
“attorney”: “222–2”,
“initiatorRelOrg”: 2,
“attorneyPost”: 2,
“sn”: 2
}
]
}
],
“meta”: null
}
两个id值变得一样,后者id覆盖了前者表中的id

解决方法:
用AS起别名:

<resultMap id="baishuaishuai" type="com.boot.model.entity.ActReDelegateRule">
        <id column="id" property="id" />
        <result column="assignee" property="assignee" />
        <result column="dele_scope" property="deleScope" />
        <result column="trigger_type" property="triggerType" />
        <result column="priority" property="priority" />
        <result column="status" property="status" />
        <result column="int_user" property="intUser" />
        <result column="int_time" property="intTime" />
        <collection property="actReDeleAttorneys" ofType="com.boot.model.entity.ActReDeleAttorney">
            <id column="ids" property="id" />
            <result column="dele_id" property="deleId" />
            <result column="attorney_type" property="attorneyType" />
            <result column="attorney" property="attorney" />
            <result column="initiator_rel_org" property="initiatorRelOrg" />
            <result column="attorney_post" property="attorneyPost" />
            <result column="sn" property="sn" />
        </collection>

    </resultMap>

映射列写别名,查询将原来的id AS操作起别名为ids即可,这样不用修改数据库了,我之前还修改了数据库发现改动较大还不如这样方便,只修改映射

<select id="searchActReDelegateRuleByAssigneeAll" resultMap="baishuaishuai">
SELECT
	a.id,a.assignee,aa.attorney,aa.dele_id,aa.id as ids
	FROM
	 act_re_delegate_rule a RIGHT JOIN act_re_dele_attorney aa ON a.id = aa.dele_id
	 WHERE  a.id=#{Id}
    </select>

修改后的查询结果:
{
“id”: “756169606771888128”,
“assignee”: “baibai”,
“deleScope”: null,
“triggerType”: null,
“priority”: null,
“status”: null,
“intUser”: null,
“intTime”: null,
“actReDeleAttorneys”: [
{
“id”: “756172726246354944”,
“deleId”: “756169606771888128”,
“attorneyType”: 1,
“attorney”: “111–1”,
“initiatorRelOrg”: 1,
“attorneyPost”: 1,
“sn”: 1
},
{
“id”: “756172726246354945”,
“deleId”: “756169606771888128”,
“attorneyType”: 2,
“attorney”: “222–2”,
“initiatorRelOrg”: 2,
“attorneyPost”: 2,
“sn”: 2
}
]
}

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值