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取个别称就解决了。
其实问题很简单,但浪费了我很多时间,记录下来,希望也能帮到其他人。