前言
在基于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等常見的關系型資料庫,
推薦一個開源免費的 Spring Boot 最全教程:
https://github.com/javastacks/spring-boot-best-practice
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的代碼自助生成器,地址:
https://github.com/davidfantasy/mybatis-plus-generator-ui
文章通過實體集成的方式來詳細講解mybatis-plus-generator-ui,從相關概念到實際集成案例,以及具體的擴展開發介紹,如果在作業中有這種需要,不妨采用這種方式,希望本文對您有所幫助,歡迎指導交流,
原文鏈接:https://blog.csdn.net/yelangkingwuzuhu/article/details/128077533
著作權宣告:本文為CSDN博主「夜郎king」的原創文章,遵循CC 4.0 BY-SA著作權協議,轉載請附上原文出處鏈接及本宣告,
近期熱文推薦:
1.1,000+ 道 Java面試題及答案整理(2022最新版)
2.勁爆!Java 協程要來了,,,
3.Spring Boot 2.x 教程,太全了!
4.別再寫滿屏的爆爆爆炸類了,試試裝飾器模式,這才是優雅的方式!!
5.《Java開發手冊(嵩山版)》最新發布,速速下載!
覺得不錯,別忘了隨手點贊+轉發哦!
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/553064.html
標籤:其他
下一篇:返回列表