<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>
新建程序入口,以main函數的方式運行
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/test") .userName("rrr1").password("rrr1").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.mybatisplustools.example").port(8068).build(); MybatisPlusToolsApplication.run(config); } }
在輸出的日誌中,可以看到程序的運行端口,以及默認的模板目錄地址。在瀏覽器中輸入訪問地址http://localhost:8068/
輸出配置
內置Entity、Mapper、Service、Controller
等6種類型代碼的模板配置,可以上傳模板進行替換,並修改各類參數,配置參數已經按照影響的文件類型重新進行了分類,並加入了部分文本說明;也可以自行添加其它類型的自定義輸出文件。所有的配置項都會按照項目包名進行保存,只需一次性設置就可以了。
3、策略配置
將每次生成代碼時可能變動的內容加入到代碼生成選項中,方便調整每次的生成策略,比如:是否覆蓋原文件,生成文件的種類等等:
4、SQL配置生成
通過輸入查詢SQL,可自動在Mapper(Xml及Java)中生成對應的查詢方法,DTO對象和ResultMap(結果集映射配置)
5、代碼生成