1. 程式人生 > 其它 >MyBatisPlus的自動生成程式碼的策略詳解

MyBatisPlus的自動生成程式碼的策略詳解

MyBatisPlus之程式碼自動生成器

程式碼自動一鍵生成,功能強大,大大節省了開發時間
主要介紹一下springboot玩家,先來看配置的依賴maven
注意不要用最新版的依賴,很多東西新版以及移除了

<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.0.6</version>
</dependency>

<!-- 模板引擎 -->
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-engine-core</artifactId>
<version>2.0</version>
</dependency>

<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.6.1</version>
</dependency>

java測試類程式碼

public static void main(String[] args) {
//需要構建一個程式碼生成器物件
AutoGenerator mpg = new AutoGenerator();
//配置策略

//1、全域性配置
GlobalConfig gc = new GlobalConfig();
String prPath = System.getProperty("user.dir");//獲取當前系統目錄
gc.setOutputDir(prPath+"/src/main/java");//指定輸出的位置
gc.setAuthor("wangjinb");//設定作者
gc.setOpen(false);//是否開啟資源管理器
gc.setFileOverride(false);//是否覆蓋原來的檔案
gc.setServiceName("%sService");//去掉service的i字首
gc.setIdType(IdType.ID_WORKER);//設定id的生成策略預設演算法
gc.setDateType(DateType.ONLY_DATE);//設定日期生成策略
gc.setSwagger2(true);
mpg.setGlobalConfig(gc);

//2、設定資料來源”
DataSourceConfig dsc = new DataSourceConfig();
dsc.setUrl("jdbc:mysql://localhost:3306/blog?serverTimezone=UTC");
dsc.setDriverName("com.mysql.cj.jdbc.Driver");
dsc.setUsername("root");
dsc.setPassword("123456");
dsc.setDbType(DbType.MYSQL);//資料庫型別
mpg.setDataSource(dsc);

//3、配置包
PackageConfig pc = new PackageConfig();
pc.setModuleName("blog");//設定模組
pc.setParent("com.wj.testdemo");
pc.setEntity("entity");
pc.setMapper("mapper");
pc.setService("service");
pc.setController("controller");
mpg.setPackageInfo(pc);


// 4、策略配置
StrategyConfig strategy = new StrategyConfig();
strategy.setInclude("t_blog");//指定要對映的資料庫表,可以寫多個表,用“,”隔開
strategy.setNaming(NamingStrategy.underline_to_camel);//設定命名規則下劃線轉駝峰
strategy.setColumnNaming(NamingStrategy.underline_to_camel);//列名規則
strategy.setEntityLombokModel(true);//是否生成lombok註解

// strategy.setLogicDeleteFieldName("deleted");//邏輯刪除欄位配置
//自動填充的配置
TableFill create_time = new TableFill("create_time", FieldFill.INSERT);//設定時的生成策略
TableFill update_time = new TableFill("update_time", FieldFill.INSERT_UPDATE);//設定更新時間的生成策略
ArrayList<TableFill> list = new ArrayList<>();
list.add(create_time);
list.add(update_time);
strategy.setTableFillList(list);

//樂觀鎖
// strategy.setVersionFieldName("version");
strategy.setRestControllerStyle(true);//開啟駝峰命名
// strategy.setControllerMappingHyphenStyle(true);//開啟連結地址的下劃線命名 localhost:8080/hello_id_2
mpg.setStrategy(strategy);

mpg.execute();//執行
}

轉載部落格 https://blog.csdn.net/wangjinb/article/details/106488233