Mybatis 查询结果映射到实体对象的List属性,List中元素自动去重问题

mybatis中编码如下所示:

<resultMap id="RM-BizGroupRelatedEventInfo" type="com.XXX.bean.BizGroupRelatedEventInfo">
    <result column="event_id" property="id" jdbcType="BIGINT" />
    <result column="event_name" property="name" jdbcType="VARCHAR" />
    <result column="image_obj" property="imageObj" jdbcType="VARCHAR" />
    <result column="inst_id" property="instId" jdbcType="BIGINT" />
    <result column="inst_name" property="teamName" jdbcType="VARCHAR" />
    <result column="description" property="description" jdbcType="VARCHAR" />
    <collection property="versionList" ofType="java.lang.String" javaType="java.util.List">
        <result column="event_version"/>
    </collection>
    <collection property="statusList" ofType="java.lang.Integer" javaType="java.util.List">
        <result column="event_version_status"/>
    </collection>
    <collection property="platforms" ofType="java.lang.Integer" javaType="java.util.List">
        <result column="platform"/>
    </collection>
</resultMap>
<select id="listRelatedEventInfoByGroupIdAndType" resultMap="RM-BizGroupRelatedEventInfo">
    select
    e.id event_id, e.name event_name, e.image_obj image_obj, e.description description, ev.version event_version, ev.status event_version_status, bep.platform platform, bi.id inst_id, bi.name inst_name
    from biz_event e, biz_event_version ev,	biz_event_version_group evg, biz_event_platform bep, biz_event_customer bec, biz_inst bi
    where 1 = 1
    and ev.id = evg.event_version_id
    and ev.event_id = e.id
    and evg.group_id = #{groupId,jdbcType=BIGINT}
    and evg.group_type = #{type,jdbcType=TINYINT}
    and bep.event_id = e.id
    and bec.event_id = e.id
    and e.inst_id = bi.id
</select>

1,问题:

查询出的结果中BizGroupRelatedEventInfo 对象的statusList 属性预期的正常结果应为[2,6,2],事实却是[2,6],也就是其中的元素被去重了。

2,解决:

好在本人只用到的是statusList中的最新元素(即最后一个元素2),既然后面的被前面的给覆盖了,那么就把自己想要的结果倒序过来,放在第一个位置:

更改后的sql如下,也就是添加了 order by e.id, ev.id desc,将原来默认的e.id, ev.id asc 中的ev.id倒序了过来:

<select id="selectRelatedEventInfoByGroupIdAndType" resultMap="RM-BizGroupRelatedEventInfo">
    select
    e.id event_id, e.name event_name, e.image_obj image_obj, e.description description, ev.version event_version, ev.status event_version_status, bep.platform platform, bi.id inst_id, bi.name inst_name
    from biz_event e, biz_event_version ev,	biz_event_version_group evg, biz_event_platform bep, biz_event_customer bec, biz_inst bi
    where 1 = 1
    and ev.id = evg.event_version_id
    and ev.event_id = e.id
    and evg.group_id = #{groupId,jdbcType=BIGINT}
    and evg.group_type = #{type,jdbcType=TINYINT}
    and bep.event_id = e.id
    and bec.event_id = e.id
    and e.inst_id = bi.id
    order by e.id, ev.id desc
</select>

这样得到的statusList就是[2,6], 这个2是最新的一个状态值。

改变前如果statusList集合为[3,4,6],则改变后为[6,4,3],因为改变前获取的是最后一个元素6,改变后获取的则是第一个元素,是同一个值,所以暂时解决了本次遇到的问题。

备注:

本人遇到的这种情况的解决问题是:只是用到了list类型属性中的一个值。对于仍需要全部list类型属性全部值(不去重)的话,本解决方法不适用,希望以后能够想到解决办法,如果路过的大佬知道解决方法的话,也期待能够指教一下。

原文:https://blog.csdn.net/qq_2300688967/article/details/80765004

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值