我在MyBatis中遇到一对多的关系。我的数据库中有一个用户,可以有很多地址。用户和地址表使用id链接。但是MyBatis只填充第一个地址。奇怪的是,当我单独运行mySQL代码时,它运行正常,所以我猜测它与我的MyBatis映射有关。
<select id="fetchUserAddressList" parameterType="string" resultMap="userAddressResultMap">
SELECT user.user_name, address.id, address.address_value
FROM user
LEFT OUTER JOIN address
ON user.id = address.id
WHERE user.id = #{userId}
</select>
<resultMap id="userAddressResultMap" type="com.bean.User" >
<id property="userName" column="user_name" />
<collection property="addressList" column="id" notNullColumn="id"
ofType="com.bean.Address"
resultMap="addressResultMap" />
</resultMap>
<resultMap id="addressResultMap" type="com.bean.AddressBean" >
<id property="userId" column="id" />
<result property="userAddress" column="address_value" />
</resultMap>
有人有任何想法吗?
答案 0 :(得分:2)
<resultMap id="addressResultMap" type="com.bean.AddressBean" >
<result property="userId" column="id" />
<result property="userAddress" column="address_value" />
</resultMap>