ソースを参照

全局package配置属性支持自定义模板信息获取.

https://github.com/baomidou/mybatis-plus/issues/6580
nieqiurong 6 ヶ月 前
コミット
0aa8f76067

+ 41 - 0
mybatis-plus-generator/src/main/java/com/baomidou/mybatisplus/generator/config/PackageConfig.java

@@ -17,11 +17,13 @@ package com.baomidou.mybatisplus.generator.config;
 
 import com.baomidou.mybatisplus.core.toolkit.StringPool;
 import com.baomidou.mybatisplus.core.toolkit.StringUtils;
+import com.baomidou.mybatisplus.generator.config.builder.CustomFile;
 import lombok.Getter;
 import org.jetbrains.annotations.NotNull;
 
 import java.util.Collections;
 import java.util.HashMap;
+import java.util.List;
 import java.util.Map;
 
 /**
@@ -122,10 +124,24 @@ public class PackageConfig {
      * 获取包配置信息
      *
      * @return 包配置信息
+     * @see #getPackageInfo(InjectionConfig)
      * @since 3.5.0
+     * @deprecated 3.5.10
      */
     @NotNull
     public Map<String, String> getPackageInfo() {
+        return getPackageInfo((InjectionConfig) null);
+    }
+
+    /**
+     * 获取包配置信息
+     *
+     * @param injectionConfig 配置文件信息
+     * @return 包配置信息
+     * @since 3.5.10
+     */
+    @NotNull
+    public Map<String, String> getPackageInfo(InjectionConfig injectionConfig) {
         if (packageInfo.isEmpty()) {
             packageInfo.put(ConstVal.MODULE_NAME, this.getModuleName());
             packageInfo.put(ConstVal.ENTITY, this.joinPackage(this.getEntity()));
@@ -135,6 +151,20 @@ public class PackageConfig {
             packageInfo.put(ConstVal.SERVICE_IMPL, this.joinPackage(this.getServiceImpl()));
             packageInfo.put(ConstVal.CONTROLLER, this.joinPackage(this.getController()));
             packageInfo.put(ConstVal.PARENT, this.getParent());
+            if (injectionConfig != null) {
+                List<CustomFile> customFiles = injectionConfig.getCustomFiles();
+                for (CustomFile customFile : customFiles) {
+                    if (StringUtils.isNotBlank(customFile.getPackageName())) {
+                        String name = customFile.getShortName();
+                        if (StringUtils.isNotBlank(this.parent)
+                            && customFile.getPackageName().startsWith(this.parent)) {
+                            packageInfo.put(name, customFile.getPackageName());
+                        } else {
+                            packageInfo.put(name, this.joinPackage(customFile.getPackageName()));
+                        }
+                    }
+                }
+            }
         }
         return Collections.unmodifiableMap(this.packageInfo);
     }
@@ -146,10 +176,21 @@ public class PackageConfig {
      * @return 配置信息
      * @since 3.5.0
      */
+    @Deprecated
     public String getPackageInfo(String module) {
         return getPackageInfo().get(module);
     }
 
+    /**
+     * @since 3.5.10
+     * @param injectionConfig 注入配置
+     * @param module 模块
+     * @return 配置信息
+     */
+    public String getPackageInfo(InjectionConfig injectionConfig, String module) {
+        return getPackageInfo(injectionConfig).get(module);
+    }
+
     /**
      * 构建者
      *

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

@@ -115,7 +115,7 @@ public class ConfigBuilder {
         this.templateConfig = Optional.ofNullable(templateConfig).orElseGet(GeneratorBuilder::templateConfig);
         this.packageConfig = Optional.ofNullable(packageConfig).orElseGet(GeneratorBuilder::packageConfig);
         this.injectionConfig = Optional.ofNullable(injectionConfig).orElseGet(GeneratorBuilder::injectionConfig);
-        this.pathInfo.putAll(new PathInfoHandler(this.globalConfig, this.strategyConfig, this.packageConfig).getPathInfo());
+        this.pathInfo.putAll(new PathInfoHandler(this.injectionConfig, this.globalConfig, this.strategyConfig, this.packageConfig).getPathInfo());
         Class<? extends IDatabaseQuery> databaseQueryClass = dataSourceConfig.getDatabaseQueryClass();
         try {
             Constructor<? extends IDatabaseQuery> declaredConstructor = databaseQueryClass.getDeclaredConstructor(this.getClass());

+ 10 - 0
mybatis-plus-generator/src/main/java/com/baomidou/mybatisplus/generator/config/builder/CustomFile.java

@@ -60,6 +60,16 @@ public class CustomFile {
      */
     private boolean fileOverride;
 
+    /**
+     * 获取文件短名称(去除后缀)
+     *
+     * @return 文件名
+     * @since 3.5.10
+     */
+    public String getShortName() {
+        return fileName.substring(0, fileName.lastIndexOf("."));
+    }
+
     /**
      * 构建者
      */

+ 14 - 14
mybatis-plus-generator/src/main/java/com/baomidou/mybatisplus/generator/config/builder/PathInfoHandler.java

@@ -50,11 +50,11 @@ class PathInfoHandler {
      */
     private final PackageConfig packageConfig;
 
-    PathInfoHandler(GlobalConfig globalConfig, StrategyConfig strategyConfig, PackageConfig packageConfig) {
+    PathInfoHandler(InjectionConfig injectionConfig, GlobalConfig globalConfig, StrategyConfig strategyConfig, PackageConfig packageConfig) {
         this.outputDir = globalConfig.getOutputDir();
         this.packageConfig = packageConfig;
         // 设置默认输出路径
-        this.setDefaultPathInfo(globalConfig, strategyConfig);
+        this.setDefaultPathInfo(injectionConfig, globalConfig, strategyConfig);
         // 覆盖自定义路径
         Map<OutputFile, String> pathInfo = packageConfig.getPathInfo();
         if (CollectionUtils.isNotEmpty(pathInfo)) {
@@ -68,40 +68,40 @@ class PathInfoHandler {
      * @param globalConfig   全局配置
      * @param strategyConfig 模板配置
      */
-    private void setDefaultPathInfo(GlobalConfig globalConfig, StrategyConfig strategyConfig) {
+    private void setDefaultPathInfo(InjectionConfig injectionConfig, GlobalConfig globalConfig, StrategyConfig strategyConfig) {
         Entity entity = strategyConfig.entity();
         if (entity.isGenerate()) {
-            putPathInfo(globalConfig.isKotlin() ? entity.getKotlinTemplate() : entity.getJavaTemplate(), OutputFile.entity, ConstVal.ENTITY);
+            putPathInfo(injectionConfig, globalConfig.isKotlin() ? entity.getKotlinTemplate() : entity.getJavaTemplate(), OutputFile.entity, ConstVal.ENTITY);
         }
         Mapper mapper = strategyConfig.mapper();
         if (mapper.isGenerateMapper()) {
-            putPathInfo(mapper.getMapperTemplatePath(), OutputFile.mapper, ConstVal.MAPPER);
+            putPathInfo(injectionConfig, mapper.getMapperTemplatePath(), OutputFile.mapper, ConstVal.MAPPER);
         }
         if (mapper.isGenerateMapperXml()) {
-            putPathInfo(mapper.getMapperXmlTemplatePath(), OutputFile.xml, ConstVal.XML);
+            putPathInfo(injectionConfig, mapper.getMapperXmlTemplatePath(), OutputFile.xml, ConstVal.XML);
         }
         Service service = strategyConfig.service();
         if (service.isGenerateService()) {
-            putPathInfo(service.getServiceTemplate(), OutputFile.service, ConstVal.SERVICE);
+            putPathInfo(injectionConfig, service.getServiceTemplate(), OutputFile.service, ConstVal.SERVICE);
         }
         if (service.isGenerateServiceImpl()) {
-            putPathInfo(service.getServiceImplTemplate(), OutputFile.serviceImpl, ConstVal.SERVICE_IMPL);
+            putPathInfo(injectionConfig, service.getServiceImplTemplate(), OutputFile.serviceImpl, ConstVal.SERVICE_IMPL);
         }
         Controller controller = strategyConfig.controller();
         if (controller.isGenerate()) {
-            putPathInfo(controller.getTemplatePath(), OutputFile.controller, ConstVal.CONTROLLER);
+            putPathInfo(injectionConfig, controller.getTemplatePath(), OutputFile.controller, ConstVal.CONTROLLER);
         }
-        putPathInfo(OutputFile.parent, ConstVal.PARENT);
+        putPathInfo(injectionConfig, OutputFile.parent, ConstVal.PARENT);
     }
 
-    private void putPathInfo(String template, OutputFile outputFile, String module) {
+    private void putPathInfo(InjectionConfig injectionConfig, String template, OutputFile outputFile, String module) {
         if (StringUtils.isNotBlank(template)) {
-            putPathInfo(outputFile, module);
+            putPathInfo(injectionConfig, outputFile, module);
         }
     }
 
-    private void putPathInfo(OutputFile outputFile, String module) {
-        pathInfo.putIfAbsent(outputFile, joinPath(outputDir, packageConfig.getPackageInfo(module)));
+    private void putPathInfo(InjectionConfig injectionConfig, OutputFile outputFile, String module) {
+        pathInfo.putIfAbsent(outputFile, joinPath(outputDir, packageConfig.getPackageInfo(injectionConfig, module)));
     }
 
     /**

+ 1 - 1
mybatis-plus-generator/src/main/java/com/baomidou/mybatisplus/generator/engine/AbstractTemplateEngine.java

@@ -310,7 +310,7 @@ public abstract class AbstractTemplateEngine {
         Map<String, Object> entityData = strategyConfig.entity().renderData(tableInfo);
         objectMap.putAll(entityData);
         objectMap.put("config", config);
-        objectMap.put("package", config.getPackageConfig().getPackageInfo());
+        objectMap.put("package", config.getPackageConfig().getPackageInfo(config.getInjectionConfig()));
         GlobalConfig globalConfig = config.getGlobalConfig();
         objectMap.put("author", globalConfig.getAuthor());
         objectMap.put("kotlin", globalConfig.isKotlin());