问题
使用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” 对应要调用的子查询必须要有
这样问题就解决啦!!!