Spring Boot 框架下使用MyBatis訪問數據庫之基於XML配置的方式

    MyBatis 是一款優秀的持久層框架,它支持定製化 SQL、存儲過程以及高級映射。MyBatis 避免了幾乎全部的 JDBC 代碼和手動設置參數以及獲取結果集。MyBatis 可使用簡單的 XML 或註解來配置和映射原生信息,將接口和 Java 的 POJOs(Plain Ordinary Java Object,普通的 Java對象)映射成數據庫中的記錄。java

   在Spring Boot 框架下,MyBatis官方提供了一套自動化的配置方案,讓Mybatis能夠作到開箱即用。mysql

  一、新建y一個Spring Boot項目,pom.xml文件中添加對應的引用web

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
 4     <modelVersion>4.0.0</modelVersion>
 5     <parent>
 6         <groupId>org.springframework.boot</groupId>
 7         <artifactId>spring-boot-starter-parent</artifactId>
 8         <version>2.1.8.RELEASE</version>
 9         <relativePath/> <!-- lookup parent from repository -->
10     </parent>
11     <groupId>cn.weisoft</groupId>
12     <artifactId>test-web</artifactId>
13     <version>0.0.1-SNAPSHOT</version>
14     <name>test-web</name>
15     <description>Demo project for Spring Boot</description>
16 
17     <properties>
18         <java.version>1.8</java.version>
19     </properties>
20 
21     <dependencies>
22         <dependency>
23             <groupId>org.springframework.boot</groupId>
24             <artifactId>spring-boot-starter</artifactId>
25         </dependency>
26         <dependency>
27             <groupId>org.springframework.boot</groupId>
28             <artifactId>spring-boot-starter-web</artifactId>
29         </dependency>
30         <dependency>
31             <groupId>org.springframework.boot</groupId>
32             <artifactId>spring-boot-starter-test</artifactId>
33             <scope>test</scope>
34         </dependency>
35         <dependency>
36             <groupId>org.springframework.boot</groupId>
37             <artifactId>spring-boot-starter-jdbc</artifactId>
38         </dependency>
39         <dependency>
40             <groupId>mysql</groupId>
41             <artifactId>mysql-connector-java</artifactId>
42             <scope>runtime</scope>
43         </dependency>
44         <dependency>
45             <groupId>org.mybatis.spring.boot</groupId>
46             <artifactId>mybatis-spring-boot-starter</artifactId>
47             <version>2.1.0</version>
48         </dependency>
49         <dependency>
50             <groupId>com.alibaba</groupId>
51             <artifactId>druid</artifactId>
52             <version>1.1.20</version>
53         </dependency>
54     </dependencies>
55 
56     <build>
57         <plugins>
58             <plugin>
59                 <groupId>org.springframework.boot</groupId>
60                 <artifactId>spring-boot-maven-plugin</artifactId>
61             </plugin>
62         </plugins>
63           <resources>  
64             <resource>  
65                 <directory>src/main/java</directory>  
66                 <includes>  
67                         <include>**/*.properties</include>  
68                         <include>**/*.xml</include>  
69                     </includes>  
70                 <filtering>false</filtering>  
71             </resource>  
72            </resources>  
73     </build>
74 
75 </project>
pom.xml

 

  二、建立數據庫表及存儲過程spring

 1 SET NAMES utf8mb4;
 2 SET FOREIGN_KEY_CHECKS = 0;
 3 
 4 -- ----------------------------
 5 -- Table structure for book
 6 -- ----------------------------
 7 DROP TABLE IF EXISTS `book`;
 8 CREATE TABLE `book`  (
 9   `id` int(11) NOT NULL AUTO_INCREMENT,
10   `name` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL,
11   `author` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL,
12   `publishDate` datetime(0) NULL DEFAULT CURRENT_TIMESTAMP(0),
13   PRIMARY KEY (`id`) USING BTREE
14 ) ENGINE = InnoDB AUTO_INCREMENT = 81 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci ROW_FORMAT = Dynamic;
15 
16 SET FOREIGN_KEY_CHECKS = 1;
17 
18 
19 
20 drop procedure  if exists p_get_book_by_id;
21 DELIMITER //
22 CREATE PROCEDURE p_get_book_by_id(IN v_id integer)
23 BEGIN
24     select *
25     from book
26     where id = v_id;
27 
28 END
29 
30 DROP PROCEDURE IF EXISTS p_ins_book;
31 
32 DELIMITER //
33 CREATE PROCEDURE p_ins_book (
34     OUT v_id INTEGER,
35     IN v_name VARCHAR ( 100 ),
36     v_author VARCHAR (50),
37     v_publishDate datetime    
38     )
39     BEGIN
40     INSERT INTO book ( NAME,author,publishDate)
41     VALUES
42         ( v_name, v_author,v_publishDate);
43     
44     SET v_id = LAST_INSERT_ID();
45     
46 END //
47 
48 delimiter ;
49 
50 call p_ins_book(@v_id,'c#','wilson.fu',now());
View Code

