码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2016-07-18 14:25:13      阅读:613      评论:0      收藏:0      [点我收藏+]

标签:

查询语句配置如下:

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














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

标签:

原文地址:http://www.cnblogs.com/countguo/p/5680191.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!