关联的MyBatis集合只返回一行 [英] MyBatis collection in association only return one row

查看:243
本文介绍了关联的MyBatis集合只返回一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要将MyBatis用于项目,并且我正在尝试使用我的关联中的集合定义resultMap。

I've to use MyBatis for a project and I'm trying to define a resultMap with a collection in my association.

我将简化我的问题,所以我们假设我的数据库有两个表:

I'll simplify my problem, so we suppose that my database have two table :

世界
[idWorld,worldName]

World [idWorld, worldName]

等级
[idLevel,
levelName,
idWorld ]

我想让每个世界和所有级别相关联。
目前,我设法得到这个结果图的所有结果:

I want to get each world and all levels associated. At the moment, I manage to get all result with this result map :

<resultMap type="World" id="WorldResultMap">
    <id property="idWorld" column="idWorld"/>
    <result property = "worldName" column="worldName"/>
        <collection property="level" javaType="java.util.ArrayList" ofType="Level">
            <result property="idLevel" column="idLevel" />
            <result property="levelName" column="levelName" />
        </collection>
</resultMap>

但是在映射之后我会得到这样的结果:

But after mapping I get a result like that :

  <worlds>
        <world>
            <idWorld>1</idWorld>
            <worldName> Wordl's name</worldName>
            <level> ... </level>
            <level> ... </level>
        </world>
         <world>
            <idWorld>2</idWorld>
            <worldName> Wordl's name</worldName>
            <level> ... </level>
            <level> ... </level>
            <level> ... </level>
        </world>
<worlds>

当我需要这样的东西时:

When I want something like that :

<worlds>
            <world>
                <idWorld>1</idWorld>
                <worldName> Wordl's name</worldName>
                <levels>
                        <level> ... </level>
                        <level> ... </level>
                 </levels> 
            </world>
             <world>
                <idWorld>2</idWorld>
                <worldName> Wordl's name</worldName>
                <levels>
                          <level> ... </level>
                          <level> ... </level>
                          <level> ... </level>
                </levels>
            </world>
    <worlds>

我正在尝试使用以下结果图获得此结果:

I'm trying to get this result with the following result map :

<resultMap type="World" id="WorldResultMap">
        <id property="idWorld" column="idWorld"/>
        <result property = "worldName" column="worldName"/>
            <association property="levels" javaType="Levels">
                 <collection property="level" javaType="java.util.ArrayList" ofType="Level">
                     <result property="idLevel" column="idLevel" />
                     <result property="levelName" column="levelName" />
                 </collection>
            </association>
    </resultMap>

我更改了我的实体,以便将级别列表转换为级别而不再是世界级别。
我的问题是我的上一个结果映射中每个世界只有一个级别具有相同的查询。
我想我错过了协会的使用。
我怎样才能将我的所有等级变为等级?

I change my entity in order to have the list of level into levels and not anymore into world. My problem is I get only one level for each world in my last result map with the same query. I think I miss understand the use of "association". How can I get all my level into levels ?

任何帮助将不胜感激,提前谢谢。

Any help will be appreciated, thanks in advance.

推荐答案

替换

<result property="idLevel" column="idLevel" />

<id property="idLevel" column="idLevel" />

那么你应该得到预期的结果。

then you are supposed to get the expected result.

引自 mybatis-doc#Result_Maps


非常重要:id元素在嵌套结果映射中起着非常重要的作用。您应始终指定一个或多个可用于唯一标识结果的属性。事实是,如果你把它放在外面,MyBatis仍然可以工作,但是性能成本很高。选择尽可能少的可以唯一标识结果的属性。主键是一个明显的选择(即使是复合键)。

Very Important: id elements play a very important role in Nested Result mapping. You should always specify one or more properties that can be used to uniquely identify the results. The truth is that MyBatis will still work if you leave it out, but at a severe performance cost. Choose as few properties as possible that can uniquely identify the result. The primary key is an obvious choice (even if composite).

如果 id 缺席,MyBatis仍然有用。似乎MyBatis会过滤掉它认为的重复行。

If id is absent, "MyBatis will still work". It seems that MyBatis will filter out duplicated rows it thinks.

这篇关于关联的MyBatis集合只返回一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