首頁  >  文章  >  Java  >  Java Fluent Mybatis如何建構專案與實作程式碼生成

Java Fluent Mybatis如何建構專案與實作程式碼生成

WBOY
WBOY轉載
2023-05-03 10:07:141260瀏覽

特性

看一下官方給出的特性圖

Java Fluent Mybatis如何构建项目与实现代码生成

#給幾個特性乍看之下還是很全面的,其中比較吸引我的是兩點。

1、從圖中給的文法,和sql十分相近,不仔細看還以為是直接sql語句丟了上來。看上去就比較實用。

2、No xml&mapper,雖然mybatis-plus已經做到實用IService介面實作大部分的sql操作

專案建置

springboot建置一項目的過程就不過多贅述了,這裡說下我實用的springboot版本

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

程式碼結構如下:

Java Fluent Mybatis如何构建项目与实现代码生成

maven依賴引入-fluent-mybatis

<properties>
    <fluent-mybatis.version>1.8.7</fluent-mybatis.version>
</properties>
<dependencies>
    <!-- 引入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>
        <scope>provided</scope>
        <version>${fluent-mybatis.version}</version>
    </dependency>
</dependencies>

完整maven依賴如下



    4.0.0
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.5</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    com.hy
    fluent-mybatis-project
    0.0.1-SNAPSHOT
    fluent-mybatis-project
    Demo project for Spring Boot
    
        1.8
        1.8.7
    
    
        
            org.springframework.boot
            spring-boot-starter-web
        
 
        
            org.springframework.boot
            spring-boot-devtools
            runtime
            true
        
        
            org.springframework.boot
            spring-boot-configuration-processor
            true
        
        
            org.projectlombok
            lombok
            true
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
            org
            jaudiotagger
            2.0.1
        
        
            com.google.guava
            guava
            30.1.1-jre
        
        
            cn.hutool
            hutool-all
            5.5.2
        
        
        
            com.github.atool
            fluent-mybatis
            ${fluent-mybatis.version}
        
        
        
            com.github.atool
            fluent-mybatis-processor
            provided
            ${fluent-mybatis.version}
        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            2.2.0
        
 
        
            mysql
            mysql-connector-java
            runtime
        
    
 
    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
                
                    
                        
                            org.projectlombok
                            lombok
                        
                    
                
            
        
    
 

表格建置

在資料庫建立一張測試表,表格比較簡單,先試試看。 sql如下:

CREATE TABLE `test_fluent_mybatis` (
  `id` int NOT NULL AUTO_INCREMENT COMMENT &#39;自增主键&#39;,
  `name` varchar(255) DEFAULT NULL COMMENT &#39;姓名&#39;,
  `age` int DEFAULT NULL COMMENT &#39;年龄&#39;,
  `create_time` datetime DEFAULT NULL COMMENT &#39;创建时间&#39;,
  `del_flag` int DEFAULT NULL COMMENT &#39;是否删除&#39;,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

程式碼產生工具類別

注意:放到測試程式碼包中。架構如下圖:

Java Fluent Mybatis如何构建项目与实现代码生成

程式碼產生工具類別程式碼,先按照官方給的簡單範例來,如下:

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 {
  // 数据源 url
  static final String url =
      "jdbc:mysql://192.168.0.16:3306/test?useUnicode=true&characterEncoding=utf8";
  // 数据库用户名
  static final String username = "root";
  // 数据库密码
  static final String password = "123456";
 
  @Test
  public void generate() throws Exception {
    // 引用配置类,build方法允许有多个配置类
    FileGenerator.build(Empty.class);
  }
 
  @Tables(
      // 设置数据库连接信息
      url = url,
      username = username,
      password = password,
      // 设置entity类生成src目录, 相对于 user.dir
      srcDir = "src/main/java",
      // 设置entity类的package值
      basePack = "com.hy.fmp.fluent",
      // 设置dao接口和实现的src目录, 相对于 user.dir
      daoDir = "src/main/java",
      // 设置哪些表要生成Entity文件
      tables = {@Table(value = {"test_fluent_mybatis"})})
  static class Empty { // 类名随便取, 只是配置定义的一个载体
  }
}

執行程式碼產生工具,看看都生成了什麼。

Java Fluent Mybatis如何构建项目与实现代码生成

可以看到產生的套件如下。

Java Fluent Mybatis如何构建项目与实现代码生成

解決類別找不到問題

這裡有個坑,看下面的截圖

Java Fluent Mybatis如何构建项目与实现代码生成

#其實官方給了解決方法,只是沒有對此說明。

Java Fluent Mybatis如何构建项目与实现代码生成

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

Java Fluent Mybatis如何构建项目与实现代码生成

編譯結束後我們可以在target中,找到報錯包位置中的編譯檔。

Java Fluent Mybatis如何构建项目与实现代码生成

之前報錯的類別已經不再報錯了。完美。

Java Fluent Mybatis如何构建项目与实现代码生成

以上是Java Fluent Mybatis如何建構專案與實作程式碼生成的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:yisu.com。如有侵權,請聯絡admin@php.cn刪除