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

查询语句配置如下:

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

查询出结果如下:

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

 

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

<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不久,记录在此,谨记于心~~














转载于:https://www.cnblogs.com/countguo/p/5680191.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值