1、多对一配置文件

<?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="com.atguigu.springcloud.dao.ProductDao">


	<select id="findAll" resultType="Product">
		select ID,Name,db_source from Product;
	</select>
	<insert id="addProduct" parameterType="Product">
		INSERT INTO Product(ID,Name,AddDate,Status,db_source) VALUES(#{ID},#{Name},#{AddDate},#{Status},DATABASE());
	</insert>

	<!--多对一-->
	<select id="findById" resultMap="Product_ProductType" parameterType="String">
		select * from Product where ID=#{value};
	</select>
	<resultMap id="Product_ProductType" type="com.atguigu.springcloud.entities.Product">
		<result property="ID" column="ID"></result>
		<result property="Name" column="Name"></result>
		<association property="ProductTypeEntity" column="ProductTypeID" javaType="com.atguigu.springcloud.entities.ProductType" select="getProductType" >
		</association>
	</resultMap>

	<select id="getProductType" parameterType="int" resultType="com.atguigu.springcloud.entities.ProductType">
		select * from ProductType where ID=#{ID}
	</select>
</mapper>
 

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="com.atguigu.springcloud.dao.ProductTypeDao">


	<select id="findAll" resultType="ProductType">
		select ID,Name,db_source from ProductType;
	</select>
	<insert id="addProject" parameterType="ProductType">
		INSERT INTO ProductType(ID,Name,AddDate,Status,db_source) VALUES(#{ID},#{Name},#{AddDate},#{Status},DATABASE());
	</insert>

	<!--一对多-->
	<select id="findById" resultMap="ProductType_Product" parameterType="String">
		select ID,Name,db_source from ProductType where ID=#{tid};
	</select>
	<resultMap id="ProductType_Product" type="com.atguigu.springcloud.entities.ProductType">
		<result property="ID" column="ID"></result>
		<collection property="ProductList"  javaType="ArrayList" select="GetProductTypeByfId" ofType="com.atguigu.springcloud.entities.Product" column="ID"></collection>
	</resultMap>
	<select id="GetProductTypeByfId" resultType="com.atguigu.springcloud.entities.Product">
		select * from Product where ProductTypeID=#{tid}
	</select>


</mapper>
 

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