wuheng il y a 2 ans
Parent
commit
e73e982815

+ 14 - 0
day05/src/main/java/com/lovecoding/mapper/BrandMapper.java

@@ -1,9 +1,23 @@
 package com.lovecoding.mapper;
 
 import com.lovecoding.pojo.Brand;
+import org.apache.ibatis.annotations.Param;
 
 import java.util.List;
 
 public interface BrandMapper {
+
+    int deleteById(int id);
+
     List<Brand> getBrandLIst();
+
+    int updateBrand( Brand beand );
+
+    int insertBrands( Brand brand );
+
+    int insertBrand(@Param("status") Integer status, @Param("ordered") Integer ordered, @Param("companyName") String companyName, @Param("brandName") String brandNam);
+
+    List<Brand> getBrandByCondition(
+            @Param("status") Integer status,  @Param("companyName") String companyName, @Param("brandName") String brandName );
+
 }

+ 60 - 2
day05/src/main/resources/BrandMapper.xml

@@ -1,7 +1,65 @@
 <?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.lovecoding.mapper.BrandMapper">
-    <select id="getBrandLIst" resultType="com.lovecoding.pojo.Brand">
-        SELECT id, brand_name AS brandName, company_name AS companyName, ordered, description, `status` FROM tb_brand;
+    <resultMap id="brand_resultmap" type="com.lovecoding.pojo.Brand">
+        <result property="brandName" column="brand_name"   />
+        <result property="companyName" column="company_name" />
+        <result property="description" column="company_name" />
+        <result property="id" column="id" />
+        <result property="ordered" column="ordered" />
+        <result property="status" column="status" />
+    </resultMap>
+    <sql id="brand_column" >
+        id, brand_name, company_name, ordered, description, `status`
+    </sql>
+    <insert id="insertBrand" useGeneratedKeys="true" keyProperty="id" >
+        INSERT INTO tb_brand ( brand_name, company_name, ordered, `status` )
+        VALUES ( #{brandName}, #{companyName},#{ordered},#{status} );
+    </insert>
+    <insert id="insertBrands" parameterType="com.lovecoding.pojo.Brand" useGeneratedKeys="true" keyProperty="id">
+        INSERT INTO tb_brand ( brand_name, company_name, ordered, `status` )
+        VALUES ( #{brandName}, #{companyName},#{ordered},#{status} );
+    </insert>
+    <update id="updateBrand">
+        UPDATE tb_brand
+        <set>
+            <if test="brandName != null" >
+                brand_name = #{brandName},
+            </if>
+            <if test="companyName != null" >
+                company_name = #{companyName},
+            </if>
+            <if test="ordered != null and ordered > 0" >
+                ordered = #{ordered},
+            </if>
+            <if test="status != null and status > 0" >
+                status = #{status},
+            </if>
+        </set>
+        WHERE id = #{id}
+    </update>
+    <delete id="deleteById">
+        DELETE FROM tb_brand WHERE id = #{id};
+    </delete>
+    <select id="getBrandLIst" resultMap="brand_resultmap" >
+            SELECT
+                <include refid="brand_column"></include>
+             FROM tb_brand;
+    </select>
+    <select id="getBrandByCondition" resultMap="brand_resultmap" >
+        SELECT
+        <include refid="brand_column"></include>
+        FROM tb_brand
+        <where>
+            <if test="brandName != null">
+                AND brand_name like concat( '%', #{brandName}, '%' )
+            </if>
+            <if test="companyName != null">
+                AND company_name like #{companyName}
+            </if>
+            <if test="status != null">
+                AND status <![CDATA[<]]> #{status}
+            </if>
+        </where>
     </select>
 </mapper>

+ 30 - 0
day05/src/test/java/com/lovecoding/test/DeleteBrand.java

@@ -0,0 +1,30 @@
+package com.lovecoding.test;
+
+import com.lovecoding.mapper.BrandMapper;
+import com.lovecoding.pojo.Brand;
+import org.apache.ibatis.io.Resources;
+import org.apache.ibatis.session.SqlSession;
+import org.apache.ibatis.session.SqlSessionFactory;
+import org.apache.ibatis.session.SqlSessionFactoryBuilder;
+import org.junit.Test;
+
+import java.io.IOException;
+import java.io.InputStream;
+
+public class DeleteBrand {
+
+    @Test
+    public void t1() throws IOException {
+        //mybatis 的配置档
+        String res = "mybatis-config.xml";
+        //使用 mybatis 的 Resources 把配置档转成数据流
+        InputStream resourceAsStream = Resources.getResourceAsStream(res);
+        SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resourceAsStream);
+        SqlSession sqlSession = build.openSession();
+        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
+        int i = mapper.deleteById(1);
+        sqlSession.commit(true);
+        System.out.println( i );
+    }
+
+}

+ 70 - 0
day05/src/test/java/com/lovecoding/test/SelectAll.java

@@ -27,8 +27,78 @@ public class SelectAll {
         BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
 
         List<Brand> brandLIst = mapper.getBrandLIst();
+        System.out.println( brandLIst );
+
+    }
+
+
+    @Test
+    public void t1() throws IOException {
+        //mybatis 的配置档
+        String res = "mybatis-config.xml";
+        //使用 mybatis 的 Resources 把配置档转成数据流
+        InputStream resourceAsStream = Resources.getResourceAsStream(res);
+        SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resourceAsStream);
+        SqlSession sqlSession = build.openSession();
+
+        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
+
+        String companyName = "%科技%";
+        String bandName = "牛肉面";
+        Integer state = 3;
+        List<Brand> brandLIst = mapper.getBrandByCondition( state, null,   bandName );
 
         System.out.println( brandLIst );
 
     }
+
+    @Test
+    public void t2() throws IOException {
+        //mybatis 的配置档
+        String res = "mybatis-config.xml";
+        //使用 mybatis 的 Resources 把配置档转成数据流
+        InputStream resourceAsStream = Resources.getResourceAsStream(res);
+        SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resourceAsStream);
+
+        SqlSession sqlSession = build.openSession();
+
+
+        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
+        int i = mapper.insertBrand(1, 1, "康师傅", "酸菜牛肉面");
+
+        sqlSession.commit(true);
+
+        System.out.println( i );
+
+
+    }
+
+
+    @Test
+    public void t3() throws IOException {
+        //mybatis 的配置档
+        String res = "mybatis-config.xml";
+        //使用 mybatis 的 Resources 把配置档转成数据流
+        InputStream resourceAsStream = Resources.getResourceAsStream(res);
+        SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resourceAsStream);
+
+        SqlSession sqlSession = build.openSession();
+
+        Brand brand = new Brand();
+        brand.setBrandName("翠花牛肉面");
+        brand.setCompanyName("康师傅");
+        brand.setDescription("好吃再来一桶");
+        brand.setOrdered(22);
+        brand.setStatus(1);
+
+        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
+        int i = mapper.insertBrands( brand );
+
+        sqlSession.commit(true);
+
+        System.out.println( brand );
+
+
+    }
+
 }

