Browse Source

支持自定义径生成各个模块的路径

hubin 7 years ago
parent
commit
9e8f669c0f

+ 13 - 71
mybatis-plus-generator/src/main/java/com/baomidou/mybatisplus/generator/config/PackageConfig.java

@@ -16,8 +16,13 @@
 package com.baomidou.mybatisplus.generator.config;
 
 
+import java.util.Map;
+
 import com.baomidou.mybatisplus.core.toolkit.StringUtils;
 
+import lombok.Data;
+import lombok.experimental.Accessors;
+
 /**
  * <p>
  * 跟包相关的配置项
@@ -25,6 +30,9 @@ import com.baomidou.mybatisplus.core.toolkit.StringUtils;
  * @author YangHu, tangguo, hubin
  * @since 2016-08-30
  */
+
+@Data
+@Accessors(chain = true)
 public class PackageConfig {
 
     /**
@@ -66,6 +74,11 @@ public class PackageConfig {
      */
     private String controller = "controller";
 
+    /**
+     * 路径配置信息
+     */
+    private Map<String, String> pathInfo;
+
     public String getParent() {
         if (StringUtils.isNotEmpty(moduleName)) {
             return parent + "." + moduleName;
@@ -73,75 +86,4 @@ public class PackageConfig {
         return parent;
     }
 
-    public PackageConfig setParent(String parent) {
-        this.parent = parent;
-        return this;
-    }
-
-    public String getModuleName() {
-        return moduleName;
-    }
-
-    public PackageConfig setModuleName(String moduleName) {
-        this.moduleName = moduleName;
-        return this;
-    }
-
-    public String getEntity() {
-        return entity;
-    }
-
-    public PackageConfig setEntity(String entity) {
-        this.entity = entity;
-        return this;
-    }
-
-    public String getService() {
-        return service;
-    }
-
-    public PackageConfig setService(String service) {
-        this.service = service;
-        return this;
-    }
-
-    public String getServiceImpl() {
-        return serviceImpl;
-    }
-
-    public PackageConfig setServiceImpl(String serviceImpl) {
-        this.serviceImpl = serviceImpl;
-        return this;
-    }
-
-    public String getMapper() {
-        return mapper;
-    }
-
-    public PackageConfig setMapper(String mapper) {
-        this.mapper = mapper;
-        return this;
-    }
-
-    public String getXml() {
-        return xml;
-    }
-
-    public PackageConfig setXml(String xml) {
-        this.xml = xml;
-        return this;
-    }
-
-    public String getController() {
-        if (StringUtils.isEmpty(controller)) {
-            return "web";
-        }
-        return controller;
-    }
-
-    public PackageConfig setController(String controller) {
-        this.controller = controller;
-        return this;
-    }
-
 }

+ 29 - 36
mybatis-plus-generator/src/main/java/com/baomidou/mybatisplus/generator/config/builder/ConfigBuilder.java

@@ -171,12 +171,6 @@ public class ConfigBuilder {
     }
 
 
-    public ConfigBuilder setPathInfo(Map<String, String> pathInfo) {
-        this.pathInfo = pathInfo;
-        return this;
-    }
-
-
     public String getSuperEntityClass() {
         return superEntityClass;
     }
@@ -249,40 +243,39 @@ public class ConfigBuilder {
      * @param config    PackageConfig
      */
     private void handlerPackage(TemplateConfig template, String outputDir, PackageConfig config) {
-        if (null == pathInfo) {
-            // 包信息
-            packageInfo = new HashMap<>(6);
-            packageInfo.put(ConstVal.ENTITY, joinPackage(config.getParent(), config.getEntity()));
-            packageInfo.put(ConstVal.MAPPER, joinPackage(config.getParent(), config.getMapper()));
-            packageInfo.put(ConstVal.XML, joinPackage(config.getParent(), config.getXml()));
-            packageInfo.put(ConstVal.SERVICE, joinPackage(config.getParent(), config.getService()));
-            packageInfo.put(ConstVal.SERVICE_IMPL, joinPackage(config.getParent(), config.getServiceImpl()));
-            packageInfo.put(ConstVal.CONTROLLER, joinPackage(config.getParent(), config.getController()));
-
-            // 生成路径信息
-            pathInfo = new HashMap<>(6);
-            if (StringUtils.isNotEmpty(template.getEntity(getGlobalConfig().isKotlin()))) {
-                pathInfo.put(ConstVal.ENTITY_PATH, joinPath(outputDir, packageInfo.get(ConstVal.ENTITY)));
-            }
-            if (StringUtils.isNotEmpty(template.getMapper())) {
-                pathInfo.put(ConstVal.MAPPER_PATH, joinPath(outputDir, packageInfo.get(ConstVal.MAPPER)));
-            }
-            if (StringUtils.isNotEmpty(template.getXml())) {
-                pathInfo.put(ConstVal.XML_PATH, joinPath(outputDir, packageInfo.get(ConstVal.XML)));
-            }
-            if (StringUtils.isNotEmpty(template.getService())) {
-                pathInfo.put(ConstVal.SERVICE_PATH, joinPath(outputDir, packageInfo.get(ConstVal.SERVICE)));
-            }
-            if (StringUtils.isNotEmpty(template.getServiceImpl())) {
-                pathInfo.put(ConstVal.SERVICE_IMPL_PATH, joinPath(outputDir, packageInfo.get(ConstVal.SERVICE_IMPL)));
-            }
-            if (StringUtils.isNotEmpty(template.getController())) {
-                pathInfo.put(ConstVal.CONTROLLER_PATH, joinPath(outputDir, packageInfo.get(ConstVal.CONTROLLER)));
+        // 包信息
+        packageInfo = new HashMap<>(6);
+        packageInfo.put(ConstVal.ENTITY, joinPackage(config.getParent(), config.getEntity()));
+        packageInfo.put(ConstVal.MAPPER, joinPackage(config.getParent(), config.getMapper()));
+        packageInfo.put(ConstVal.XML, joinPackage(config.getParent(), config.getXml()));
+        packageInfo.put(ConstVal.SERVICE, joinPackage(config.getParent(), config.getService()));
+        packageInfo.put(ConstVal.SERVICE_IMPL, joinPackage(config.getParent(), config.getServiceImpl()));
+        packageInfo.put(ConstVal.CONTROLLER, joinPackage(config.getParent(), config.getController()));
+
+        // 自定义路径
+        Map<String, String> configPathInfo = config.getPathInfo();
+
+        // 生成路径信息
+        pathInfo = new HashMap<>(6);
+        setPathInfo(pathInfo, template.getEntity(getGlobalConfig().isKotlin()), configPathInfo, outputDir, ConstVal.ENTITY_PATH, ConstVal.ENTITY);
+        setPathInfo(pathInfo, template.getMapper(), configPathInfo, outputDir, ConstVal.MAPPER_PATH, ConstVal.MAPPER);
+        setPathInfo(pathInfo, template.getXml(), configPathInfo, outputDir, ConstVal.XML_PATH, ConstVal.XML);
+        setPathInfo(pathInfo, template.getService(), configPathInfo, outputDir, ConstVal.SERVICE_PATH, ConstVal.SERVICE);
+        setPathInfo(pathInfo, template.getServiceImpl(), configPathInfo, outputDir, ConstVal.SERVICE_IMPL_PATH, ConstVal.SERVICE_IMPL);
+        setPathInfo(pathInfo, template.getController(), configPathInfo, outputDir, ConstVal.CONTROLLER_PATH, ConstVal.CONTROLLER);
+    }
+
+    private void setPathInfo(Map<String, String> pathInfo, String template, Map<String, String> configPathInfo,
+                             String outputDir, String path, String module) {
+        if (StringUtils.isNotEmpty(template)) {
+            String outPath = outputDir;
+            if (null != configPathInfo && null != configPathInfo.get(path)) {
+                outPath = configPathInfo.get(path);
             }
+            pathInfo.put(path, joinPath(outPath, packageInfo.get(module)));
         }
     }
 
-
     /**
      * <p>
      * 处理数据源配置