并非MyBatis返回的所有结果

并非MyBatis返回的所有结果

本文介绍了并非MyBatis返回的所有结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个查询,如果手动执行,则返回4个结果,但是如果通过 MyBatis 3.7.8 执行,则仅返回3个结果(尽管在日志中显示找到了4个结果").因此,似乎某个地方迷路了,我也不知道为什么.

I have a query that returns 4 results if manually executed, but only 3 results if executed via MyBatis 3.7.8 (although it says '4 results found' in the logs). So it seems one gets lost somewhere and I don't know why.

我怀疑这与我的resultmap中不存在唯一标识符有关,但是在我的表中允许重复并且这些也应该返回.

I suspect it has something to do with that there doesn't exist an unique identifier in my resultmap, but in my table duplicates are allowed and these should also be returned.

结果图:

<resultMap type="my.package.Info" id="InfoMap">
    <result property="taskownerId"          column="v_taskowner_id"/>
    <result property="shipmentUnitId"       column="v_shipmentunit_id"/>
    <result property="sampleId"             column="v_sample_id"/>
     <association property="spread" column="v_spread_id" javaType="Spread">
        <id property="id" column="v_spread_id" />
        <result property="name" column="v_spread_name" />
    </association>
</resultMap>

选择:

<select id="findInfos" resultMap="InfoMap" parameterType="map">
    SELECT
        v.taskowner           v_taskowner,
        v.shipmentunit_id     v_shipmentunit_id,
        v.sample_id           v_sample_id,
        v.spread_id           v_spread_id,
        v.spread_name         v_spread_name
    FROM
        view_infos  v
    <where>
    rownum &lt;= 10000
    AND v.sample_id = #{sampleId}
    <if test="taskownerId != null">
        AND v.taskowner = #{taskownerId}
    </if>
    </where>
</select>

推荐答案

与完整的解决方案相比,这更多的是替代方法:

This is more of a workaround than a complete solution:

如果结果是嵌套结果图(带有嵌套对象的对象),则MyBatis需要为该映射中的每个返回对象提供一个ID,并且由于缺少<id>标签,MyBatis可以做到"不能区分那些对象.我目前正在解决此问题,因为在类中删除了嵌套对象,因为它们非常简单,因此冗余代码不会脱颖而出(它们只包含一个id和一个名称).

If the result is a nested result map (an object with nested objects) MyBatis needs an ID for every returned object in that map and because of the lack of an <id> tag MyBatis can't distinguish between those objects. I currently worked around this with removing the nested objects in my class because they were simple enough so that redudant code wouldn't stand out (they only contain an id and a name).

现在,我获得了该查询的预期结果.

Now I get the expected amount of results for this query.

如前所述,这只是一种解决方法,没有解决方案.对于更复杂的嵌套对象,不建议.

As stated before, this is only a workaround, no solution. This is NOT ADVISABLE for more complex nested objects.

这篇关于并非MyBatis返回的所有结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 09:26