MyBatisPlus3.x中使用代码生成器(全注释)
生活随笔
收集整理的這篇文章主要介紹了
MyBatisPlus3.x中使用代码生成器(全注释)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
場景
MyBaitsPlus3.x與2.x是不一樣的。這里使用3.0.1版本。
官方文檔
https://mp.baomidou.com/guide/generator.html#%E4%BD%BF%E7%94%A8%E6%95%99%E7%A8%8B
這里在IDEA上的SpringBoot項目中進行代碼生成測試。
實現
添加依賴
添加 代碼生成器 依賴
<dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-generator</artifactId><version>3.2.0</version> </dependency>注意:MyBatis-Plus 從 3.0.3 之后移除了代碼生成器與模板引擎的默認依賴,需要手動添加相關依賴
<!-- 模板引擎velocity start--><dependency><groupId>org.apache.velocity</groupId><artifactId>velocity-engine-core</artifactId><version>2.0</version></dependency><!-- 模板引擎velocity end--><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>3.0.1</version></dependency>模板引擎
MyBatis-Plus 支持 Velocity(默認)、Freemarker、Beetl,用戶可以選擇自己熟悉的模板引擎。
注意!如果您選擇了非默認引擎,需要在 AutoGenerator 中 設置模板引擎。
AutoGenerator generator = new AutoGenerator();// set freemarker engine generator.setTemplateEngine(new FreemarkerTemplateEngine());// set beetl engine generator.setTemplateEngine(new BeetlTemplateEngine());// set custom engine (reference class is your custom engine class) generator.setTemplateEngine(new CustomTemplateEngine());編寫代碼
在test下新建單元測試類Generatortest.java
全局配置
?//全局配置GlobalConfig config = new GlobalConfig();//設置是否支持AR模式config.setActiveRecord(true)//設置生成代碼的作者.setAuthor("badaoliumangqizhi")//設置輸出代碼的位置.setOutputDir("f:output")//.setEnableCache(false)// XML 二級緩存//.setBaseResultMap(true)// XML ResultMap//.setBaseColumnList(true)// XML columList//.setKotlin(true) 是否生成 kotlin 代碼//設置是否覆蓋原來的代碼.setFileOverride(true);數據源配置
//數據庫連接urlString dbUrl = "jdbc:sqlserver://;DatabaseName=";//數據源配置DataSourceConfig dataSourceConfig = new DataSourceConfig();//數據庫類型 枚舉dataSourceConfig.setDbType(DbType.SQL_SERVER)//設置url.setUrl(dbUrl)//設置用戶名.setUsername("")//設置密碼.setPassword("")//設置數據庫驅動.setDriverName("com.microsoft.sqlserver.jdbc.SQLServerDriver")// 自定義數據庫表字段類型轉換【可選】.setTypeConvert(new MySqlTypeConvert() {@Overridepublic DbColumnType processTypeConvert(GlobalConfig globalConfig, String fieldType) {System.out.println("轉換類型:" + fieldType);//tinyint轉換成Booleanif ( fieldType.toLowerCase().contains( "tinyint" ) ) {return DbColumnType.BOOLEAN;}//將數據庫中datetime轉換成dateif ( fieldType.toLowerCase().contains( "datetime" ) ) {return DbColumnType.DATE;}return (DbColumnType) super.processTypeConvert(globalConfig, fieldType);}});策略配置
//策略配置StrategyConfig strategyConfig = new StrategyConfig();strategyConfig//全局大寫命名是否開啟.setCapitalMode(true)//【實體】是否為lombok模型.setEntityLombokModel(true)//表名生成策略? 下劃線轉駝峰.setNaming(NamingStrategy.underline_to_camel)//自動填充設置//.setTableFillList(tableFillList)//修改替換成你需要的表名,多個表名傳數組.setInclude("wms_receive_order");集成注入配置
//注入全局設置new AutoGenerator().setGlobalConfig(config)//注入數據源配置.setDataSource(dataSourceConfig)//注入策略配置.setStrategy(strategyConfig)//設置包名信息.setPackageInfo(new PackageConfig()//提取公共父級包名.setParent("com.badao.bus.sys")//設置controller信息.setController("controller")//設置實體類信息.setEntity("entity"))//設置自定義模板.setTemplate(new TemplateConfig()//.setXml(null)//指定自定義模板路徑, 位置:/resources/templates/entity2.java.ftl(或者是.vm)//注意不要帶上.ftl(或者是.vm), 會根據使用的模板引擎自動識別// 自定義模板配置,模板可以參考源碼 /mybatis-plus/src/main/resources/template 使用 copy// 至您項目 src/main/resources/template 目錄下,模板名稱也可自定義如下配置:// .setController("...");// .setEntity("...");// .setMapper("...");// .setXml("...");// .setService("...");.setServiceImpl("templates/serviceImpl.java"))//開始執行代碼生成.execute();}完整生成器代碼
package com.ws.test.generator;import com.baomidou.mybatisplus.annotation.DbType; import com.baomidou.mybatisplus.annotation.FieldFill; import com.baomidou.mybatisplus.generator.config.*; import com.baomidou.mybatisplus.generator.config.converts.MySqlTypeConvert; import com.baomidou.mybatisplus.generator.config.po.TableFill; import com.baomidou.mybatisplus.generator.config.rules.DbColumnType; import org.junit.Test; import com.baomidou.mybatisplus.generator.AutoGenerator; import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;import java.util.ArrayList; import java.util.List;/*** Created by badado on 2019/4/25.*/ public class Generatortest {@Testpublic void generateTest(){//全局配置GlobalConfig config = new GlobalConfig();//設置是否支持AR模式config.setActiveRecord(true)//設置生成代碼的作者.setAuthor("badaoliumangqizhi")//設置輸出代碼的位置.setOutputDir("f:output")//.setEnableCache(false)// XML 二級緩存//.setBaseResultMap(true)// XML ResultMap//.setBaseColumnList(true)// XML columList//.setKotlin(true) 是否生成 kotlin 代碼//設置是否覆蓋原來的代碼.setFileOverride(true);//******************************數據源配置***************************************//數據庫連接urlString dbUrl = "jdbc:sqlserver://;DatabaseName=";//數據源配置DataSourceConfig dataSourceConfig = new DataSourceConfig();//數據庫類型 枚舉dataSourceConfig.setDbType(DbType.SQL_SERVER)//設置url.setUrl(dbUrl)//設置用戶名.setUsername("")//設置密碼.setPassword("")//設置數據庫驅動.setDriverName("com.microsoft.sqlserver.jdbc.SQLServerDriver")// 自定義數據庫表字段類型轉換【可選】.setTypeConvert(new MySqlTypeConvert() {@Overridepublic DbColumnType processTypeConvert(GlobalConfig globalConfig, String fieldType) {System.out.println("轉換類型:" + fieldType);//tinyint轉換成Booleanif ( fieldType.toLowerCase().contains( "tinyint" ) ) {return DbColumnType.BOOLEAN;}//將數據庫中datetime轉換成dateif ( fieldType.toLowerCase().contains( "datetime" ) ) {return DbColumnType.DATE;}return (DbColumnType) super.processTypeConvert(globalConfig, fieldType);}});//******************************策略配置******************************************************// 自定義需要填充的字段 數據庫中的字段List<TableFill> tableFillList = new ArrayList<>();tableFillList.add(new TableFill("gmt_modified", FieldFill.INSERT_UPDATE));tableFillList.add(new TableFill("modifier_id", FieldFill.INSERT_UPDATE));tableFillList.add(new TableFill("creator_id", FieldFill.INSERT));tableFillList.add(new TableFill("gmt_creat", FieldFill.INSERT));tableFillList.add(new TableFill("available_flag", FieldFill.INSERT));tableFillList.add(new TableFill("deleted_flag", FieldFill.INSERT));tableFillList.add(new TableFill("sync_flag", FieldFill.INSERT));//策略配置StrategyConfig strategyConfig = new StrategyConfig();strategyConfig//全局大寫命名是否開啟.setCapitalMode(true)//【實體】是否為lombok模型.setEntityLombokModel(true)//表名生成策略? 下劃線轉駝峰.setNaming(NamingStrategy.underline_to_camel)//自動填充設置.setTableFillList(tableFillList)//修改替換成你需要的表名,多個表名傳數組.setInclude("wms_receive_order");//集成注入設置//注入全局設置new AutoGenerator().setGlobalConfig(config)//注入數據源配置.setDataSource(dataSourceConfig)//注入策略配置.setStrategy(strategyConfig)//設置包名信息.setPackageInfo(new PackageConfig()//提取公共父級包名.setParent("com.badao.bus.sys")//設置controller信息.setController("controller")//設置實體類信息.setEntity("entity"))//設置自定義模板.setTemplate(new TemplateConfig()//.setXml(null)//指定自定義模板路徑, 位置:/resources/templates/entity2.java.ftl(或者是.vm)//注意不要帶上.ftl(或者是.vm), 會根據使用的模板引擎自動識別// 自定義模板配置,模板可以參考源碼 /mybatis-plus/src/main/resources/template 使用 copy// 至您項目 src/main/resources/template 目錄下,模板名稱也可自定義如下配置:// .setController("...");// .setEntity("...");// .setMapper("...");// .setXml("...");// .setService("...");.setServiceImpl("templates/serviceImpl.java"))//開始執行代碼生成.execute();} }效果
運行測試
生成成功后會自動彈出生成代碼的目錄
?
總結
以上是生活随笔為你收集整理的MyBatisPlus3.x中使用代码生成器(全注释)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SpringBoot中使用POI导出Ex
- 下一篇: MyBatisPLus3.x中代码生成器