簡(jiǎn)述
偶然看到一篇關(guān)于阿里新orm框架的文章,好奇的點(diǎn)了進(jìn)去。開發(fā)后端多年,看到這個(gè)還是有點(diǎn)興奮的。常用mysql的orm框架mybatis、jpa,到后來的優(yōu)化框架mybatis-plus都是用過,他們或多或少都有優(yōu)缺點(diǎn)吧。程序員本就是日常革新技術(shù)的職業(yè),所以了解更多的框架絕對(duì)不會(huì)有錯(cuò)誤。所以我嘗試著把自己學(xué)習(xí)該框架的過程,記錄下來,盡可能去掉一些項(xiàng)目工程中用不到的功能,展示一些實(shí)用有幫助的代碼。
特性
首先分享一下碼云上的項(xiàng)目鏈接:碼云地址
看一下官方給出的特性圖
給出對(duì)幾個(gè)特性乍一看還是很全面的,其中比較吸引我的是兩點(diǎn)。
1、從圖中給出的語(yǔ)法,和sql十分相近,不仔細(xì)看還以為是直接sql語(yǔ)句扔了上來。看上去就比較實(shí)用。
2、No xml&mapper,雖然mybatis-plus已經(jīng)做到實(shí)用 IService接口實(shí)現(xiàn)大部分的sql操作
項(xiàng)目搭建
springboot搭建一項(xiàng)目的過程就不過多贅述了,這里說下我實(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依賴引入-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)如下圖:
代碼生成工具類代碼,先按照官方給的簡(jiǎn)單樣例來,如下:
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í)行代碼生成工具,看看都生成了些什么。
可以看到生成的包如下。
解決類找不到問題
這里有個(gè)坑,看下面的截圖
其實(shí)官方給了解決方法,只是沒有對(duì)此說明。
簡(jiǎn)而言之就是你需要使用maven編譯一下,所以我們compile一下。
編譯結(jié)束后我們可以在target中,找到報(bào)錯(cuò)包位置中的編譯文件。
之前報(bào)錯(cuò)的類已經(jīng)不再報(bào)錯(cuò)了。完美。
總結(jié)
OK,現(xiàn)在項(xiàng)目和表代碼都生成完成了,下一篇講一下簡(jiǎn)單的操作。
文章鏈接:FluentMybatis 項(xiàng)目構(gòu)建、代碼生成(下)
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)搜索服務(wù)器之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持服務(wù)器之家!
原文鏈接:https://huyi-aliang.blog.csdn.net/article/details/120848199