mybatis 配置返回集合collection时只有一条记录

时间:2022-01-07 03:37:18

查询语句配置如下:

<select id="selectCustomerList" resultMap="CustomerDtoMap" parameterType="map">
select * from
(
select <include refid="AliasCustomerColumnList"/>
from customer c
) c
left join display_venue_reserve dvr
on c.c_customer_id = dvr.customer_id where 1=1
<if test="customerMgrId != null">
and c.c_customer_mgr_id = #{customerMgrId}
</if>
<if test="status != null">
and dvr.status = #{status}
</if>
</select>

resultMap配置如下

<resultMap id="CustomerDtoMap" type="com.al.ec.showroom.dto.customer.CustomerDto" extends="AliasBaseResultMap">
<collection property="displayVenueReserveDtoList" ofType="com.al.ec.showroom.dto.display.DisplayVenueReserveDto">
<association property="displayVenueReserve" column="VENUE_RESERVE_ID" javaType="com.al.ec.showroom.model.display.DisplayVenueReserve"
resultMap="com.al.ec.showroom.mapper.display.DisplayVenueReserveMapper.BaseResultMap">
</association>
</collection>
</resultMap>

查询出结果如下:

mybatis 配置返回集合collection时只有一条记录

只有一条记录,但是数据库直接执行语句是有多条记录的

经过很多轮的改代码研究,发现是因为

<collection property="displayVenueReserveDtoList" ofType="com.al.ec.showroom.dto.display.DisplayVenueReserveDto">
<association property="displayVenueReserve" column="VENUE_RESERVE_ID" javaType="com.al.ec.showroom.model.display.DisplayVenueReserve"
resultMap="com.al.ec.showroom.mapper.display.DisplayVenueReserveMapper.BaseResultMap">
</association>
</collection>
需要在collection节点下添加
<id column="dvr_VENUE_RESERVE_ID" property="venueReserveId" jdbcType="DECIMAL"></id>

更改后代码如下

<resultMap id="CustomerDtoMap" type="com.al.ec.showroom.dto.customer.CustomerDto" extends="AliasBaseResultMap">
<collection property="displayVenueReserveDtoList" ofType="com.al.ec.showroom.dto.display.DisplayVenueReserveDto">
<id column="VENUE_RESERVE_ID" property="venueReserveId" jdbcType="DECIMAL"></id>
<association property="displayVenueReserve" column="VENUE_RESERVE_ID" javaType="com.al.ec.showroom.model.display.DisplayVenueReserve"
resultMap="com.al.ec.showroom.mapper.display.DisplayVenueReserveMapper.AliasBaseResultMap">
</association>
</collection>
</resultMap>

一直以为<association>节点有column属性就可以作为主键就可以了。。。。原来是需要在<collection>下添加id子节点

查询结果如下

mybatis 配置返回集合collection时只有一条记录

但是对于这样配置的原理还不是很了解,如果知道的大神希望能在评论里给出回复,感谢

小白,刚接触mybatis不久,记录在此,谨记于心~~