Browse Source

day05 end

wuheng 2 years ago
parent
commit
0ceafca0a0

+ 9 - 0
day05/src/main/java/com/lovecoding/mapper/AuthorBookMapper.java

@@ -0,0 +1,9 @@
+package com.lovecoding.mapper;
+
+import com.lovecoding.pojo.Author;
+
+import java.util.List;
+
+public interface AuthorBookMapper {
+    List<Author> selectAuthor();
+}

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

@@ -1,12 +1,18 @@
 package com.lovecoding.mapper;
 
 import com.lovecoding.pojo.Brand;
+import org.apache.ibatis.annotations.Insert;
 import org.apache.ibatis.annotations.Param;
+import org.apache.ibatis.annotations.Select;
 
 import java.util.List;
 
 public interface BrandMapper {
 
+    int insertBatch(List<Brand> list);
+
+    int deleteByids(int[] ids);
+
     int deleteById(int id);
 
     List<Brand> getBrandLIst();

+ 62 - 0
day05/src/main/java/com/lovecoding/pojo/Author.java

@@ -0,0 +1,62 @@
+package com.lovecoding.pojo;
+
+import java.util.List;
+
+public class Author {
+    private int id;
+    private String username;
+    private String password;
+    private String email;
+    private List<Book> books;
+
+    public List<Book> getBooks() {
+        return books;
+    }
+
+    public void setBooks(List<Book> books) {
+        this.books = books;
+    }
+
+    public int getId() {
+        return id;
+    }
+
+    public void setId(int id) {
+        this.id = id;
+    }
+
+    public String getUsername() {
+        return username;
+    }
+
+    public void setUsername(String username) {
+        this.username = username;
+    }
+
+    public String getPassword() {
+        return password;
+    }
+
+    public void setPassword(String password) {
+        this.password = password;
+    }
+
+    public String getEmail() {
+        return email;
+    }
+
+    public void setEmail(String email) {
+        this.email = email;
+    }
+
+    @Override
+    public String toString() {
+        return "Author{" +
+                "id=" + id +
+                ", username='" + username + '\'' +
+                ", password='" + password + '\'' +
+                ", email='" + email + '\'' +
+                ", books=" + books +
+                '}';
+    }
+}

+ 40 - 0
day05/src/main/java/com/lovecoding/pojo/Book.java

@@ -0,0 +1,40 @@
+package com.lovecoding.pojo;
+
+public class Book {
+    private Integer id;
+    private String authorId;
+    private String bookName;
+
+    public Integer getId() {
+        return id;
+    }
+
+    public void setId(Integer id) {
+        this.id = id;
+    }
+
+    public String getAuthorId() {
+        return authorId;
+    }
+
+    public void setAuthorId(String authorId) {
+        this.authorId = authorId;
+    }
+
+    public String getBookName() {
+        return bookName;
+    }
+
+    public void setBookName(String bookName) {
+        this.bookName = bookName;
+    }
+
+    @Override
+    public String toString() {
+        return "Author{" +
+                "id=" + id +
+                ", authorId='" + authorId + '\'' +
+                ", bookName='" + bookName + '\'' +
+                '}';
+    }
+}

+ 27 - 0
day05/src/main/resources/AuthorBookMapper.xml

@@ -0,0 +1,27 @@
+<?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.AuthorBookMapper">
+
+    <resultMap id="select_author_book" type="com.lovecoding.pojo.Author" >
+        <result column="username" property="username" />
+        <result column="password" property="password" />
+        <result column="email" property="email" />
+        <result column="id" property="id" />
+        <collection property="books" ofType="com.lovecoding.pojo.Book" select="selectBook" column="{authorId=id}" />
+    </resultMap>
+
+    <resultMap id="book" type="com.lovecoding.pojo.Book" >
+        <result column="author_id" property="authorId" />
+        <result column="book_name" property="bookName" />
+        <result column="id" property="id" />
+    </resultMap>
+
+    <select id="selectAuthor" resultMap="select_author_book">
+        SELECT * FROM `tb_author`;
+    </select>
+
+    <select id="selectBook" resultMap="book">
+        SELECT * FROM `tb_book` WHERE author_id = #{authorId}
+    </select>
+
+</mapper>

+ 20 - 0
day05/src/main/resources/BrandMapper.xml

@@ -62,4 +62,24 @@
             </if>
         </where>
     </select>
+
+
+    <delete id="deleteByids">
+         DELETE FROM tb_brand WHERE id IN
+         <foreach collection="array" item="id" separator="," open="(" close=")" >
+             #{id}
+         </foreach>
+    </delete>
+
+
+
+    <insert id="insertBatch">
+        INSERT INTO `tb_brand` (`brand_name`, `company_name`, `ordered`, `description`, `status`) VALUES
+            <foreach collection="list"  separator="," item="brand"  >
+                (#{brand.brandName},#{brand.companyName},#{brand.ordered},#{brand.description},#{brand.status})
+            </foreach>
+    </insert>
+
+
+
 </mapper>

+ 1 - 0
day05/src/main/resources/mybatis-config.xml

@@ -17,6 +17,7 @@
 
     <mappers>
         <mapper resource="BrandMapper.xml" />
+        <mapper resource="AuthorBookMapper.xml" />
     </mappers>
 
 </configuration>

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

@@ -0,0 +1,34 @@
+package com.lovecoding.test;
+
+import com.lovecoding.mapper.AuthorBookMapper;
+import com.lovecoding.mapper.BrandMapper;
+import com.lovecoding.pojo.Author;
+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;
+import java.util.List;
+
+public class CollectoionDemo {
+
+    @Test
+    public void t() 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();
+        AuthorBookMapper mapper = sqlSession.getMapper(AuthorBookMapper.class);
+
+        List<Author> authors = mapper.selectAuthor();
+
+        System.out.println( authors );
+
+    }
+
+}

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

@@ -27,4 +27,20 @@ public class DeleteBrand {
         System.out.println( i );
     }
 
+
+    @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[] ids = {5,8};
+        int i = mapper.deleteByids(ids);
+        sqlSession.commit(true);
+        System.out.println( i );
+    }
+
 }

+ 55 - 0
day05/src/test/java/com/lovecoding/test/InsertBrand.java

@@ -0,0 +1,55 @@
+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;
+import java.util.ArrayList;
+import java.util.HashMap;
+
+public class InsertBrand {
+
+    @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);
+
+        ArrayList<Brand> brands = new ArrayList<Brand>();
+
+        Brand brand1 = new Brand();
+        brand1.setBrandName("翠花酸菜");
+        brand1.setCompanyName("康师傅");
+        brand1.setDescription("好吃再来一桶");
+        brand1.setOrdered(22);
+        brand1.setStatus(1);
+
+        Brand brand2 = new Brand();
+        brand2.setBrandName("翠花羊肉卷");
+        brand2.setCompanyName("康师傅");
+        brand2.setDescription("好吃再来一桶");
+        brand2.setOrdered(22);
+        brand2.setStatus(1);
+
+        brands.add(brand1);
+        brands.add(brand2);
+
+        int i = mapper.insertBatch(brands);
+
+        sqlSession.commit(true);
+
+        System.out.println( i );
+
+    }
+
+}