表1 category 是标签表,包含主标签和子标签,通过自关联查询得到完整结果
表2 是商品表
<?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="top.roud.buyfresh.dao.ProductMapper"> <resultMap id="CategoryResult" type="Category"> <id column="sub_id" property="id"/> <result column="sub_name" property="name"/> </resultMap> <resultMap id="CategoryWithSubResult" type="Category"> <id column="id" property="id"/> <result column="name" property="name"/> <collection property="categories" resultMap="CategoryResult"/> </resultMap> <resultMap type="Product" id="ProductResult"> <id column="id" property="id" /> <result column="name" property="name" /> <result column="price" property="price" /> <result column="producingArea" property="producingArea" /> <result column="groundingDate" property="groundingDate" /> <result column="goodsCode" property="goodsCode"/> <result column="description" property="description" /> <result column="image" property="image" /> <result column="introduction_image" property="introduction_image"/> <association property="category" column="category_id" select="findCategoryById" /> </resultMap> <select id="findCategoryById" resultMap="CategoryWithSubResult"> select t3.id, t3.name,t3.sub_id,t3.sub_name from( select t1.id id,t1.name name,t2.id sub_id,t2.name sub_name from bf_category t1,bf_category t2 where t1.parent_id=0 and t1.id=t2.parent_id) t3 where id=#{agr0} </select> <select id="findProductById" resultType="Product" parameterType="int"> select id,name,price,producingArea,groundingDate,goodsCode,description,image,introduction_image,category_id from bf_product where id=#{arg0} </select> <select id="findAllProducts" resultMap="ProductResult"> select id,name,price,producingArea,groundingDate,goodsCode,description,image,introduction_image,category_id from bf_product </select> <select id="findByCategoryId" resultMap="ProductResult" parameterType="int"> select id,name,price,producingArea,groundingDate,goodsCode,description,image,introduction_image,category_id from bf_product where category_id=#{arg0} </select> </mapper>