1. 程式人生 > >SpringBoot2.x整合MyBatis

SpringBoot2.x整合MyBatis

SpringBoot Mybatis MySQL 集成 Spring

首先在IDEA中創建一個SpringBoot工程:
技術分享圖片
技術分享圖片

選擇一些基本的包:
技術分享圖片

完成創建:
技術分享圖片

工程創建成功後,補充pom.xml文件中的依賴,完整的依賴如下:

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.2</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!-- alibaba的druid數據庫連接池 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.9</version>
        </dependency>
    </dependencies>

然後使用mybatis-generator插件來自動生成相關的文件及代碼,完成後工程目錄結構如下:
技術分享圖片

註:如不懂如何使用mybatis-generator插件的話,可參考我另外一篇文章:Mybatis-Generator插件的使用與Spring集成Mybatis的配置

生成dao層相關的代碼及文件後,則修改SpringBoot配置文件的格式為.yml,並編輯配置內容如下:

server:
  port: 8080

spring:
    datasource:
        name: mybatis_test
        #使用druid連接池
        type: com.alibaba.druid.pool.DruidDataSource
        #druid相關配置
        druid:
          #監控統計攔截的filters
          filters: stat
          driver-class-name: com.mysql.jdbc.Driver
          #配置基本屬性
          url: jdbc:mysql:///school?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true
          username: root
          password: password
          #配置初始化大小/最小/最大
          initial-size: 1
          min-idle: 1
          max-active: 20
          #獲取連接等待超時時間
          max-wait: 60000
          #間隔多久進行一次檢測,檢測需要關閉的空閑連接
          time-between-eviction-runs-millis: 60000
          #一個連接在池中最小生存的時間
          min-evictable-idle-time-millis: 300000
          validation-query: SELECT ‘x‘
          test-while-idle: true
          test-on-borrow: false
          test-on-return: false
          #打開PSCache,並指定每個連接上PSCache的大小。oracle設為true,mysql設為false。分庫分表較多推薦設置為false
          pool-prepared-statements: false
          max-pool-prepared-statement-per-connection-size: 20

mybatis:
  # 映射文件所在路徑
  mapper-locations: classpath:mappers/*.xml
  # pojo類所在包路徑
  type-aliases-package: org.zero01.example.demo.pojo

編輯配置文件完成後,打開SpringBoot的啟動類,加上@MapperScan註解,指向dao層接口所在的包路徑:

package com.example.demo;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@MapperScan("com.example.demo.dao")
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

接下來我們就可以開始進行測試了,首先打開ClsMapper接口,按 Shift + Ctrl + T 快捷鍵即可快速創建該接口的測試用例,如下:
技術分享圖片

選擇需要測試的方法,我這裏作為演示就只測試一下查詢方法:
技術分享圖片

生成了測試用例的基本代碼後,我們還需要自己編寫相關的代碼,如下:

package com.example.demo.dao;

import com.example.demo.pojo.Cls;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import static org.junit.Assert.*;

@RunWith(SpringRunner.class)
@SpringBootTest
public class ClsMapperTest {

    @Autowired
    private ClsMapper clsMapper;

    @Test
    public void selectByPrimaryKey() {
        Cls cls = clsMapper.selectByPrimaryKey(1);
        Assert.assertNotNull(cls);
        Assert.assertEquals((Integer) 1, cls.getCid());
        System.out.println(cls);
    }
}

運行該測試用例,控制臺輸出內容如下:
技術分享圖片

註:Cls類裏重寫了toString方法

如上,從測試結果可以看到是測試成功的,那麽我們SpringBoot集成Mybatis就完成了。

SpringBoot2.x整合MyBatis