标签:xml Mapper bf name 映射 image category id select
1.需求
表1 category 是标签表,包含主标签和子标签,通过自关联查询得到完整结果
表2 是商品表
2.配置完成的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="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>
标签:xml,Mapper,bf,name,映射,image,category,id,select 来源: https://www.cnblogs.com/roud/p/15851756.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。