SSM框架,mapper一对多映射,但是List只有一条数据

先上有问题的代码:

  <resultMap id="ItemResult" type="Item">
        <id property="id" column="id"/>
        <result property="order" column="order"/>
        <result property="height" column="height"/>
        <result property="weight" column="weight"/>
        <result property="base_experience" column="base_experience"/>
        <result property="is_default" column="is_default"/>
        <result property="item_species_id" column="item_species_id"/>
        <result property="name" column="name"/>
        <collection property="itemTypes" resultMap="itemType"/>
    </resultMap>
    <resultMap id="itemType" type="ItemType">
        <id property="id" column="id"/>
        <result property="slot" column="slot"/>
        <result property="item_id" column="item_id"/>
        <result property="type_id" column="type_id"/>
    </resultMap>
    <select id="selectItemByID" resultMap="ItemResult">
        SELECT
            A.id,
       A.order,
       A.height,
A.weight,
       A.base_experience,
       A.is_default,
       A.item_species_id,
       A.name B.id, B.slot, B.item_id, B.type_id FROM item A LEFT JOIN itemtype B on A.id = B.item_id WHERE A.id = #{id}
</select>

用A.id和B.item_id做关联,item对于itemtype是一对多关系,所以查询出来的list<itemType>应该是有多条的,但是事实上却只有一条。

用数据库查询也是多条,就很纳闷。

我找了很久,突然发现了问题所在:两张表的id列重名了,column都是"id",导致映射出错。

这个问题用as取个别称就解决了。

其实问题很简单,但浪费了我很多时间,记录下来,希望也能帮到其他人。

 

转载于:https://www.cnblogs.com/morn-yang/p/9191591.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值