三、創建對應 實體類sql

 1 package cn.weisoft.testweb.entity;
 2 
 3 
 4 import java.util.Date;
 5 
 6 public class Book {
 7     private Integer id;
 8     private String name;
 9 
10     private String author;
11 
12     private Date publishDate;
13 
14     public Integer getId() {
15         return id;
16     }
17 
18     public void setId(Integer id) {
19         this.id = id;
20     }
21 
22     public String getName() {
23         return name;
24     }
25 
26     public void setName(String name) {
27         this.name = name;
28     }
29 
30     public String getAuthor() {
31         return author;
32     }
33 
34     public void setAuthor(String author) {
35         this.author = author;
36     }
37 
38     public Date getPublishDate() {
39         return publishDate;
40     }
41 
42     public void setPublishDate(Date publishDate) {
43         this.publishDate = publishDate;
44     }
45 }
View Code

四、創建Mapper類及配置文件數據庫

package cn.weisoft.testweb.mapper;

import cn.weisoft.testweb.entity.Book;
import org.apache.ibatis.annotations.Mapper;

@Mapper
public interface BookMapper {
    Book getBookById(int id);

    int addBook(Book book);

    int addBookByProc(Book book);

    Book selectBookById(int id);
}
BookMapper
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 3 <mapper namespace="cn.weisoft.testweb.mapper.BookMapper">
 4     <select id="getBookById" parameterType="int" resultType="cn.weisoft.testweb.entity.Book">
 5         select * from book where id=#{id}
 6     </select>
 7     <insert id="addBook" parameterType="cn.weisoft.testweb.entity.Book">
 8         INSERT into book(name,author,publishDate) VALUES (#{name},#{author},#{publishDate})
 9     </insert>
10     <insert id="addBookByProc" parameterType="cn.weisoft.testweb.entity.Book" statementType="CALLABLE">
11         {call p_ins_book(
12         #{id,mode=OUT,jdbcType=INTEGER},
13         #{name,mode=IN},
14         #{author,mode=IN},
15         #{publishDate,mode=IN}
16         )}
17     </insert>
18     <select id="selectBookById" parameterType="int" resultType="cn.weisoft.testweb.entity.Book" statementType="CALLABLE">
19       {call p_get_book_by_id(
20       #{id,mode=IN}
21       )}
22     </select>
23 </mapper>
BookMapper.xml

配置文件中包括經過SQL語句及存儲過程訪問數據庫的兩種方式apache

五、創建Service類c#

package cn.weisoft.testweb.serivce;

import cn.weisoft.testweb.entity.Book;
import cn.weisoft.testweb.mapper.BookMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class BookService2 {
    @Autowired
    BookMapper bookMapper;

    public Book getBookById(int id){
        return bookMapper.getBookById(id);
    }

    public int addBook(Book book){
        return bookMapper.addBook(book);
    }

    public int addBookByProc(Book book){
        return bookMapper.addBookByProc(book);
    }


    public Book selectBookById(int id){
        return bookMapper.selectBookById(id);
    }
}
View Code

 

六、添加Controller測試類mybatis

 1 package cn.weisoft.testweb.controller;
 2 
 3 import cn.weisoft.testweb.entity.Book;
 4 import cn.weisoft.testweb.serivce.BookService;
 5 import org.springframework.beans.factory.annotation.Autowired;
 6 import org.springframework.web.bind.annotation.GetMapping;
 7 import org.springframework.web.bind.annotation.RestController;
 8 
 9 import java.util.Date;
10 
11 @RestController
12 public class BookController {
13     @Autowired
14     BookService bookService;
15 
16     @GetMapping("/book/getBookById")
17     public Book getBookById(int id){
18         return bookService.getBookById(id);
19     }
20 
21 
22     @GetMapping("/book/selectBookById")
23     public Book selectBookById(int id){
24         return bookService.selectBookById(id);
25     }
26 
27 
28     @GetMapping("/book/addBook")
29     public  int addBook(){
30         Book book =new Book();
31         book.setName("C#入門。");
32         book.setAuthor("wilson.fu88");
33         book.setPublishDate(new Date());
34 
35         return bookService.addBook(book);
36     }
37 
38     @GetMapping("/book/addBookByProc")
39     public Book addBookByProc(){
40         Book book =new Book();
41         book.setName("Spring Boot 入門。");
42         book.setAuthor("wilson.fu1982");
43         book.setPublishDate(new Date());
44 
45          bookService.addBookByProc(book);
46          return book;
47     }
48 }
BookController

 

基於XML配置的方式,默認BookMapper.xml不會自動複製到target對應的文件夾,須要在pom.xml中加上以下配置,這個不是推薦的方式,由於spring boot自己講究的是0配置,後面將會寫一遍基於註解方式實現上面的數據訪問app

<build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
          <resources>  
            <resource>  
                <directory>src/main/java</directory>  
                <includes>  
                        <include>**/*.properties</include>  
                        <include>**/*.xml</include>  
                    </includes>  
                <filtering>false</filtering>  
            </resource>  
           </resources>  
    </build>
pom-build

七、設置應用的配置文件,裏面主要包含數據庫鏈接,日誌配置等信息

 1 spring:
 2   datasource:
 3     type: com.alibaba.druid.pool.DruidDataSource
 4     url: jdbc:mysql://localhost/testdb?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&failOverReadOnly=false&maxReconnects=15000&serverTimezone=Asia/Shanghai
 5     username: root
 6     password: root
 7 logging:
 8   path: E:/temp/20190906/mybatis-study
 9   file: test-web.log
10   level:
11     cn:
12       weisoft:
13         mybatisstudy:
14           mapper: debug
application.yml

 

八、讓Mybatis的SQL語句輸出到日誌中配置,在resources文件夾添加logback.xml文件,配置以下

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <configuration debug="false">
 3     <!--定義日誌文件的存儲地址 勿在 LogBack 的配置中使用相對路徑-->
 4     <property name="LOG_HOME" value="E:/temp/20190906/test-web" />
 5     <!-- 控制檯輸出 -->
 6     <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
 7         <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
 8             <!--格式化輸出:%d表示日期,%thread表示線程名,%-5level:級別從左顯示5個字符寬度%msg:日誌消息,%n是換行符-->
 9             <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>
10         </encoder>
11     </appender>
12     <!-- 按照天天生成日誌文件 -->
13     <appender name="FILE"  class="ch.qos.logback.core.rolling.RollingFileAppender">
14         <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
15             <!--日誌文件輸出的文件名-->
16             <FileNamePattern>${LOG_HOME}/TestWeb.log.%d{yyyy-MM-dd}.log</FileNamePattern>
17             <!--日誌文件保留天數-->
18             <MaxHistory>30</MaxHistory>
19         </rollingPolicy>
20         <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
21             <!--格式化輸出:%d表示日期,%thread表示線程名,%-5level:級別從左顯示5個字符寬度%msg:日誌消息,%n是換行符-->
22             <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>
23         </encoder>
24         <!--日誌文件最大的大小-->
25         <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
26             <MaxFileSize>10MB</MaxFileSize>
27         </triggeringPolicy>
28     </appender>
29 
30 
31     <!--myibatis log configure-->
32     <logger name="com.apache.ibatis" level="DEBUG"/>
33     <logger name="java.sql.Connection" level="DEBUG"/>
34     <logger name="java.sql.Statement" level="DEBUG"/>
35     <logger name="java.sql.PreparedStatement" level="DEBUG"/>
36 
37     <!-- 日誌輸出級別 -->
38     <root level="DEBUG">
39         <appender-ref ref="STDOUT" />
40         <appender-ref ref="FILE" />
41     </root>
42     <!--日誌異步到數據庫 -->
43     <!--<appender name="DB" class="ch.qos.logback.classic.db.DBAppender">-->
44     <!--<!–日誌異步到數據庫 –>-->
45     <!--<connectionSource class="ch.qos.logback.core.db.DriverManagerConnectionSource">-->
46     <!--<!–鏈接池 –>-->
47     <!--<dataSource class="com.mchange.v2.c3p0.ComboPooledDataSource">-->
48     <!--<driverClass>com.mysql.jdbc.Driver</driverClass>-->
49     <!--<url>jdbc:mysql://127.0.0.1:3306/databaseName</url>-->
50     <!--<user>root</user>-->
51     <!--<password>root</password>-->
52     <!--</dataSource>-->
53     <!--</connectionSource>-->
54     <!--</appender>-->
55 </configuration>
logback.xml
相關文章
相關標籤/搜索