溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Java Fluent Mybatis怎么構(gòu)建項(xiàng)目與實(shí)現(xiàn)代碼生成

發(fā)布時(shí)間:2023-05-05 09:57:12 來(lái)源:億速云 閱讀:131 作者:zzz 欄目:編程語(yǔ)言

這篇文章主要介紹了Java Fluent Mybatis怎么構(gòu)建項(xiàng)目與實(shí)現(xiàn)代碼生成的相關(guān)知識(shí),內(nèi)容詳細(xì)易懂,操作簡(jiǎn)單快捷,具有一定借鑒價(jià)值,相信大家閱讀完這篇Java Fluent Mybatis怎么構(gòu)建項(xiàng)目與實(shí)現(xiàn)代碼生成文章都會(huì)有所收獲,下面我們一起來(lái)看看吧。

特性

看一下官方給出的特性圖

Java Fluent Mybatis怎么構(gòu)建項(xiàng)目與實(shí)現(xiàn)代碼生成

給出對(duì)幾個(gè)特性乍一看還是很全面的,其中比較吸引我的是兩點(diǎn)。

1、從圖中給出的語(yǔ)法,和sql十分相近,不仔細(xì)看還以為是直接sql語(yǔ)句扔了上來(lái)??瓷先ゾ捅容^實(shí)用。

2、No xml&mapper,雖然mybatis-plus已經(jīng)做到實(shí)用 IService接口實(shí)現(xiàn)大部分的sql操作

項(xiàng)目搭建

springboot搭建一項(xiàng)目的過(guò)程就不過(guò)多贅述了,這里說(shuō)下我實(shí)用的springboot版本

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.5</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

代碼結(jié)構(gòu)如下:

Java Fluent Mybatis怎么構(gòu)建項(xiàng)目與實(shí)現(xiàn)代碼生成

maven依賴引入-fluent-mybatis
<properties>
    <fluent-mybatis.version>1.8.7</fluent-mybatis.version>
</properties>
<dependencies>
    <!-- 引入fluent-mybatis 運(yùn)行依賴包, scope為compile -->
    <dependency>
        <groupId>com.github.atool</groupId>
        <artifactId>fluent-mybatis</artifactId>
        <version>${fluent-mybatis.version}</version>
    </dependency>
    <!-- 引入fluent-mybatis-processor, scope設(shè)置為provider 編譯需要,運(yùn)行時(shí)不需要 -->
    <dependency>
        <groupId>com.github.atool</groupId>
        <artifactId>fluent-mybatis-processor</artifactId>
        <scope>provided</scope>
        <version>${fluent-mybatis.version}</version>
    </dependency>
</dependencies>

完整maven依賴如下

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.5</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.hy</groupId>
    <artifactId>fluent-mybatis-project</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>fluent-mybatis-project</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
        <fluent-mybatis.version>1.8.7</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-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org</groupId>
            <artifactId>jaudiotagger</artifactId>
            <version>2.0.1</version>
        </dependency>
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>30.1.1-jre</version>
        </dependency>
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.5.2</version>
        </dependency>
        <!-- 引入fluent-mybatis 運(yùn)行依賴包, scope為compile -->
        <dependency>
            <groupId>com.github.atool</groupId>
            <artifactId>fluent-mybatis</artifactId>
            <version>${fluent-mybatis.version}</version>
        </dependency>
        <!-- 引入fluent-mybatis-processor, scope設(shè)置為provider 編譯需要,運(yùn)行時(shí)不需要 -->
        <dependency>
            <groupId>com.github.atool</groupId>
            <artifactId>fluent-mybatis-processor</artifactId>
            <scope>provided</scope>
            <version>${fluent-mybatis.version}</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.2.0</version>
        </dependency>
 
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
    </dependencies>
 
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>
 
</project>
表構(gòu)建

在數(shù)據(jù)庫(kù)創(chuàng)建一張測(cè)試表,表比較簡(jiǎn)單,先試試看。sql如下:

