mybatis_plus程式碼自動生成外掛配置
阿新 • • 發佈:2020-08-14
mybatis-plus程式碼自動生成配置
1、匯入模板引擎依賴
<!-- 使用程式碼生成器要匯入模板引擎--> <!-- https://mvnrepository.com/artifact/org.apache.velocity/velocity-engine-core --> <dependency> <groupId>org.apache.velocity</groupId> <artifactId>velocity-engine-core</artifactId> <version>2.2</version> </dependency> <!-- io.swagger --> <!-- https://mvnrepository.com/artifact/io.swagger/swagger-annotations --> <dependency> <groupId>io.swagger</groupId> <artifactId>swagger-annotations</artifactId> <version>1.5.22</version> </dependency>
2、編輯執行類
public static void main(String[] args) { // 需要構建一個 程式碼自動生成器物件 AutoGenerator mpg = new AutoGenerator(); // 配置策略 // 1、全域性配置 GlobalConfig gc = new GlobalConfig(); String projectPath = System.getProperty("user.dir"); gc.setOutputDir(projectPath + "/src/main/java"); gc.setAuthor("bingbing"); gc.setOpen(false); gc.setFileOverride(false); // 是否覆蓋 gc.setServiceName("%sService"); // 去Service的I字首 gc.setIdType(IdType.ID_WORKER); //配置主鍵自增策略 gc.setDateType(DateType.ONLY_DATE); //設定日期型別,用的java.util下的 gc.setSwagger2(true); mpg.setGlobalConfig(gc); //2、設定資料來源 DataSourceConfig dsc = new DataSourceConfig(); dsc.setUrl("jdbc:mysql://localhost:3306/mybatis_plus?Unicode=true&UseSSL=false&characterEncoding=utf-8&serverTimezone=Asia/Shanghai"); dsc.setDriverName("com.mysql.cj.jdbc.Driver"); dsc.setUsername("root"); dsc.setPassword("root"); dsc.setDbType(DbType.MYSQL); mpg.setDataSource(dsc); //3、包的配置 PackageConfig pc = new PackageConfig(); pc.setModuleName("blog"); pc.setParent("com.bing"); pc.setEntity("entity"); pc.setMapper("mapper"); pc.setService("service"); pc.setController("controller"); mpg.setPackageInfo(pc); //4、策略配置 StrategyConfig strategy = new StrategyConfig(); strategy.setInclude("user"); // 設定要對映的表名,可以傳多個 strategy.setNaming(NamingStrategy.underline_to_camel); //下劃線配置 strategy.setColumnNaming(NamingStrategy.underline_to_camel); //下劃線配置 strategy.setEntityLombokModel(true); // 自動lombok; strategy.setLogicDeleteFieldName("deleted"); //邏輯刪除配置 // 自動填充配置 TableFill gmtCreate = new TableFill("gmt_create", FieldFill.INSERT); TableFill gmtModified = new TableFill("gmt_modified", FieldFill.INSERT_UPDATE); ArrayList<TableFill> tableFills = new ArrayList<>(); tableFills.add(gmtCreate); tableFills.add(gmtModified); strategy.setTableFillList(tableFills); // 樂觀鎖 strategy.setVersionFieldName("version"); strategy.setRestControllerStyle(true); strategy.setControllerMappingHyphenStyle(true); //localhost:8080 / hello_id_2 mpg.setStrategy(strategy); mpg.execute(); }