其他分享
首页 > 其他分享> > SSM一对一映射、映射里带映射的Mapper.xml配置

SSM一对一映射、映射里带映射的Mapper.xml配置

作者:互联网

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