|
@@ -0,0 +1,193 @@
|
|
|
+package com.dayou;
|
|
|
+
|
|
|
+import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
|
|
|
+import com.baomidou.mybatisplus.core.toolkit.StringPool;
|
|
|
+import com.baomidou.mybatisplus.core.toolkit.StringUtils;
|
|
|
+import com.baomidou.mybatisplus.generator.AutoGenerator;
|
|
|
+import com.baomidou.mybatisplus.generator.InjectionConfig;
|
|
|
+import com.baomidou.mybatisplus.generator.config.*;
|
|
|
+import com.baomidou.mybatisplus.generator.config.po.TableInfo;
|
|
|
+import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
|
|
|
+import com.baomidou.mybatisplus.generator.engine.VelocityTemplateEngine;
|
|
|
+import com.dayou.common.BaseEntity;
|
|
|
+
|
|
|
+import java.util.*;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 代码生成器 基于mybatis-plus
|
|
|
+ */
|
|
|
+public class CodeGenerator {
|
|
|
+ /**
|
|
|
+ * <p>
|
|
|
+ * 读取控制台内容
|
|
|
+ * </p>
|
|
|
+ */
|
|
|
+ public static String scanner(String tip) {
|
|
|
+ Scanner scanner = new Scanner(System.in);
|
|
|
+ StringBuilder help = new StringBuilder();
|
|
|
+ help.append("请输入" + tip + ":");
|
|
|
+ System.out.println(help.toString());
|
|
|
+ if (scanner.hasNext()) {
|
|
|
+ String ipt = scanner.next();
|
|
|
+ if (StringUtils.isNotEmpty(ipt)) {
|
|
|
+ return ipt;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ throw new MybatisPlusException("请输入正确的" + tip + "!");
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void main(String[] args) {
|
|
|
+ // 代码生成器
|
|
|
+ AutoGenerator mpg = new AutoGenerator();
|
|
|
+
|
|
|
+ // 全局配置
|
|
|
+ GlobalConfig gc = new GlobalConfig();
|
|
|
+ final String projectPath = System.getProperty("user.dir");
|
|
|
+ //如果需要额外生成一个备份,则设置输出路径,否则直接设为null,因为下面每个模块已经单独设置了输出目录
|
|
|
+ //gc.setOutputDir(projectPath+"/gen");
|
|
|
+ gc.setOutputDir(null);
|
|
|
+ gc.setAuthor("mybatis-plus-generator");
|
|
|
+ gc.setOpen(true);
|
|
|
+ gc.setBaseResultMap(true);
|
|
|
+ gc.setBaseColumnList(true);
|
|
|
+ gc.setServiceName("%sService"); // 去掉 Service 接口的首字母 I
|
|
|
+ mpg.setGlobalConfig(gc);
|
|
|
+
|
|
|
+ // 数据源配置
|
|
|
+ DataSourceConfig dsc = new DataSourceConfig();
|
|
|
+ dsc.setUrl("jdbc:mysql://localhost:3306/productivity-platform?autoReconnect=true&useUnicode=true&zeroDateTimeBehavior=CONVERT_TO_NULL&useSSL=false&serverTimezone=Asia/Shanghai&allowMultiQueries=true&characterEncoding=UTF-8&nullCatalogMeansCurrent=true&rewriteBatchedStatements=true");
|
|
|
+ // dsc.setSchemaName("public");
|
|
|
+ dsc.setDriverName("com.mysql.jdbc.Driver");
|
|
|
+ dsc.setUsername("root");
|
|
|
+ dsc.setPassword("914851221");
|
|
|
+ mpg.setDataSource(dsc);
|
|
|
+
|
|
|
+
|
|
|
+ // 包配置
|
|
|
+ final PackageConfig pc = new PackageConfig();
|
|
|
+// pc.setController(".controller.gen");
|
|
|
+ pc.setController("controller");
|
|
|
+// pc.setModuleName(scanner("模块名"));
|
|
|
+ String basePkg = "com.dayou";
|
|
|
+ pc.setParent(basePkg);
|
|
|
+ mpg.setPackageInfo(pc);
|
|
|
+
|
|
|
+
|
|
|
+ // 自定义配置
|
|
|
+ /*InjectionConfig cfg = new InjectionConfig() {
|
|
|
+ @Override
|
|
|
+ public void initMap() {
|
|
|
+ // to do nothing
|
|
|
+ Map<String, Object> map = new HashMap<>();
|
|
|
+ map.put("resp", basePkg + ".common.RestResponse");
|
|
|
+ map.put("pullDownModel", basePkg + ".common.PullDownModel");
|
|
|
+ map.put("basePkg", basePkg);
|
|
|
+ this.setMap(map);
|
|
|
+ }
|
|
|
+ };*/
|
|
|
+
|
|
|
+ // 如果模板引擎是 freemarker
|
|
|
+// String templatePath = "/templates/mapper.xml.ftl";
|
|
|
+ // 如果模板引擎是 velocity
|
|
|
+ String templatePath = "template/mapper.xml.vm";
|
|
|
+ String entityTemplatePath = "template/entity.java.vm";
|
|
|
+ String serviceTemplatePath = "template/service.java.vm";
|
|
|
+ String serviceImplTemplatePath = "template/serviceImpl.java.vm";
|
|
|
+ String controllerTemplatePath = "template/controller.java.vm";
|
|
|
+ String mapperTemplatePath = "template/mapper.java.vm";
|
|
|
+
|
|
|
+ // 自定义输出配置,输出的文件不会覆盖现有文件
|
|
|
+ List<FileOutConfig> focList = new ArrayList<>();
|
|
|
+
|
|
|
+ // 自定义配置会被优先输出
|
|
|
+ focList.add(new FileOutConfig(templatePath) {
|
|
|
+ @Override
|
|
|
+ public String outputFile(TableInfo tableInfo) {
|
|
|
+ // 自定义输出文件名 , 如果你 Entity 设置了前后缀、此处注意 xml 的名称会跟着发生变化!!
|
|
|
+ return projectPath + "/dao" + "/src/main/resources/mapper/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ String basePkgPath = "/com/dayou";
|
|
|
+ focList.add(new FileOutConfig(entityTemplatePath) {
|
|
|
+ @Override
|
|
|
+ public String outputFile(TableInfo tableInfo) {
|
|
|
+ // 自定义输出文件名 , 如果你 Entity 设置了前后缀、此处注意 xml 的名称会跟着发生变化!!
|
|
|
+ return projectPath + "/domain" + "/src/main/java" + basePkgPath + "/entity/" + tableInfo.getEntityName() + StringPool.DOT_JAVA;
|
|
|
+ }
|
|
|
+ });
|
|
|
+ focList.add(new FileOutConfig(serviceTemplatePath) {
|
|
|
+ @Override
|
|
|
+ public String outputFile(TableInfo tableInfo) {
|
|
|
+ // 自定义输出文件名 , 如果你 Entity 设置了前后缀、此处注意 xml 的名称会跟着发生变化!!
|
|
|
+ return projectPath + "/service" + "/src/main/java" + basePkgPath + "/service/" + tableInfo.getServiceName() + StringPool.DOT_JAVA;
|
|
|
+ }
|
|
|
+ });
|
|
|
+ focList.add(new FileOutConfig(serviceImplTemplatePath) {
|
|
|
+ @Override
|
|
|
+ public String outputFile(TableInfo tableInfo) {
|
|
|
+ // 自定义输出文件名 , 如果你 Entity 设置了前后缀、此处注意 xml 的名称会跟着发生变化!!
|
|
|
+ return projectPath + "/service" + "/src/main/java" + basePkgPath + "/service/impl/" + tableInfo.getServiceImplName() + StringPool.DOT_JAVA;
|
|
|
+ }
|
|
|
+ });
|
|
|
+ focList.add(new FileOutConfig(controllerTemplatePath) {
|
|
|
+ @Override
|
|
|
+ public String outputFile(TableInfo tableInfo) {
|
|
|
+ // 自定义输出文件名 , 如果你 Entity 设置了前后缀、此处注意 xml 的名称会跟着发生变化!!
|
|
|
+ return projectPath + "/biz-base" + "/src/main/java" + basePkgPath + "/controller/" + tableInfo.getControllerName() + StringPool.DOT_JAVA;
|
|
|
+ //return projectPath + "/biz-custom/biz-education" + "/src/main/java" + basePkgPath + "/controller/" + tableInfo.getControllerName() + StringPool.DOT_JAVA;
|
|
|
+ }
|
|
|
+ });
|
|
|
+ focList.add(new FileOutConfig(mapperTemplatePath) {
|
|
|
+ @Override
|
|
|
+ public String outputFile(TableInfo tableInfo) {
|
|
|
+ // 自定义输出文件名 , 如果你 Entity 设置了前后缀、此处注意 xml 的名称会跟着发生变化!!
|
|
|
+ return projectPath + "/dao" + "/src/main/java" + basePkgPath + "/mapper/" + tableInfo.getMapperName() + StringPool.DOT_JAVA;
|
|
|
+ }
|
|
|
+ });
|
|
|
+ /*
|
|
|
+ cfg.setFileCreate(new IFileCreate() {
|
|
|
+ @Override
|
|
|
+ public boolean isCreate(ConfigBuilder configBuilder, FileType fileType, String filePath) {
|
|
|
+ // 判断自定义文件夹是否需要创建
|
|
|
+ checkDir("调用默认方法创建的目录");
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ });
|
|
|
+ */
|
|
|
+ /*cfg.setFileOutConfigList(focList);
|
|
|
+ mpg.setCfg(cfg);*/
|
|
|
+
|
|
|
+ // 配置模板
|
|
|
+ TemplateConfig templateConfig = new TemplateConfig();
|
|
|
+ // 配置自定义输出模板
|
|
|
+ //指定自定义模板路径,注意不要带上.ftl/.vm, 会根据使用的模板引擎自动识别
|
|
|
+ templateConfig.setEntity("template/entity.java");
|
|
|
+ templateConfig.setService("template/service.java");
|
|
|
+ templateConfig.setServiceImpl("template/serviceImpl.java");
|
|
|
+ templateConfig.setController("template/controller.java");
|
|
|
+ templateConfig.setXml("template/mapper.xml");
|
|
|
+ templateConfig.setMapper("template/mapper.java");
|
|
|
+ mpg.setTemplate(templateConfig);
|
|
|
+
|
|
|
+ // 策略配置
|
|
|
+ StrategyConfig strategy = new StrategyConfig();
|
|
|
+ strategy.setNaming(NamingStrategy.underline_to_camel);
|
|
|
+ strategy.setColumnNaming(NamingStrategy.underline_to_camel);
|
|
|
+ strategy.setSuperEntityClass(BaseEntity.class);
|
|
|
+ strategy.setSuperMapperClass("com.baomidou.mybatisplus.core.mapper.BaseMapper");
|
|
|
+ strategy.setEntityLombokModel(true);
|
|
|
+ strategy.setRestControllerStyle(true);
|
|
|
+ strategy.setControllerMappingHyphenStyle(true);
|
|
|
+ // 公共父类
|
|
|
+// strategy.setSuperControllerClass(basePkg + ".controller.BaseController");
|
|
|
+ // 写于父类中的公共字段
|
|
|
+ strategy.setSuperEntityColumns("id", "create_time", "update_time", "delete_status");
|
|
|
+ strategy.setInclude(scanner("表名,多个英文逗号分割").split(","));
|
|
|
+ strategy.setControllerMappingHyphenStyle(true);
|
|
|
+ strategy.setTablePrefix(pc.getModuleName() + "_");
|
|
|
+ mpg.setStrategy(strategy);
|
|
|
+ mpg.setTemplateEngine(new VelocityTemplateEngine());
|
|
|
+ mpg.execute();
|
|
|
+ }
|
|
|
+}
|