+ 34 - 0
day05/src/test/java/com/lovecoding/test/UpdateBrand.java

@@ -0,0 +1,34 @@
+package com.lovecoding.test;
+
+import com.lovecoding.mapper.BrandMapper;
+import com.lovecoding.pojo.Brand;
+import org.apache.ibatis.io.Resources;
+import org.apache.ibatis.session.SqlSession;
+import org.apache.ibatis.session.SqlSessionFactory;
+import org.apache.ibatis.session.SqlSessionFactoryBuilder;
+import org.junit.Test;
+
+import java.io.IOException;
+import java.io.InputStream;
+
+public class UpdateBrand {
+
+    @Test
+    public void t1() throws IOException {
+        //mybatis 的配置档
+        String res = "mybatis-config.xml";
+        //使用 mybatis 的 Resources 把配置档转成数据流
+        InputStream resourceAsStream = Resources.getResourceAsStream(res);
+        SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resourceAsStream);
+        SqlSession sqlSession = build.openSession();
+        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
+        Brand brand = new Brand();
+        brand.setBrandName("三只松鼠");
+        brand.setCompanyName("四只松鼠");
+        brand.setId(1);
+        int i = mapper.updateBrand(brand);
+        sqlSession.commit(true);
+        System.out.println( i );
+    }
+
+}