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

給出對(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)如下:

maven依賴(lài)引入-fluent-mybatis
<properties>
<fluent-mybatis.version>1.8.7</fluent-mybatis.version>
</properties>
<dependencies>
<!-- 引入fluent-mybatis 運(yùn)行依賴(lài)包, 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依賴(lài)如下
<?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)行依賴(lài)包, 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;
代碼生成工具類(lèi)
注意:放到測(cè)試代碼包中。結(jié)構(gòu)如下圖:

代碼生成工具類(lèi)代碼,先按照官方給的簡(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 {
// 引用配置類(lèi),build方法允許有多個(gè)配置類(lèi)
FileGenerator.build(Empty.class);
}
@Tables(
// 設(shè)置數(shù)據(jù)庫(kù)連接信息
url = url,
username = username,
password = password,
// 設(shè)置entity類(lèi)生成src目錄, 相對(duì)于 user.dir
srcDir = "src/main/java",
// 設(shè)置entity類(lèi)的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 { // 類(lèi)名隨便取, 只是配置定義的一個(gè)載體
}
}
執(zhí)行代碼生成工具,看看都生成了些什么。

可以看到生成的包如下。

解決類(lèi)找不到問(wèn)題
這里有個(gè)坑,看下面的截圖

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

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

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

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

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

到此這篇關(guān)于Java Fluent Mybatis實(shí)戰(zhàn)之構(gòu)建項(xiàng)目與代碼生成篇上的文章就介紹到這了,更多相關(guān)Java Fluent Mybatis內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Java Fluent Mybatis實(shí)戰(zhàn)之構(gòu)建項(xiàng)目與代碼生成篇下
- Fluent Mybatis實(shí)現(xiàn)環(huán)境隔離和租戶隔離
- Fluent Mybatis 批量更新的使用
- springboot 整合fluent mybatis的過(guò)程,看這篇夠了
- Fluent MyBatis實(shí)現(xiàn)動(dòng)態(tài)SQL
- Fluent Mybatis快速入門(mén)詳細(xì)教程
- Fluent Mybatis零xml配置實(shí)現(xiàn)復(fù)雜嵌套查詢
- Fluent Mybatis如何做到代碼邏輯和sql邏輯的合一
- Java Fluent Mybatis 聚合查詢與apply方法詳解流程篇
相關(guān)文章
SpringBoot之配置logging日志及在控制臺(tái)中輸出過(guò)程
這篇文章主要介紹了SpringBoot之配置logging日志及在控制臺(tái)中輸出過(guò)程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-06-06
SpringBoot?Validation提示信息國(guó)際化配置方式
這篇文章主要介紹了SpringBoot?Validation提示信息國(guó)際化配置方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02
Spring實(shí)戰(zhàn)之協(xié)調(diào)作用域不同步的Bean操作示例
這篇文章主要介紹了Spring實(shí)戰(zhàn)之協(xié)調(diào)作用域不同步的Bean操作,結(jié)合實(shí)例形式分析了Spring協(xié)調(diào)作用域不同步的Bean相關(guān)配置及使用技巧,需要的朋友可以參考下2019-11-11
Springboot在有鎖的情況下正確使用事務(wù)的實(shí)現(xiàn)代碼
這篇文章主要介紹了Springboot在有鎖的情況下如何正確使用事務(wù),今天通過(guò)一個(gè)實(shí)驗(yàn)給大家分析一下商品超賣(mài)問(wèn)題,模擬場(chǎng)景分析通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2021-12-12
Mybatis框架之代理模式(Proxy Pattern)的實(shí)現(xiàn)
本文主要介紹了MyBatis框架中使用代理模式ProxyPattern的原理和實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2024-11-11

