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文件,如果不使用嵌套查询的话,只执行
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> |