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

2022/1/28 6:06:55

本文主要是介绍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>


这篇关于SSM一对一映射、映射里带映射的Mapper.xml配置的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程