Lombok mybatis 多对一、一对多mapper.xml配置文件
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.
·
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>
更多推荐
已为社区贡献1条内容
所有评论(0)