mybatis collection分页N+1问题

问题
使用mybatis联合查询,实体包含private List items,分页查询后结果集不正确,出现n+1 问题
xml:

 <resultMap type="com.test.domain.returnBean.BookMainDetailVo" id="ReturnMap">     
        <result property="code" column="code"/>
        <result property="number" column="number"/>
        <result property="billingDate" column="billing_date"/>
        <result property="type" column="type"/>
        <result property="no" column="no"/>
        <result property="name" column="name"/>
        <result property="phone" column="phone"/>
        <result property="bank" column="bank"/>
      

<collection property="items"    ofType="com.test.domain.BookDetail"
                   column="id" select="com.test.dao.BookDetailDao.getCheckItemInfo"/>

    </resultMap>
    
	<select id="selectByPage" parameterType="java.util.Map" resultMap="ReturnMap">
	        SELECT
	        m.id,m.code,m.number,DATE_FORMAT(m.billing_date,'%Y-%m-%d') AS billing_date ,m.type,m.no,m.name,m.phone,m.bank
	        FROM
	        book_main m
	        WHERE
	        m.no = #{no}
	        AND m.billing_date BETWEEN #{billingDateStart} AND #{billingDateEnd}
	        <if test="updateTimeStart != null and updateTimeEnd != null">
	            AND m.update_time BETWEEN #{updateTimeStart} AND #{updateTimeEnd}
	        </if>
	        ORDER BY billing_date DESC
	    </select>

问题原因

 <id property="id" column="id"/>
 
<collection property="items"    ofType="com.test..domain.BookDetail"
                   column="id" select="com.test.dao.BookDetailDao.getCheckItemInfo"/>

column=“id” 是需要关联查询的值 主体查询的时候一定要查出来
select=“com.test…dao.BookDetailDao.getCheckItemInfo” 对应要调用的子查询必须要有

这样问题就解决啦!!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值