mapper.xml文件如下
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.fangxinqian.order.zhendao.service.mapper.ProductMapper">
<resultMap type="cn.fangxinqian.order.zhendao.common.vos.ProductByGroupVO" id="productByGroupVO">
<collection column="id" property="simple" ofType="cn.fangxinqian.order.zhendao.common.vos.EnclosureVO"
select="getSimple">
</collection>
<collection column="id" property="active" ofType="cn.fangxinqian.order.zhendao.common.vos.EnclosureVO"
select="getActive">
<id column="id" property="id"/>
<result column="name" property="name"/>
<result column="enclosure_url" property="enclosureUrl"/>
</collection>
</resultMap>
<select id = "getProductByGroup" resultMap="productByGroupVO">
SELECT distinct p.id,
p.product_name,
p.create_time,
IFNULL(
(SELECT count(1)
FROM t_template_product
GROUP BY product_id
HAVING product_id = p.id),0) 'count'
FROM t_product p
LEFT JOIN t_template_product tp
ON p.id = tp.product_id
WHERE true
<if test="groupId != 0">
AND p.group_id = #{groupId}
</if>
<if test="key != null">
AND p.product_name like CONCAT(CONCAT('%',#{key},'%'))
</if>
</select>
<!-- 獲取未刪除的普通附件 -->
<select id="getSimple" resultType="cn.fangxinqian.order.zhendao.common.vos.EnclosureVO" parameterType="integer">
select id,`name`,enclosure_url from t_enclosure e where product_id = #{id} and enclosure_type = 0 and enclosure_status = 0
</select>
<!-- 獲取未刪除的活動附件 -->
<select id="getActive" resultType="cn.fangxinqian.order.zhendao.common.vos.EnclosureVO" parameterType="integer">
select id,`name`,enclosure_url from t_enclosure e where product_id = #{id} and enclosure_type = 1 and enclosure_status = 0
</select>
</mapper>
這是我的xml文件,如果不使用嵌套查詢的話,只執行getProductByGroup
是能正常返回的,但是加上了嵌套查詢以後,返回除了主查詢的主鍵沒有封裝進去,別的都正常,確定寫了get和set方法,後面在某個論壇上面找到一個解答,就是在寫resultMap的時候,主鍵不以id這樣的格式來寫<id column="id" property="id"/>
,而是以標籤來寫,在resultMap加上<result column="id" property="id"/>
這樣就能成功返回啦。
<resultMap type="cn.fangxinqian.order.zhendao.common.vos.ProductByGroupVO" id="productByGroupVO">
<result column="id" property="id"/>
<collection column="id" property="simple" ofType="cn.fangxinqian.order.zhendao.common.vos.EnclosureVO"
select="getSimple">
</collection>
<collection column="id" property="active" ofType="cn.fangxinqian.order.zhendao.common.vos.EnclosureVO"
select="getActive">
<id column="id" property="id"/>
<result column="name" property="name"/>
<result column="enclosure_url" property="enclosureUrl"/>
</collection>
</resultMap>