简体   繁体   English

Mybatis XML resultMap具有收集和关联问题

[英]Mybatis xml resultMap with collection and association issue

im actually testing mybatis. 我实际上正在测试mybatis。 I like really but, i want to go deeper and i have a problem, with resultMap. 我真的很喜欢,但是,我想更深入一点,我对resultMap有问题。

Actually i just want to get from database a computer object, which is composed of multiple screens and one tower (other object of my code) 实际上我只想从数据库中获得一个计算机对象,该计算机对象由多个屏幕和一个塔楼组成(我代码的另一个对象)

This is my resultMap for computer : 这是我的计算机结果图:

<resultMap type="entity.Computer" id="computer">
        <id column="id" property="id"/>
        <result column="name" property="name"/>
        <association property="tower" column="towerid" resultMap="towerResult" columnPrefix="t_"/>
        <collection ofType="entity.Screen" property="screen" javaType="ArrayList" resultMap="screenResult" columnPrefix="s_"/>
    </resultMap>

this the request : 这个请求:

<select id="getcomputerById" resultMap="computer">
        Select c.id, c.name, c.towerid, s.id as s_id, s.size as s_size, s.type as s_type, s.computer_id as s_computer_id, t.id as t_id, t.ram as t_ram, t.stockage as t_stockage from computer c inner join tower t on t.id = c.towerid left join screen s ON s.computer_id = c.id where c.id=#{computerId}
    </select>

With this code everything works fine. 使用此代码,一切正常。 BUTTTTTTTT ! BUTTTTTTTT! What i wanted to do is : 我想做的是:

<resultMap type="entity.Computer" id="computer">
        <id column="id" property="id"/>
        <result column="name" property="name"/>
        <association property="tower" column="towerid" select="getTowerbycomputerid"/>
        <collection ofType="entity.Screen" property="screen" javaType="ArrayList" resultMap="screenResult" columnPrefix="s_"/>
    </resultMap>

The only thing different is : <association property="tower" column="towerid" select="getTowerbycomputerid"/> 唯一不同的是: <association property="tower" column="towerid" select="getTowerbycomputerid"/>

Of course i change my request to : 当然,我将请求更改为:

<select id="getcomputerById" resultMap="computer">
        Select c.id, c.name, c.towerid, s.id as s_id, s.size as s_size, s.type as s_type, s.computer_id as s_computer_id from computer c inner join tower t on t.id = c.towerid left join screen s ON s.computer_id = c.id where c.id=#{computerId}
    </select>

There is the xml match the getTowerbycomputerid : xml匹配getTowerbycomputerid:

<select id="getTowerbycomputerid" resultMap="towerResult">
        Select t.id, t.ram, t.stockage from tower t inner join computer c on c.towerid=t.id where c.id=#{computerId}
    </select>

And the resultMap : 和resultMap:

<resultMap id="towerResult" type="entity.Tower">
        <id property="id" column="id"/>
        <result property="ram" column="ram"/>
        <result property="stockage" column="stockage"/>
    </resultMap>

I don't understand why the second resultmap don't work. 我不明白为什么第二个结果图不起作用。 If i have one-one tower and one-one Screen I can have a resultmap, with two association and in them a select="getmethod" And it work perfectly But when i change my code to have one-one tower and one-many Screen, i can't let select="getmethod" for the last association. 如果我有一个塔楼和一个屏幕,那么我可以有一个结果图,具有两个关联,并且在其中有一个select =“ getmethod”并且它运行良好,但是当我更改代码以拥有一个塔楼和一对多时屏幕上,我不能让select =“ getmethod”用于最后的关联。 It return null for the one-one, but the one-many work (with the right select statement). 对于一对一,它返回null,但是对于一对多,它返回正确的select语句。

Any idea ? 任何想法 ? Maybe it's not possible to do it? 也许不可能做到这一点?

THx :) 谢谢 :)

i answer my question, @ave put me on the right way. 我回答了我的问题,@ ave让我走对了路。

His comment : It should be possible. 他的评论:应该有可能。 The nested select getTowerbycomputerid seems to expect computer id, whereas you specify column="towerid" in the association. 嵌套的select getTowerbycomputerid似乎期望使用计算机ID,而您在关联中指定column =“ towerid”。 Shouldn't it be "id"? 不应该是“ id”吗? If that's not the reason, please consider providing a complete example like these. 如果不是这个原因,请考虑提供像这样的完整示例。

It's not exactly this but it helps me to find a solution. 这不完全是,但这可以帮助我找到解决方案。 There is my request : 有我的要求:

@Select("Select c.id, c.name, c.towerid, s.id as s_id, s.size as s_size, s.type as s_type, s.computer_id as s_computer_id from computer c left join screen s ON s.computer_id = c.id where c.id=#{computerId}")
@ResultMap("ComputerMapper.computer")
public Computer getcomputerById(@Param("computerId") Integer computerId);

This is my resultMap : 这是我的resultMap:

<resultMap type="entity.Computer" id="computer">
        <id column="id" property="id"/>
        <result column="name" property="name"/>
        <association property="tower" column="towerid" javaType="entity.Tower" select="getTowerbycomputerid"/>
        <collection ofType="entity.Screen" property="screen" javaType="ArrayList" resultMap="screenResult" columnPrefix="s_"/>
    </resultMap>

And now my resultMap and the request to get the tower : 现在我的resultMap和获取塔的请求:

<resultMap id="towerResult" type="entity.Tower">
        <id property="id" column="id"/>
        <result property="ram" column="ram"/>
        <result property="stockage" column="stockage"/>
    </resultMap>
    <select id="getTowerbycomputerid" resultMap="towerResult">
        Select t.id, t.ram, t.stockage from tower t where t.id = #{towerid}
    </select>

And now everything works fine. 现在一切正常。 Before i got this to select the tower: 在我选择这座塔之前:

<select id="getTowerbycomputerid" resultMap="towerResult">
            Select t.id, t.ram, t.stockage from tower t inner join computer c on c.towerid = t.id where c.id = #{computerId}
        </select>

This is the end. 这就是结局。 Thx @ave :) Thx @ave :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM