前言
在基于Mybatis的開發(fā)模式中,很多開發(fā)者還會選擇Mybatis-Plus來輔助功能開發(fā),以此提高開發(fā)的效率。雖然Mybatis也有代碼生成的工具,但Mybatis-Plus由于在Mybatis基礎(chǔ)上做了一些調(diào)整,因此,常規(guī)的生成工具生成的代碼還有一些不太符合預(yù)期。而且對于多數(shù)據(jù)庫的支持不是很好。
因此,我們需要一款支持高度定制化,帶圖形UI頁面,能適配多數(shù)數(shù)據(jù)庫的基礎(chǔ)程序生成框架。本文就介紹這款基于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框架的各類業(yè)務(wù)代碼。提供交互式的Web UI用于生成兼容mybatis-plus框架的相關(guān)功能代碼,包括Entity、Mapper、Mapper.xml、Service、Controller等,可以自定義模板以及各類輸出參數(shù),也可通過SQL查詢語句直接生成代碼。
功能列表:
- Table查詢: 查詢配置的關(guān)系型數(shù)據(jù)庫表的列表查詢。
- 輸出配置: 對需要生成的相關(guān)代碼,比如Entity、Mapper、Servcie、Controller等代碼模板信息進行配置,用于在轉(zhuǎn)換時調(diào)用。
- 項目導(dǎo)入: 可以導(dǎo)入其它項目配置好的信息給本項目使用。
- 下載模板: 支持本項目配置的模板信息下載后共享。
- 策略配置: 直接定義各種文件的生成策略。
- 模板上傳: 支持從別的項目中下載模板,同上傳供本項目使用。
- SQL輸入上傳: 支持將查詢語句直接上傳或者復(fù)制到輸入框中。
- SQL代碼生成: 基于SQL腳本生成相應(yīng)的代碼。
二、 mybatis-plus-generator-ui怎么用?
mybatis-plus-generator-ui提供以jar包的形式為外部項目提供服務(wù),通過配置的數(shù)據(jù)庫配置去讀取數(shù)據(jù)庫的配置信息,并通過Web UI的方式提供給開發(fā)者使用。mybatis-plus-generator-ui支持POSTGRE_SQL、ORACLE、DB2、MySQL、SQLSERVER等常見的關(guān)系型數(shù)據(jù)庫。
推薦一個開源免費的 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函數(shù)的方式運行
mybatis-plus-generator-ui在1.4.0版本之后,可支持將GeberatorUIServer獨立部署為一個單獨的spring boot項目,通過頁面指定目標(biāo)項目根目錄的方式為多個項目提供源碼生成服務(wù)。這種方式適用于有多個項目庫需要獨立進行開發(fā)的模式。實例關(guān)鍵代碼如下:
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")
// 數(shù)據(jù)庫schema,POSTGRE_SQL,ORACLE,DB2類型的數(shù)據(jù)庫需要指定
// .schemaName("myBusiness")
// 如果需要修改各類生成文件的默認(rèn)命名規(guī)則,可自定義一個NameConverter實例,覆蓋相應(yīng)的名稱轉(zhuǎn)換方法:
.nameConverter(new NameConverter() {
/**
* 自定義Service類文件的名稱規(guī)則
*/
public String serviceNameConvert(String tableName) {
return this.entityNameConvert(tableName) + "Service";
}
/**
* 自定義Controller類文件的名稱規(guī)則
*/
public String controllerNameConvert(String tableName) {
return this.entityNameConvert(tableName) + "Action";
}
}).basePackage("com.github.davidfantasy.mybatisplustools.example").port(8068).build();
MybatisPlusToolsApplication.run(config);
}
}
在上面的配置中,我們連接的示例數(shù)據(jù)庫是PostgerSQL,需要在Maven中定義相應(yīng)的驅(qū)動程序,并且在上述代碼中正確配置相應(yīng)的類。最后指定了程序的運行端口為8086,這種運行方式跟SpringBoot非常相似。
3、實例運行
運行以上的main方法,在控制臺可以看到以下輸出即為成功部署。
在輸出的日志中,可以看到程序的運行端口,以及默認(rèn)的模板目錄地址。在瀏覽器中輸入訪問地址http://localhost:8068/,即可進行配置生成。
三、mybatis-plus-generator-ui代碼生成
1、Table的查詢和瀏覽
可以直接瀏覽和查詢配置的數(shù)據(jù)源的數(shù)據(jù)表信息,可選擇一個或多個生成模板代碼。
2、輸出配置
內(nèi)置Entity、Mapper、Service、Controller等6種類型代碼的模板配置,可以上傳模板進行替換,并修改各類參數(shù),配置參數(shù)已經(jīng)按照影響的文件類型重新進行了分類,并加入了部分文本說明;也可以自行添加其它類型的自定義輸出文件。所有的配置項都會按照項目包名進行保存,只需一次性設(shè)置就可以了。
3、策略配置
將每次生成代碼時可能變動的內(nèi)容加入到代碼生成選項中,方便調(diào)整每次的生成策略,比如:是否覆蓋原文件,生成文件的種類等等:
4、SQL配置生成
通過輸入查詢SQL,可自動在Mapper(Xml及Java)中生成對應(yīng)的查詢方法,DTO對象和ResultMap(結(jié)果集映射配置)
5、代碼生成
四、自定義擴展
1、相關(guān)模板調(diào)整
在相關(guān)的頁面中,可以進行相應(yīng)的調(diào)整,在對應(yīng)的btl模板中下載對應(yīng)文件的具體模板,使用文本工具打開,直接修改源代碼,文中取一種方式示例,其它方式一樣。
2、代碼層級的配置
在一些團隊中,肯定對Mapper的定義為Dao,Controller層需要定義為Action,通過修改代碼模板btl的方式是可以的,還有一種方式是通過調(diào)整內(nèi)部映射的方式來進行修改。主要使用的類是NameConverter。
/**
* 自定義Service類文件的名稱規(guī)則
*/
public String serviceNameConvert(String tableName) {
return this.entityNameConvert(tableName) + "Service";
}
/**
* 自定義Controller類文件的名稱規(guī)則
*/
public String controllerNameConvert(String tableName) {
return this.entityNameConvert(tableName) + "Action";
}
除了Service、Controller、Entity、FieldName都可以實現(xiàn)自定義的擴展。下面是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;
/**
* 自定義各類名稱轉(zhuǎn)換的規(guī)則
*/
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()));
}
/**
* 自定義表字段名到實體類屬性名的轉(zhuǎn)換規(guī)則
*
* @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 在頁面上輸入的輸出文件標(biāo)識
* @param tableName 關(guān)聯(lián)的數(shù)據(jù)表名稱名稱
* @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目錄下,進行相應(yīng)的擴展開發(fā)。
修改完成后,需要另行編譯src\frontend中的靜態(tài)資源(源碼中不包含已編譯的頁面),在src\frontend文件夾中執(zhí)行:
yarn install
yarn run build
五、總結(jié)
以上就是今天要講的內(nèi)容,本文簡要介紹一款基于Mybatis-Plus的代碼自助生成器,地址:
https://github.com/davidfantasy/mybatis-plus-generator-ui
文章通過實例集成的方式來詳細講解mybatis-plus-generator-ui,從相關(guān)概念到實際集成案例,以及具體的擴展開發(fā)介紹。如果在工作中有這種需要,不妨采用這種方式。希望本文對您有所幫助,歡迎指導(dǎo)交流。
原文鏈接:https://blog.csdn.net/yelangkingwuzuhu/article/details/128077533
版權(quán)聲明:本文為CSDN博主「夜郎king」的原創(chuàng)文章,遵循CC 4.0 BY-SA版權(quán)協(xié)議,轉(zhuǎn)載請附上原文出處鏈接及本聲明。
近期熱文推薦:
1.1,000+ 道 Java面試題及答案整理(2022最新版)
2.勁爆!Java 協(xié)程要來了。。。
3.Spring Boot 2.x 教程,太全了!
4.別再寫滿屏的爆爆爆炸類了,試試裝飾器模式,這才是優(yōu)雅的方式??!
5.《Java開發(fā)手冊(嵩山版)》最新發(fā)布,速速下載!文章來源:http://www.zghlxwxcb.cn/news/detail-454689.html
覺得不錯,別忘了隨手點贊+轉(zhuǎn)發(fā)哦!文章來源地址http://www.zghlxwxcb.cn/news/detail-454689.html
到了這里,關(guān)于MyBatis-Plus 可視化代碼生成器來啦,讓你的開發(fā)效率大大提速??!的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!