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不久,記錄在此,謹記於心~~















免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM