Browse Source

完善自动生成工具

yq 3 years ago
parent
commit
716989b5e9

+ 129 - 0
技术分享/MybatisGeneratorUtils.java

@@ -0,0 +1,129 @@
+package com.bizmatics.controller.web;
+
+import com.baomidou.mybatisplus.core.toolkit.StringPool;
+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 java.io.File;
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * @author yq
+ * @date 2021/7/6 11:42
+ */
+public class TestUtils {
+    public static void main(String[] args) {
+        //修改成自己的模块名称
+        String[] models = {"test-controller", "test-service", "test-model", "test-persistence"};
+        for (String model : models) {
+            shell(model);
+        }
+    }
+
+    private static void shell(String model) {
+
+        AutoGenerator mpg = new AutoGenerator();
+        //1、全局配置
+        GlobalConfig gc = new GlobalConfig();
+        File file = new File(model);
+        String path = file.getAbsolutePath();
+        gc.setOutputDir(path + "/src/main/java");  //生成路径(一般都是生成在此项目的src/main/java下面)
+        //修改为自己的名字
+        gc.setAuthor("ya"); //设置作者
+        gc.setOpen(false);
+        gc.setFileOverride(true); //第二次生成会把第一次生成的覆盖掉
+        gc.setServiceName("%sService"); //生成的service接口名字首字母是否为I,这样设置就没有
+        gc.setBaseResultMap(true); //生成resultMap
+        mpg.setGlobalConfig(gc);
+
+        //2、数据源配置
+        //修改数据源
+        DataSourceConfig dsc = new DataSourceConfig();
+        dsc.setUrl("jdbc:mysql://localhost:3306/test?useUnicode=true&serverTimezone=GMT&useSSL=false&characterEncoding=utf8");
+        dsc.setDriverName("com.mysql.jdbc.Driver");
+        dsc.setUsername("root");
+        dsc.setPassword(null);
+        mpg.setDataSource(dsc);
+
+        // 3、包配置
+        PackageConfig pc = new PackageConfig();
+        pc.setParent("com.bizmatics");
+        pc.setController("controller.web");
+        pc.setEntity("model");
+        pc.setMapper("persistence.mapper");
+        pc.setService("service");
+        pc.setServiceImpl("service.impl");
+        //pc.setModuleName("test");
+        mpg.setPackageInfo(pc);
+
+        // 4、策略配置
+        StrategyConfig strategy = new StrategyConfig();
+        strategy.setNaming(NamingStrategy.underline_to_camel);
+        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
+        strategy.setSuperMapperClass("com.bizmatics.common.mvc.base.CrudMapper");
+        strategy.setSuperServiceClass("com.bizmatics.common.mvc.base.CrudService");
+        strategy.setSuperServiceImplClass("com.bizmatics.common.mvc.base.AbstractCrudService");
+        // strategy.setTablePrefix("t_"); // 表名前缀
+        strategy.setEntityLombokModel(true); //使用lombok
+        //修改自己想要生成的表
+        strategy.setInclude("test");  // 逆向工程使用的表   如果要生成多个,这里可以传入String[]
+        mpg.setStrategy(strategy);
+
+        // 关闭默认 xml 生成,调整生成 至 根目录
+        //修改对应的模块名称
+        TemplateConfig tc = new TemplateConfig();
+        if ("test-persistence".equals(model)) {
+            tc.setController(null);
+            tc.setEntity(null);
+            tc.setService(null);
+            tc.setServiceImpl(null);
+            // 自定义配置
+            InjectionConfig cfg = new InjectionConfig() {
+                @Override
+                public void initMap() {
+                    // to do nothing
+                }
+            };
+            //如果模板引擎是 velocity
+            String templatePath = "/templates/mapper.xml.vm";
+            // 自定义输出配置
+            List<FileOutConfig> focList = new ArrayList<>();
+            // 自定义配置会被优先输出
+            focList.add(new FileOutConfig(templatePath) {
+                @Override
+                public String outputFile(TableInfo tableInfo) {
+                    // 自定义输出文件名 , 如果你 Entity 设置了前后缀、此处注意 xml 的名称会跟着发生变化!!
+                    return path + "/src/main/resources/mapper/mysql/" + "/"
+                            + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
+                }
+            });
+            cfg.setFileOutConfigList(focList);
+            mpg.setCfg(cfg);
+            tc.setXml(null);
+        } else if ("test-model".equals(model)) {
+            tc.setController(null);
+            tc.setService(null);
+            tc.setServiceImpl(null);
+            tc.setMapper(null);
+            tc.setXml(null);
+        } else if ("test-service".equals(model)) {
+            tc.setController(null);
+            tc.setMapper(null);
+            tc.setXml(null);
+            tc.setEntity(null);
+        } else if ("test-controller".equals(model)) {
+            tc.setMapper(null);
+            tc.setXml(null);
+            tc.setService(null);
+            tc.setServiceImpl(null);
+            tc.setEntity(null);
+        }
+        mpg.setTemplate(tc);
+        //5、执行
+        mpg.execute();
+    }
+}

+ 1 - 100
技术分享/manven多模块+mybatisplus自动生成代码

@@ -10,106 +10,7 @@
             <version>2.1</version>
         </dependency>
 
-package com.bizmatics.fiveep.controller;
-
-
-import com.baomidou.mybatisplus.generator.AutoGenerator;
-import com.baomidou.mybatisplus.generator.config.*;
-import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
-
-import java.io.File;
-
-/**
- * @author yq
- * @date 2021/7/5 16:38
- */
-public class TestUtils {
-    public static void main(String[] args) {
-        String[] models = {"fiveep-controller", "fiveep-service", "fiveep-model", "fiveep-persistence"};
-        for (String model : models) {
-            shell(model);
-        }
-    }
-
-    private static void shell(String model) {
-
-        AutoGenerator mpg = new AutoGenerator();
-        //1、全局配置
-        GlobalConfig gc = new GlobalConfig();
-        File file = new File(model);
-        String path = file.getAbsolutePath();
-        gc.setOutputDir(path + "/src/main/java");  //生成路径(一般都是生成在此项目的src/main/java下面)
-        //修改
-        gc.setAuthor("ya"); //设置作者
-        gc.setOpen(false);
-        gc.setFileOverride(true); //第二次生成会把第一次生成的覆盖掉
-        gc.setServiceName("%sService"); //生成的service接口名字首字母是否为I,这样设置就没有
-        gc.setBaseResultMap(true); //生成resultMap
-        mpg.setGlobalConfig(gc);
-
-        //2、数据源配置
-        //修改
-        DataSourceConfig dsc = new DataSourceConfig();
-        dsc.setUrl("jdbc:mysql://localhost:3306/test?useUnicode=true&serverTimezone=GMT&useSSL=false&characterEncoding=utf8");
-        dsc.setDriverName("com.mysql.jdbc.Driver");
-        dsc.setUsername("root");
-        dsc.setPassword(null);
-        mpg.setDataSource(dsc);
-
-        // 3、包配置
-        //修改
-        PackageConfig pc = new PackageConfig();
-        pc.setParent("com.bizmatics");
-        pc.setController("controller.web");
-        pc.setEntity("model");
-        pc.setMapper("persistence.mapper");
-        pc.setService("service");
-        pc.setServiceImpl("service.impl");
-        //pc.setModuleName("test");
-        mpg.setPackageInfo(pc);
-
-        // 4、策略配置
-        StrategyConfig strategy = new StrategyConfig();
-        strategy.setNaming(NamingStrategy.underline_to_camel);
-        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
-        strategy.setSuperMapperClass("com.bizmatics.common.mvc.base.CrudMapper");
-        strategy.setSuperServiceClass("com.bizmatics.common.mvc.base.CrudService");
-        strategy.setSuperServiceImplClass("com.bizmatics.common.mvc.base.AbstractCrudService");
-        // strategy.setTablePrefix("t_"); // 表名前缀
-        strategy.setEntityLombokModel(true); //使用lombok
-        strategy.setInclude("test");  // 逆向工程使用的表   如果要生成多个,这里可以传入String[]
-        mpg.setStrategy(strategy);
-
-        // 关闭默认 xml 生成,调整生成 至 根目录
-        TemplateConfig tc = new TemplateConfig();
-        if ("fiveep-persistence".equals(model)) {
-            tc.setController(null);
-            tc.setEntity(null);
-            tc.setService(null);
-            tc.setServiceImpl(null);
-        } else if ("fiveep-model".equals(model)) {
-            tc.setController(null);
-            tc.setService(null);
-            tc.setServiceImpl(null);
-            tc.setMapper(null);
-            tc.setXml(null);
-        } else if ("fiveep-service".equals(model)) {
-            tc.setController(null);
-            tc.setMapper(null);
-            tc.setXml(null);
-            tc.setEntity(null);
-        } else if ("fiveep-controller".equals(model)) {
-            tc.setMapper(null);
-            tc.setXml(null);
-            tc.setService(null);
-            tc.setServiceImpl(null);
-            tc.setEntity(null);
-        }
-        mpg.setTemplate(tc);
-        //5、执行
-        mpg.execute();
-    }
-}
+2 把MybatisGeneratorUtils.java放到自己的项目,修改配置
 参考链接
 https://blog.csdn.net/u010559460/article/details/103945985/