CREATE TABLE `test_fluent_mybatis` (
  `id` int NOT NULL AUTO_INCREMENT COMMENT '自增主鍵',
  `name` varchar(255) DEFAULT NULL COMMENT '姓名',
  `age` int DEFAULT NULL COMMENT '年齡',
  `create_time` datetime DEFAULT NULL COMMENT '創(chuàng)建時(shí)間',
  `del_flag` int DEFAULT NULL COMMENT '是否刪除',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
代碼生成工具類

注意:放到測(cè)試代碼包中。結(jié)構(gòu)如下圖:

Java Fluent Mybatis怎么構(gòu)建項(xiàng)目與實(shí)現(xiàn)代碼生成

代碼生成工具類代碼,先按照官方給的簡(jiǎn)單樣例來(lái),如下:

package com.hy.fmp;
 
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;
 
public class EntityGeneratorDemo {
  // 數(shù)據(jù)源 url
  static final String url =
      "jdbc:mysql://192.168.0.16:3306/test?useUnicode=true&characterEncoding=utf8";
  // 數(shù)據(jù)庫(kù)用戶名
  static final String username = "root";
  // 數(shù)據(jù)庫(kù)密碼
  static final String password = "123456";
 
  @Test
  public void generate() throws Exception {
    // 引用配置類,build方法允許有多個(gè)配置類
    FileGenerator.build(Empty.class);
  }
 
  @Tables(
      // 設(shè)置數(shù)據(jù)庫(kù)連接信息
      url = url,
      username = username,
      password = password,
      // 設(shè)置entity類生成src目錄, 相對(duì)于 user.dir
      srcDir = "src/main/java",
      // 設(shè)置entity類的package值
      basePack = "com.hy.fmp.fluent",
      // 設(shè)置dao接口和實(shí)現(xiàn)的src目錄, 相對(duì)于 user.dir
      daoDir = "src/main/java",
      // 設(shè)置哪些表要生成Entity文件
      tables = {@Table(value = {"test_fluent_mybatis"})})
  static class Empty { // 類名隨便取, 只是配置定義的一個(gè)載體
  }
}

執(zhí)行代碼生成工具,看看都生成了些什么。

Java Fluent Mybatis怎么構(gòu)建項(xiàng)目與實(shí)現(xiàn)代碼生成

可以看到生成的包如下。

Java Fluent Mybatis怎么構(gòu)建項(xiàng)目與實(shí)現(xiàn)代碼生成

解決類找不到問(wèn)題

這里有個(gè)坑,看下面的截圖

Java Fluent Mybatis怎么構(gòu)建項(xiàng)目與實(shí)現(xiàn)代碼生成

其實(shí)官方給了解決方法,只是沒(méi)有對(duì)此說(shuō)明。

Java Fluent Mybatis怎么構(gòu)建項(xiàng)目與實(shí)現(xiàn)代碼生成

簡(jiǎn)而言之就是你需要使用maven編譯一下,所以我們compile一下。

Java Fluent Mybatis怎么構(gòu)建項(xiàng)目與實(shí)現(xiàn)代碼生成

編譯結(jié)束后我們可以在target中,找到報(bào)錯(cuò)包位置中的編譯文件。

Java Fluent Mybatis怎么構(gòu)建項(xiàng)目與實(shí)現(xiàn)代碼生成

之前報(bào)錯(cuò)的類已經(jīng)不再報(bào)錯(cuò)了。完美。

Java Fluent Mybatis怎么構(gòu)建項(xiàng)目與實(shí)現(xiàn)代碼生成

關(guān)于“Java Fluent Mybatis怎么構(gòu)建項(xiàng)目與實(shí)現(xiàn)代碼生成”這篇文章的內(nèi)容就介紹到這里,感謝各位的閱讀!相信大家對(duì)“Java Fluent Mybatis怎么構(gòu)建項(xiàng)目與實(shí)現(xiàn)代碼生成”知識(shí)都有一定的了解,大家如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI