为了账号安全,请及时绑定邮箱和手机立即绑定

带有集合和关联问题的 Mybatis xml resultMap

带有集合和关联问题的 Mybatis xml resultMap

小怪兽爱吃肉 2023-04-19 16:22:24
我实际上是在测试 mybatis。我真的很喜欢,但是,我想更深入地了解 resultMap,但我遇到了问题。实际上我只想从数据库中获取一个计算机对象,它由多个屏幕和一个塔组成(我的代码的另一个对象)这是我的计算机 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>使用此代码,一切正常。BUTTTTTTTTT!我想做的是:<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>我不明白为什么第二个结果图不起作用。如果我有一对一的塔和一对一的屏幕,我可以有一个结果图,有两个关联,在它们中有一个 select="getmethod" 并且它完美地工作但是当我改变我的代码以有一个一对一的塔和一个多屏幕,我不能让 select="getmethod" 用于最后一个关联。它为一对一返回 null,但一对多工作(使用正确的 select 语句)。任何想法 ?也许这是不可能的?
查看完整描述

1 回答

?
德玛西亚99

TA贡献1770条经验 获得超3个赞

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


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

这就是结局。


查看完整回答
反对 回复 2023-04-19
  • 1 回答
  • 0 关注
  • 146 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信