1. 程式人生 > 其它 >實驗: spring-boot 整合 fluent-mybatis 實驗過程!!!!

實驗: spring-boot 整合 fluent-mybatis 實驗過程!!!!

1.參考:

簡單整合,會報錯誤

https://segmentfault.com/a/1190000040467885?utm_source=sf-similar-article

利用maven編譯,

https://blog.csdn.net/zhiweihongyan1/article/details/120848199?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522164592123216780274162555%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fall.%2522%257D&request_id=164592123216780274162555&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~first_rank_ecpm_v1~rank_v31_ecpm-5-120848199.pc_search_result_cache&utm_term=fluent+mybatis&spm=1018.2226.3001.4187

開啟目標顯示才不會錯誤。

2. 新建工程

附:idea 建立spring-boot程式抓圖:參考連線。 https://www.cnblogs.com/cqmcu/p/15926462.html

新增pom

<properties>
        <java.version>1.8</java.version>
        <fluent-mybatis.version>1.6.13</fluent-mybatis.version>
    </properties>

    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>


        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true
</optional> </dependency> <!-- 引入fluent-mybatis 執行依賴包, scope為compile --> <dependency> <groupId>com.github.atool</groupId> <artifactId>fluent-mybatis</artifactId> <version>${fluent-mybatis.version}</version> </dependency> <!-- 引入fluent-mybatis-processor, scope設定為provider 編譯需要,執行時不需要 --> <dependency> <groupId>com.github.atool</groupId> <artifactId>fluent-mybatis-processor</artifactId> <version>${fluent-mybatis.version}</version> <!-- <scope>provided</scope>--> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.2.2</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <version>3.1.0</version> </plugin> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
View Code

注意,mawen編譯外掛,沒有則有問題。

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.1.0</version>
</plugin>

3. application.yml配置
server:
  port: 8080 # 埠號
spring:
  datasource: # 資料庫引數配置
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/test_db?useUnicode=true&characterEncoding=utf8
    username: root
    password: 123456
View Code

4.EntityGeneratorTests.java 自動程式碼生成

package com.example.helloworld;

import cn.org.atool.generator.FileGenerator;
import cn.org.atool.generator.annotation.Table;
import cn.org.atool.generator.annotation.Tables;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
public class EntityGeneratorTests {

    // 資料來源 url
    static final String url = "jdbc:mysql://127.0.0.1:3306/test_db?useUnicode=true&characterEncoding=utf8";
    // 資料庫使用者名稱
    static final String username = "root";
    // 資料庫密碼
    static final String password = "123456";

    @Test
    public void generate() {
        // 引用配置類,build方法允許有多個配置類
        FileGenerator.build(Empty.class);
    }

    @Tables(
            // 設定資料庫連線資訊
            url = url, username = username, password = password,
            // 設定entity類生成src目錄, 相對於 user.dir
            srcDir = "src/main/java",
            // 設定entity類的package值
            basePack = "com.example.helloworld",
            // 設定dao介面和實現的src目錄, 相對於 user.dir
            daoDir = "src/main/java",
            // 設定哪些表要生成Entity檔案
            tables = {@Table(value = {"person"})}
    )
    static class Empty { //類名隨便取, 只是配置定義的一個載體
    }

}
View Code

5. 利用maven編譯

6.顯示target

7.編譯產生程式碼

8. 產生目的碼

9.新增對映掃描

10.Controller測試程式碼

11.postman功能測試