Java Fluent Mybatis實戰(zhàn)之構(gòu)建項目與代碼生成篇上
簡述
偶然看到一篇關(guān)于阿里新orm框架的文章,好奇的點了進(jìn)去。開發(fā)后端多年,看到這個還是有點興奮的。常用mysql的orm框架mybatis、jpa,到后來的優(yōu)化框架mybatis-plus都是用過,他們或多或少都有優(yōu)缺點吧。程序員本就是日常革新技術(shù)的職業(yè),所以了解更多的框架絕對不會有錯誤。所以我嘗試著把自己學(xué)習(xí)該框架的過程,記錄下來,盡可能去掉一些項目工程中用不到的功能,展示一些實用有幫助的代碼。
特性
首先分享一下碼云上的項目鏈接:碼云地址
看一下官方給出的特性圖
給出對幾個特性乍一看還是很全面的,其中比較吸引我的是兩點。
1、從圖中給出的語法,和sql十分相近,不仔細(xì)看還以為是直接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)文章希望大家以后多多支持腳本之家!
- Java Fluent Mybatis實戰(zhàn)之構(gòu)建項目與代碼生成篇下
- Fluent Mybatis實現(xiàn)環(huán)境隔離和租戶隔離
- Fluent Mybatis 批量更新的使用
- springboot 整合fluent mybatis的過程,看這篇夠了
- Fluent MyBatis實現(xiàn)動態(tài)SQL
- Fluent Mybatis快速入門詳細(xì)教程
- Fluent Mybatis零xml配置實現(xiàn)復(fù)雜嵌套查詢
- Fluent Mybatis如何做到代碼邏輯和sql邏輯的合一
- Java Fluent Mybatis 聚合查詢與apply方法詳解流程篇
相關(guān)文章
kafka生產(chǎn)者和消費者的javaAPI的示例代碼
這篇文章主要介紹了kafka生產(chǎn)者和消費者的javaAPI的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-06-06解析Mybatis Porxy動態(tài)代理和sql解析替換問題
這篇文章主要介紹了Mybatis Porxy動態(tài)代理和sql解析替換,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-04-04Spring websocket并發(fā)發(fā)送消息異常的解決
本文主要介紹了 Spring websocket并發(fā)發(fā)送消息異常的解決,當(dāng)多個線程同時嘗試通過 WebSocket 會話發(fā)送消息時,會拋出異常,下面就來解決一下,感興趣的可以了解一下2023-09-09spring整合redis以及使用RedisTemplate的方法
本篇文章主要介紹了spring整合redis以及使用RedisTemplate的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-05-05@Transactional和@DS怎樣在事務(wù)中切換數(shù)據(jù)源
這篇文章主要介紹了@Transactional和@DS怎樣在事務(wù)中切換數(shù)據(jù)源問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-07-07