簡述
偶然看到一篇關于阿里新orm框架的文章,好奇的點了進去。開發后端多年,看到這個還是有點興奮的。常用mysql的orm框架mybatis、jpa,到后來的優化框架mybatis-plus都是用過,他們或多或少都有優缺點吧。程序員本就是日常革新技術的職業,所以了解更多的框架絕對不會有錯誤。所以我嘗試著把自己學習該框架的過程,記錄下來,盡可能去掉一些項目工程中用不到的功能,展示一些實用有幫助的代碼。
特性
首先分享一下碼云上的項目鏈接:碼云地址
看一下官方給出的特性圖
給出對幾個特性乍一看還是很全面的,其中比較吸引我的是兩點。
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>
代碼結構如下:
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依賴如下
<?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設置為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>
表構建
在數據庫創建一張測試表,表比較簡單,先試試看。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 '創建時間', `del_flag` int DEFAULT NULL COMMENT '是否刪除', PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
代碼生成工具類
注意:放到測試代碼包中。結構如下圖:
代碼生成工具類代碼,先按照官方給的簡單樣例來,如下:
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 { // 類名隨便取, 只是配置定義的一個載體 } }
執行代碼生成工具,看看都生成了些什么。
可以看到生成的包如下。
解決類找不到問題
這里有個坑,看下面的截圖
其實官方給了解決方法,只是沒有對此說明。
簡而言之就是你需要使用maven編譯一下,所以我們compile一下。
編譯結束后我們可以在target中,找到報錯包位置中的編譯文件。
之前報錯的類已經不再報錯了。完美。
總結
OK,現在項目和表代碼都生成完成了,下一篇講一下簡單的操作。
文章鏈接:FluentMybatis 項目構建、代碼生成(下)
Github代碼鏈接:GitHub倉庫
如果本文對你有幫助,請點個贊支持一下吧。
到此這篇關于Java Fluent Mybatis實戰之構建項目與代碼生成篇上的文章就介紹到這了,更多相關Java Fluent Mybatis內容請搜索服務器之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持服務器之家!
原文鏈接:https://huyi-aliang.blog.csdn.net/article/details/120848199