Mybatis XML结果映射与收集和关联问题

时间:2019-07-25 15:58:19

标签: java xml spring mybatis

我实际上正在测试mybatis。我真的很喜欢,但是,我想更深入一点,并且在resultMap方面遇到问题。

实际上我只想从数据库中获得一个计算机对象,该计算机对象由多个屏幕和一个塔楼组成(我代码的另一个对象)

这是我的计算机结果图:

<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>

此请求:

<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>

使用此代码,一切正常。 BUTTTTTTTT! 我想做的是:

<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>

唯一的不同是:<association property="tower" column="towerid" select="getTowerbycomputerid"/>

我当然将请求更改为:

<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>

getTowerbycomputerid与xml匹配:

<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>

和resultMap:

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

我不明白为什么第二个结果图不起作用。 如果我有一个塔和一个屏幕 我可以有一个带有两个关联的resultmap,其中有一个select =“ getmethod” 它完美地工作 但是,当我将代码更改为具有一个塔楼和多个屏幕时,我不能让select =“ getmethod”用于最后的关联。 对于一对一,它返回null,但是对许多工作(使用正确的select语句)返回。

有什么主意吗? 也许不可能做到这一点?

THx:)

1 个答案:

答案 0 :(得分:0)

我回答了我的问题,@ave正确地对待了我。

他的评论:应该可以。嵌套的select getTowerbycomputerid似乎期望使用计算机ID,而您在关联中指定column =“ towerid”。不应该是“ id”吗?如果不是这个原因,请考虑提供像这样的完整示例。

这不完全是,但这可以帮助我找到解决方案。 有我的要求:

@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);

这是我的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>

现在是我的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>

现在一切正常。 在我选择塔之前:

<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>

到此结束。 Thx @ave:)