Mybatis使用ResultMap中嵌套collection标签主键id不封装的问题

mapper.xml文件如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<?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这样的格式来写,而是以标签来写,在resultMap加上 这样就能成功返回啦。

1
2
3
4
5
6
7
8
9
10
11
12
    <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>