前言
在基于Mybatis的開發模式中,很多開發者還會選擇Mybatis-Plus來輔助功能開發,以此提高開發的效率。雖然Mybatis也有代碼生成的工具,但Mybatis-Plus由于在Mybatis基礎上做了一些調整,是以,正常的生成工具生成的代碼還有一些不太符合預期。而且對于多資料庫的支援不是很好。
是以,我們需要一款支援高度定制化,帶圖形UI頁面,能适配多數資料庫的基礎程式生成架構。本文就介紹這款基于Mybatis-Plus的代碼自助生成器,github位址:mybatis-plus-generator-ui。
文章通過執行個體內建的方式來詳細講解mybatis-plus-generator-ui,感興趣的朋友可以自己clone下來,也可以自己進行擴充自定義。
一、mybatis-plus-generator-ui是什麼?
它是對mybatis-plus-generator進行封裝,通過Web UI快速生成相容Spring boot,mybatis-plus架構的各類業務代碼。提供互動式的Web UI用于生成相容mybatis-plus架構的相關功能代碼,包括Entity、Mapper、Mapper.xml、Service、Controller等,可以自定義模闆以及各類輸出參數,也可通過SQL查詢語句直接生成代碼。
功能清單:
- Table查詢: 查詢配置的關系型資料庫表的清單查詢。
- 輸出配置: 對需要生成的相關代碼,比如Entity、Mapper、Servcie、Controller等代碼模闆資訊進行配置,用于在轉換時調用。
- 項目導入: 可以導入其它項目配置好的資訊給本項目使用。
- 下載下傳模闆: 支援本項目配置的模闆資訊下載下傳後共享。
- 政策配置: 直接定義各種檔案的生成政策。
- 模闆上傳: 支援從别的項目中下載下傳模闆,同上傳供本項目使用。
- SQL輸入上傳: 支援将查詢語句直接上傳或者複制到輸入框中。
- SQL代碼生成: 基于SQL腳本生成相應的代碼。
二、 mybatis-plus-generator-ui怎麼用?
mybatis-plus-generator-ui提供以jar包的形式為外部項目提供服務,通過配置的資料庫配置去讀取資料庫的配置資訊,并通過Web UI的方式提供給開發者使用。mybatis-plus-generator-ui支援POSTGRE_SQL、ORACLE、DB2、MySQL、SQLSERVER等常見的關系型資料庫。
1、maven pom引入
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.yelang</groupId>
<artifactId>mybatis-plus-generator-ui-case</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>com.github.davidfantasy</groupId>
<artifactId>mybatis-plus-generator-ui</artifactId>
<version>1.4.5</version>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.2.25</version>
</dependency>
</dependencies>
</project>
2、建立程式入口,以main函數的方式運作
mybatis-plus-generator-ui在1.4.0版本之後,可支援将GeberatorUIServer獨立部署為一個單獨的spring boot項目,通過頁面指定目标項目根目錄的方式為多個項目提供源碼生成服務。這種方式适用于有多個項目庫需要獨立進行開發的模式。執行個體關鍵代碼如下:
package com.yelang;
import com.github.davidfantasy.mybatisplus.generatorui.GeneratorConfig;
import com.github.davidfantasy.mybatisplus.generatorui.MybatisPlusToolsApplication;
import com.github.davidfantasy.mybatisplus.generatorui.mbp.NameConverter;
public class GeneratorMain {
public static void main(String[] args) {
GeneratorConfig config = GeneratorConfig.builder().jdbcUrl("jdbc:postgresql://127.0.0.1:5432/ghyapp")
.userName("ghy01").password("ghy01").driverClassName("org.postgresql.Driver")
// 資料庫schema,POSTGRE_SQL,ORACLE,DB2類型的資料庫需要指定
// .schemaName("myBusiness")
// 如果需要修改各類生成檔案的預設命名規則,可自定義一個NameConverter執行個體,覆寫相應的名稱轉換方法:
.nameConverter(new NameConverter() {
/**
* 自定義Service類檔案的名稱規則
*/
public String serviceNameConvert(String tableName) {
return this.entityNameConvert(tableName) + "Service";
}
/**
* 自定義Controller類檔案的名稱規則
*/
public String controllerNameConvert(String tableName) {
return this.entityNameConvert(tableName) + "Action";
}
}).basePackage("com.github.davidfantasy.mybatisplustools.example").port(8068).build();
MybatisPlusToolsApplication.run(config);
}
}
在上面的配置中,我們連接配接的示例資料庫是PostgerSQL,需要在Maven中定義相應的驅動程式,并且在上述代碼中正确配置相應的類。最後指定了程式的運作端口為8086,這種運作方式跟SpringBoot非常相似。
3、執行個體運作
運作以上的main方法,在控制台可以看到以下輸出即為成功部署。
在輸出的日志中,可以看到程式的運作端口,以及預設的模闆目錄位址。在浏覽器中輸入通路位址http://localhost:8068/,即可進行配置生成。
三、mybatis-plus-generator-ui代碼生成
1、Table的查詢和浏覽
可以直接浏覽和查詢配置的資料源的資料表資訊,可選擇一個或多個生成模闆代碼。
2、輸出配置
内置Entity、Mapper、Service、Controller等6種類型代碼的模闆配置,可以上傳模闆進行替換,并修改各類參數,配置參數已經按照影響的檔案類型重新進行了分類,并加入了部分文本說明;也可以自行添加其它類型的自定義輸出檔案。所有的配置項都會按照項目包名進行儲存,隻需一次性設定就可以了。
3、政策配置
将每次生成代碼時可能變動的内容加入到代碼生成選項中,友善調整每次的生成政策,比如:是否覆寫原檔案,生成檔案的種類等等:
4、SQL配置生成
通過輸入查詢SQL,可自動在Mapper(Xml及Java)中生成對應的查詢方法,DTO對象和ResultMap(結果集映射配置)
5、代碼生成
四、自定義擴充
1、相關模闆調整
在相關的頁面中,可以進行相應的調整,在對應的btl模闆中下載下傳對應檔案的具體模闆,使用文本工具打開,直接修改源代碼,文中取一種方式示例,其它方式一樣。
2、代碼層級的配置
在一些團隊中,肯定對Mapper的定義為Dao,Controller層需要定義為Action,通過修改代碼模闆btl的方式是可以的,還有一種方式是通過調整内部映射的方式來進行修改。主要使用的類是NameConverter。
/**
* 自定義Service類檔案的名稱規則
*/
public String serviceNameConvert(String tableName) {
return this.entityNameConvert(tableName) + "Service";
}
/**
* 自定義Controller類檔案的名稱規則
*/
public String controllerNameConvert(String tableName) {
return this.entityNameConvert(tableName) + "Action";
}
除了Service、Controller、Entity、FieldName都可以實作自定義的擴充。下面是NameConverter類的核心代碼,這裡有詳細的定義。
package com.github.davidfantasy.mybatisplus.generatorui.mbp;
import cn.hutool.core.util.StrUtil;
import com.github.davidfantasy.mybatisplus.generatorui.dto.Constant;
import com.google.common.base.Strings;
import static com.github.davidfantasy.mybatisplus.generatorui.dto.Constant.DOT_JAVA;
import static com.github.davidfantasy.mybatisplus.generatorui.dto.Constant.DOT_XML;
/**
* 自定義各類名稱轉換的規則
*/
public interface NameConverter {
/**
* 自定義Entity.java的類名稱
*
* @param tableName 表名稱
* @return
*/
default String entityNameConvert(String tableName) {
if (Strings.isNullOrEmpty(tableName)) {
return "";
}
tableName = tableName.substring(tableName.indexOf(StrUtil.UNDERLINE) + 1, tableName.length());
return StrUtil.upperFirst(StrUtil.toCamelCase(tableName.toLowerCase()));
}
/**
* 自定義表字段名到實體類屬性名的轉換規則
*
* @param fieldName 表字段名稱
* @return
*/
default String propertyNameConvert(String fieldName) {
if (Strings.isNullOrEmpty(fieldName)) {
return "";
}
if (fieldName.contains("_")) {
return StrUtil.toCamelCase(fieldName.toLowerCase());
}
return fieldName;
}
/**
* 自定義Mapper.java的類名稱
*/
default String mapperNameConvert(String tableName) {
return entityNameConvert(tableName) + "Mapper";
}
/**
* 自定義Mapper.xml的檔案名稱
*/
default String mapperXmlNameConvert(String tableName) {
return entityNameConvert(tableName) + "Mapper";
}
/**
* 自定義Service.java的類名稱
*/
default String serviceNameConvert(String tableName) {
return "I" + entityNameConvert(tableName) + "Service";
}
/**
* 自定義ServiceImpl.java的類名稱
*/
default String serviceImplNameConvert(String tableName) {
return entityNameConvert(tableName) + "ServiceImpl";
}
/**
* 自定義Controller.java的類名稱
*/
default String controllerNameConvert(String tableName) {
return entityNameConvert(tableName) + "Controller";
}
/**
* 自定義其它生成檔案的檔案名(不包括entity,mapper.java,mapper.xml,service,serviceImpl,controller這6種)
*
* @param fileType 在頁面上輸入的輸出檔案辨別
* @param tableName 關聯的資料表名稱名稱
* @return 生成檔案的名稱,帶字尾
*/
default String outputFileNameConvert(String fileType, String tableName) {
if (fileType.equals(Constant.FILE_TYPE_ENTITY)) {
return this.entityNameConvert(tableName) + DOT_JAVA;
} else if (fileType.equals(Constant.FILE_TYPE_MAPPER)) {
return this.mapperNameConvert(tableName) + DOT_JAVA;
} else if (fileType.equals(Constant.FILE_TYPE_MAPPER_XML)) {
return this.mapperXmlNameConvert(tableName) + DOT_XML;
} else if (fileType.equals(Constant.FILE_TYPE_SERVICE)) {
return this.serviceNameConvert(tableName) + DOT_JAVA;
} else if (fileType.equals(Constant.FILE_TYPE_SERVICEIMPL)) {
return this.serviceImplNameConvert(tableName) + DOT_JAVA;
} else if (fileType.equals(Constant.FILE_TYPE_CONTROLLER)) {
return this.controllerNameConvert(tableName) + DOT_JAVA;
}
return this.entityNameConvert(tableName) + fileType;
}
}
mybatis-plus-generator-ui的功能非常豐富,甚至針對ui都是可以自定義修改的。如果需要定制UI的話,将代碼clone下來後,進入到frontend目錄下,進行相應的擴充開發。
修改完成後,需要另行編譯src\frontend中的靜态資源(源碼中不包含已編譯的頁面),在src\frontend檔案夾中執行:
yarn install
yarn run build
五、總結
以上就是今天要講的内容,本文簡要介紹一款基于Mybatis-Plus的代碼自助生成器,位址:
GitHub - davidfantasy/mybatis-plus-generator-ui: 對mybatis-plus-generator進行封裝,通過Web UI快速生成相容Spring boot,mybatis-plus架構的各類業務代碼
文章通過執行個體內建的方式來詳細講解mybatis-plus-generator-ui,從相關概念到實際內建案例,以及具體的擴充開發介紹。如果在工作中有這種需要,不妨采用這種方式。希望本文對您有所幫助,歡迎指導交流。