Java Fluent Mybatis實戰(zhàn)之構(gòu)建項目與代碼生成篇上
簡述
偶然看到一篇關(guān)于阿里新orm框架的文章,好奇的點了進去。開發(fā)后端多年,看到這個還是有點興奮的。常用mysql的orm框架mybatis、jpa,到后來的優(yōu)化框架mybatis-plus都是用過,他們或多或少都有優(yōu)缺點吧。程序員本就是日常革新技術(shù)的職業(yè),所以了解更多的框架絕對不會有錯誤。所以我嘗試著把自己學(xué)習(xí)該框架的過程,記錄下來,盡可能去掉一些項目工程中用不到的功能,展示一些實用有幫助的代碼。
特性
首先分享一下碼云上的項目鏈接:碼云地址
看一下官方給出的特性圖

給出對幾個特性乍一看還是很全面的,其中比較吸引我的是兩點。
1、從圖中給出的語法,和sql十分相近,不仔細看還以為是直接sql語句扔了上來。看上去就比較實用。
2、No xml&mapper,雖然mybatis-plus已經(jīng)做到實用 IService接口實現(xiàn)大部分的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>
代碼結(jié)構(gòu)如下:

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設(shè)置為provider 編譯需要,運行時不需要 -->
<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 運行依賴包, scope為compile -->
<dependency>
<groupId>com.github.atool</groupId>
<artifactId>fluent-mybatis</artifactId>
<version>${fluent-mybatis.version}</version>
</dependency>
<!-- 引入fluent-mybatis-processor, scope設(shè)置為provider 編譯需要,運行時不需要 -->
<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ù)庫創(chuàng)建一張測試表,表比較簡單,先試試看。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)建時間', `del_flag` int DEFAULT NULL COMMENT '是否刪除', PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
代碼生成工具類
注意:放到測試代碼包中。結(jié)構(gòu)如下圖:

代碼生成工具類代碼,先按照官方給的簡單樣例來,如下:
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ù)庫用戶名
static final String username = "root";
// 數(shù)據(jù)庫密碼
static final String password = "123456";
@Test
public void generate() throws Exception {
// 引用配置類,build方法允許有多個配置類
FileGenerator.build(Empty.class);
}
@Tables(
// 設(shè)置數(shù)據(jù)庫連接信息
url = url,
username = username,
password = password,
// 設(shè)置entity類生成src目錄, 相對于 user.dir
srcDir = "src/main/java",
// 設(shè)置entity類的package值
basePack = "com.hy.fmp.fluent",
// 設(shè)置dao接口和實現(xiàn)的src目錄, 相對于 user.dir
daoDir = "src/main/java",
// 設(shè)置哪些表要生成Entity文件
tables = {@Table(value = {"test_fluent_mybatis"})})
static class Empty { // 類名隨便取, 只是配置定義的一個載體
}
}
執(zhí)行代碼生成工具,看看都生成了些什么。

可以看到生成的包如下。

解決類找不到問題
這里有個坑,看下面的截圖

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

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

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

之前報錯的類已經(jīng)不再報錯了。完美。

總結(jié)
OK,現(xiàn)在項目和表代碼都生成完成了,下一篇講一下簡單的操作。
文章鏈接:Java Fluent Mybatis實戰(zhàn)之構(gòu)建項目與代碼生成篇下
Github代碼鏈接: GitHub倉庫
如果本文對你有幫助,請點個贊支持一下吧。

到此這篇關(guān)于Java Fluent Mybatis實戰(zhàn)之構(gòu)建項目與代碼生成篇上的文章就介紹到這了,更多相關(guān)Java Fluent Mybatis內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot之配置logging日志及在控制臺中輸出過程
這篇文章主要介紹了SpringBoot之配置logging日志及在控制臺中輸出過程,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-06-06
SpringBoot?Validation提示信息國際化配置方式
這篇文章主要介紹了SpringBoot?Validation提示信息國際化配置方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-02-02
Spring實戰(zhàn)之協(xié)調(diào)作用域不同步的Bean操作示例
這篇文章主要介紹了Spring實戰(zhàn)之協(xié)調(diào)作用域不同步的Bean操作,結(jié)合實例形式分析了Spring協(xié)調(diào)作用域不同步的Bean相關(guān)配置及使用技巧,需要的朋友可以參考下2019-11-11
Springboot在有鎖的情況下正確使用事務(wù)的實現(xiàn)代碼
這篇文章主要介紹了Springboot在有鎖的情況下如何正確使用事務(wù),今天通過一個實驗給大家分析一下商品超賣問題,模擬場景分析通過實例代碼給大家介紹的非常詳細,需要的朋友可以參考下2021-12-12
Mybatis框架之代理模式(Proxy Pattern)的實現(xiàn)
本文主要介紹了MyBatis框架中使用代理模式ProxyPattern的原理和實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-11-11

