1. 程式人生 > 實用技巧 >SpringMVC-08-Mybatis層編寫

SpringMVC-08-Mybatis層編寫

Mybatis層編寫

  1. 資料庫配置檔案 database.properties
driver=com.mysql.jdbc.Driver
#如果使用的是mysql 8.0+ 需要增加一個時區的配置 &serverTimezone=Asia/Shanghai
url=jdbc:mysql://localhost:3306/mybatis?useSSL=true&useUnicode=true&characterEncoding=UTF-8
username=huba
password=123456
  1. IDEA關聯資料庫

  2. 編寫Mybatis核心配置檔案

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE configuration
            PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
            "http://mybatis.org/dtd/mybatis-3-config.dtd">
    <!--核心配置檔案-->
    <configuration>
        <!--配置資料來源,現在交給Spring完成-->
    
        <!--別名-->
        <typeAliases>
            <package name="com.kuang.pojo"/>
        </typeAliases>
    
        <!--設定-->
    
        <!--繫結mapper-->
        <mappers>
            <mapper class="com.kuang.dao.BookMapper"/>
        </mappers>
    </configuration>
    
  3. 實體類 Books

    使用lombok!

    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public class Books {
        private int bookID;
        private String bookName;
        private int bookCounts;
        private String detail;
    }
    
  4. dao層 BookMapper介面BookMapper.xml

    public interface BookMapper {
        //增加一本書
        int addBook(Books books);
    
        //刪除一本書
        int deleteBookById(@Param("bookId") int id);
    
        //更新一本書
        int updateBook(Books books);
    
        //查詢一本書
        Books queryBookById(@Param("bookID") int id);
    
        //查詢全部的書
        List<Books> queryAllBook();
    }
    
    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE mapper
            PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
            "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    <mapper namespace="com.kuang.dao.BookMapper">
        <insert id="addBook" parameterType="Books">
            insert into ssmbuild.books (bookName, bookCounts, detail)
            values (#{bookName},#{bookCounts},#{detail});
        </insert>
    
        <delete id="deleteBookById" parameterType="int">
            delete from ssmbuild.books where bookID=#{bookId}
        </delete>
    
        <update id="updateBook" parameterType="Books">
            update ssmbuild.books
            set bookName = #{bookName},bookCounts=#{bookCounts},detail=#{detail}
            where bookID=#{bookID};
        </update>
    
        <select id="queryBookById" resultType="Books">
            select * from ssmbuild.books
            where bookID=#{bookID}
        </select>
    
        <select id="queryAllBook" resultType="Books">
            select * from ssmbuild.books
        </select>
    </mapper>
    

    在mybatis核心配置檔案中繫結mapper

    <!--繫結mapper-->
    <mappers>
        <mapper class="com.kuang.dao.BookMapper"/>
    </mappers>
    
  5. service層 BookService介面BookServiceImpl

    public interface BookService {
        //增加一本書
        int addBook(Books books);
    
        //刪除一本書
        int deleteBookById(int id);
    
        //更新一本書
        int updateBook(Books books);
    
        //查詢一本書
        Books queryBookById(int id);
    
        //查詢全部的書
        List<Books> queryAllBook();
    
    }
    
    public class BookServiceImpl implements BookService{
    
        private BookMapper bookMapper;
    
        public void setBookMapper(BookMapper bookMapper) {
            this.bookMapper = bookMapper;
        }
    
        //serice層呼叫dao層
        public int addBook(Books books) {
            return bookMapper.addBook(books);
        }
    
        public int deleteBookById(int id) {
            return bookMapper.deleteBookById(id);
        }
    
        public int updateBook(Books books) {
            return bookMapper.updateBook(books);
        }
    
        public Books queryBookById(int id) {
            return bookMapper.queryBookById(id);
        }
    
        public List<Books> queryAllBook() {
            return bookMapper.queryAllBook();
        }
    }