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() 自定义变量
原文:https://www.cnblogs.com/Binblink/p/12001105.html