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类型属性全部值(不去重)的话,本解决方法不适用,希望以后能够想到解决办法,如果路过的大佬知道解决方法的话,也期待能够指教一下。

猜你喜欢

转载自blog.csdn.net/qq_2300688967/article/details/80765004