在mybatis中的嵌套集合中再次使用相同的结果集

tzdcorbm  于 2021-08-25  发布在  Java
关注(0)|答案(0)|浏览(189)

我有以下mybatis结果图,其中包含一个集合:

<resultMap id="getAllDeliveries" type="foo.bar.dao.pojo.Delivery">
    <id property="id" column="deliveryId" />
    // some more stuff
    <collection property="topicGroups" resultSet="topics-1" resultMap="topicGroups" 
            column="deliveryId" foreignColumn="deliveryId"
            ofType="foo.bar.dao.pojo.TopicGroup" />
</resultMap>

<resultMap id="topicGroups" 
        type="foo.bar.dao.pojo.TopicGroup"
        autoMapping="true">
    <id property="id" column="topicGroupId" />
    <collection property="dataTopics" resultSet="topics-2"
            column="deliveryId" foreignColumn="deliveryId"
            ofType="foo.bar.dao.pojo.DataTopic">
        <result property="id" column="topicId" />
        // some more stuff
    </collection>
</resultMap>

使用以下sql查询,该查询由主查询和2次完全相同的次查询组成:

<select id="getAllDeliveries" 
    resultSets="deliveries,topics-1,topics-2"
    resultMap="getAllDeliveries">

    SELECT delivery.[deliveryId]
            <!-- some more stuff -->
    FROM [Delivery] as delivery

    SELECT dtglink.deliveryId,
        dtglink.topicGroupId,
        tgSet.topicId
    FROM [DeliveryTopicGroup] dtglink
    INNER JOIN [DatasetTopicGroupSet] as tgSet
        ON dtglink.topicGroupId = tgSet.topicGroupId;

    SELECT dtglink.deliveryId,
        dtglink.topicGroupId,
        tgSet.topicId
    FROM [DeliveryTopicGroup] dtglink
    INNER JOIN [DatasetTopicGroupSet] as tgSet
        ON dtglink.topicGroupId = tgSet.topicGroupId;

</select>

如果不执行两次相同的查询,我就无法获得相同的结果。我尝试在内部集合中再次使用相同的resultset,并尝试从内部集合中删除resultset属性,但在这两种情况下,整个topicgroups属性都为null。
有没有一种方法可以在resultmap中执行此操作,而不必执行相同的查询两次(我也不想为此使用typehandler。)

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题