1 import com.baomidou.mybatisplus.annotation.IdType; 2 import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException; 3 import com.baomidou.mybatisplus.core.toolkit.StringPool; 4 import com.baomidou.mybatisplus.core.toolkit.StringUtils; 5 import com.baomidou.mybatisplus.generator.AutoGenerator; 6 import com.baomidou.mybatisplus.generator.InjectionConfig; 7 import com.baomidou.mybatisplus.generator.config.*; 8 import com.baomidou.mybatisplus.generator.config.po.TableInfo; 9 import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy; 10 import com.baomidou.mybatisplus.generator.engine.VelocityTemplateEngine; 11 12 import java.util.ArrayList; 13 import java.util.List; 14 import java.util.Scanner; 15 16 public class Generator { 17 /** 18 * <p> 19 * 讀取控制台內容 20 * </p> 21 */ 22 public static String scanner(String tip) { 23 Scanner scanner = new Scanner(System.in); 24 StringBuilder help = new StringBuilder(); 25 help.append("請輸入" + tip + ":"); 26 System.out.println(help.toString()); 27 if (scanner.hasNext()) { 28 String ipt = scanner.next(); 29 if (StringUtils.isNotEmpty(ipt)) { 30 return ipt; 31 } 32 } 33 throw new MybatisPlusException("請輸入正確的" + tip + "!"); 34 } 35 36 37 public static void main(String[] args) { 38 39 // 代碼生成器 40 AutoGenerator mpg = new AutoGenerator(); 41 // 全局配置 42 GlobalConfig gc = new GlobalConfig(); 43 String projectPath = System.getProperty("user.dir"); 44 gc.setOutputDir(projectPath + "/wuage-dao/src/main/java"); 45 gc.setAuthor("binblink"); 46 47 //生成后是否打開文件夾 48 gc.setOpen(false); 49 //是否覆蓋之前的生成的同名文件 50 gc.setFileOverride(true); 51 52 gc.setBaseColumnList(true); 53 gc.setBaseResultMap(true); 54 gc.setMapperName("%sMapper"); 55 gc.setServiceName("%sService"); 56 gc.setServiceImplName("%sServiceImpl"); 57 gc.setIdType(IdType.UUID); 58 // gc.setEntityName("%s"); 59 // gc.setSwagger2(true); 實體屬性 Swagger2 注解 60 mpg.setGlobalConfig(gc); 61 62 // 數據源配置 63 DataSourceConfig dsc = new DataSourceConfig(); 64 dsc.setUrl("jdbc:mysql://localhost:3306/wuage?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8"); 65 // dsc.setSchemaName("public"); 66 dsc.setDriverName("com.mysql.cj.jdbc.Driver"); 67 dsc.setUsername("root"); 68 dsc.setPassword("123456"); 69 70 mpg.setDataSource(dsc); 71 72 // 包配置 73 PackageConfig pc = new PackageConfig(); 74 // pc.setModuleName(""); 75 pc.setParent("com.binblink"); 76 pc.setEntity("entity"); 77 pc.setMapper("mapper"); 78 pc.setService("service"); 79 pc.setServiceImpl("service.impl"); 80 // pc.setController(null); 81 // pc.setXml("resources"); 82 mpg.setPackageInfo(pc); 83 // FreemarkerTemplateEngine freemarkerTemplateEngine = new FreemarkerTemplateEngine(); 84 VelocityTemplateEngine velocityTemplateEngine = new VelocityTemplateEngine(); 85 // 自定義配置 86 InjectionConfig cfg = new InjectionConfig() { 87 @Override 88 public void initMap() { 89 90 } 91 }; 92 93 // // 自定義輸出配置 94 List<FileOutConfig> focList = new ArrayList<>(); 95 //自定義配置會被優先輸出 96 focList.add(new FileOutConfig("/codeTemplate/mapper.xml.vm") { 97 @Override 98 public String outputFile(TableInfo tableInfo) { 99 100 // 自定義輸出文件名 ,如果你 Entity 設置了前后綴、此處注意 xml 的名稱會跟着發生變化!! 101 return projectPath + "/wuage-dao/src/main/resources/mapper" 102 + "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML; 103 } 104 }); 105 // 106 // focList.add(new FileOutConfig(jspListTempla) { 107 // @Override 108 // public String outputFile(TableInfo tableInfo) { 109 // 110 // String filename = StringUtil.toLowerCaseFirstOne(tableInfo.getEntityName()); 111 // // 自定義輸出文件名 , 如果你 Entity 設置了前后綴、此處注意 xml 的名稱會跟着發生變化!! 112 // return projectPath + "/src/main/webapp/WEB-INF/jsp/"+ pc.getModuleName() 113 // + "/" + filename + "/"+filename +"_list.jsp"; 114 // } 115 // }); 116 // 117 // focList.add(new FileOutConfig(jspEditTempla) { 118 // @Override 119 // public String outputFile(TableInfo tableInfo) { 120 // 121 // String filename = StringUtil.toLowerCaseFirstOne(tableInfo.getEntityName()); 122 // 123 // return projectPath + "/src/main/webapp/WEB-INF/jsp/"+ pc.getModuleName() 124 // + "/" + filename + "/"+filename +"_edit.jsp"; 125 // } 126 // }); 127 // 128 // focList.add(new FileOutConfig(controllerTempla) { 129 // @Override 130 // public String outputFile(TableInfo tableInfo) { 131 // 132 // String filename = StringUtil.toLowerCaseFirstOne(tableInfo.getEntityName()); 133 // 134 // return projectPath + "/src/main/java/com/fh/controller/"+ pc.getModuleName() 135 // + "/" + filename + "/"+tableInfo.getEntityName() +"Controller"+StringPool.DOT_JAVA; 136 // } 137 // }); 138 //// 139 // cfg.setFileCreate(new IFileCreate() { 140 // @Override 141 // public boolean isCreate(ConfigBuilder configBuilder, FileType fileType, String filePath) { 142 // // 判斷自定義文件夾是否需要創建 143 // checkDir("調用默認方法創建的目錄"); 144 // return false; 145 // } 146 // }); 147 // 148 cfg.setFileOutConfigList(focList); 149 mpg.setCfg(cfg); 150 // 配置模板 151 TemplateConfig templateConfig = new TemplateConfig(); 152 153 // 配置自定義輸出模板 154 //指定自定義模板路徑,注意不要帶上.ftl/.vm, 會根據使用的模板引擎自動識別 ftl.myCodeTemple 155 // templateConfig.setEntity("templates/entity2.java"); 156 // templateConfig.setService("ftl/myCodeTemple/service.java"); 157 // templateConfig.setController("ftl/myCodeTemple/appcontroller.java"); 158 // templateConfig.setMapper("ftl/myCodeTemple/mapper.java"); 159 160 // templateConfig.setServiceImpl("ftl/myCodeTemple/serviceImpl.java"); 161 //控制是否按默認的來生成文件 設置為null則不生成默認的 162 templateConfig.setXml(null); 163 templateConfig.setController(null); 164 // templateConfig.setXml("/codeTemplate/mapper.xml"); 165 mpg.setTemplate(templateConfig); 166 167 // 策略配置 168 StrategyConfig strategy = new StrategyConfig(); 169 170 // strategy.setSuperEntityClass("com.baomidou.ant.common.BaseEntity"); 171 strategy.setEntityLombokModel(true); 172 // strategy.setRestControllerStyle(true); 173 174 strategy.setNaming(NamingStrategy.underline_to_camel); 175 strategy.setColumnNaming(NamingStrategy.underline_to_camel); 176 // 公共父類 177 // strategy.setSuperControllerClass("com.fh.controller.base.BaseController"); 178 // 寫於父類中的公共字段 179 // strategy.setSuperEntityColumns("id"); 180 strategy.setInclude(scanner("表名,多個英文逗號分割").split(",")); 181 // strategy.setControllerMappingHyphenStyle(true); 182 183 //去除表前綴 184 strategy.setTablePrefix("sys_"); 185 // strategy.setTablePrefix(pc.getModuleName() + "_"); 186 mpg.setStrategy(strategy); 187 mpg.setTemplateEngine(velocityTemplateEngine); 188 mpg.execute(); 189 } 190 }
上面的代碼 可以按自定義模板生成 取消默認生成的模塊 ,設置mapper.xml 路徑 去除代碼生成的表前綴
MP版本 3.2.0
ps.代碼比較亂 需要可以提煉一些參數為局部變量 這樣會清晰一點
引入日志的坑 引入這個就行了
<dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-simple</artifactId> <version>1.7.25</version> </dependency>
自定義的模板中的一些變量可以在源碼的templates 中參照默認模板設置;也可以在com.baomidou.mybatisplus.generator.engine.AbstractTemplateEngine 源碼中查看有哪些變量可以用
也可以在initMap() 自定義變量