diff --git a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/GlobalConfig.java b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/GlobalConfig.java index 107d1315..c6b482d5 100644 --- a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/GlobalConfig.java +++ b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/GlobalConfig.java @@ -41,6 +41,7 @@ public class GlobalConfig { private ServiceImplConfig serviceImplConfig; private ControllerConfig controllerConfig; private TableDefConfig tableDefConfig; + private MapperXmlConfig mapperXmlConfig; // === 其他配置 === @@ -54,6 +55,7 @@ public class GlobalConfig { private boolean serviceImplGenerateEnable; private boolean controllerGenerateEnable; private boolean tableDefGenerateEnable; + private boolean mapperXmlGenerateEnable; public GlobalConfig() { this.javadocConfig = new JavadocConfig(); @@ -115,6 +117,13 @@ public class GlobalConfig { return tableDefConfig; } + public MapperXmlConfig getMapperXmlConfig() { + if (mapperXmlConfig == null) { + mapperXmlConfig = new MapperXmlConfig(); + } + return mapperXmlConfig; + } + public EntityConfig enableEntity() { entityGenerateEnable = true; return getEntityConfig(); @@ -145,6 +154,11 @@ public class GlobalConfig { return getTableDefConfig(); } + public MapperXmlConfig enableMapperXml() { + mapperXmlGenerateEnable = true; + return mapperXmlConfig; + } + public void disableEntity() { entityGenerateEnable = false; } @@ -169,6 +183,10 @@ public class GlobalConfig { tableDefGenerateEnable = false; } + public void disableMapperXml() { + mapperXmlGenerateEnable = false; + } + public boolean isEntityGenerateEnable() { return entityGenerateEnable; } @@ -193,6 +211,10 @@ public class GlobalConfig { return tableDefGenerateEnable; } + public boolean isMapperXmlGenerateEnable() { + return mapperXmlGenerateEnable; + } + public void addCustomConfig(String key, Object value) { if (customConfig == null) { customConfig = new HashMap<>(); diff --git a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/MapperXmlConfig.java b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/MapperXmlConfig.java new file mode 100644 index 00000000..07a43d89 --- /dev/null +++ b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/MapperXmlConfig.java @@ -0,0 +1,39 @@ +package com.mybatisflex.codegen.config; + +/** + * 生成 MapperXml 的配置。 + * + * @author 王帅 + * @since 2023-05-17 + */ +public class MapperXmlConfig { + + /** + * MapperXml 文件的前缀。 + */ + private String filePrefix = ""; + + /** + * MapperXml 文件的后缀。 + */ + private String fileSuffix = "Mapper"; + + public String getFilePrefix() { + return filePrefix; + } + + public MapperXmlConfig setFilePrefix(String filePrefix) { + this.filePrefix = filePrefix; + return this; + } + + public String getFileSuffix() { + return fileSuffix; + } + + public MapperXmlConfig setFileSuffix(String fileSuffix) { + this.fileSuffix = fileSuffix; + return this; + } + +} \ No newline at end of file diff --git a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/PackageConfig.java b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/PackageConfig.java index 5b24be30..6ff36607 100644 --- a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/PackageConfig.java +++ b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/PackageConfig.java @@ -51,8 +51,13 @@ public class PackageConfig { */ private String tableDefPackage; + /** + * MapperXml 文件所在位置。 + */ + private String mapperXmlPath; + public String getSourceDir() { - if (sourceDir == null || StringUtil.isBlank(sourceDir)) { + if (StringUtil.isBlank(sourceDir)) { return System.getProperty("user.dir") + "/src/main/java"; } return sourceDir; @@ -144,4 +149,16 @@ public class PackageConfig { return this; } + public String getMapperXmlPath() { + if (StringUtil.isBlank(mapperXmlPath)) { + return getSourceDir().concat("/resources/mapper"); + } + return mapperXmlPath; + } + + public PackageConfig setMapperXmlPath(String mapperXmlPath) { + this.mapperXmlPath = mapperXmlPath; + return this; + } + } \ No newline at end of file diff --git a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/constant/TemplateConst.java b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/constant/TemplateConst.java index 9e77020a..bbd01f07 100644 --- a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/constant/TemplateConst.java +++ b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/constant/TemplateConst.java @@ -14,6 +14,8 @@ public final class TemplateConst { public static final String SERVICE_IMPL = "/templates/enjoy/serviceImpl.tpl"; public static final String CONTROLLER = "/templates/enjoy/controller.tpl"; public static final String TABLE_DEF = "/templates/enjoy/tableDef.tpl"; + public static final String MAPPER_XML = "/templates/enjoy/mapperXml.tpl"; + private TemplateConst() { } diff --git a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/entity/Table.java b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/entity/Table.java index 001b0005..0a229ca0 100644 --- a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/entity/Table.java +++ b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/entity/Table.java @@ -198,6 +198,19 @@ public class Table { + tableDefConfig.getClassSuffix(); } + /** + * 构建 MapperXml 的文件名称 + * + * @return fileName + */ + public String buildMapperXmlFileName() { + String tableDefJavaFileName = getEntityJavaFileName(); + MapperXmlConfig mapperXmlConfig = globalConfig.getMapperXmlConfig(); + return mapperXmlConfig.getFilePrefix() + + tableDefJavaFileName + + mapperXmlConfig.getFileSuffix(); + } + public String buildExtends() { EntityConfig entityConfig = globalConfig.getEntityConfig(); if (entityConfig.getSupperClass() != null) { diff --git a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/generator/GeneratorFactory.java b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/generator/GeneratorFactory.java index d82a2e3f..e44fcd6e 100644 --- a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/generator/GeneratorFactory.java +++ b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/generator/GeneratorFactory.java @@ -32,6 +32,7 @@ public class GeneratorFactory { registerGenerator("serviceImpl", new ServiceImplGenerator()); registerGenerator("controller", new ControllerGenerator()); registerGenerator("tableDef", new TableDefGenerator()); + registerGenerator("mapperXml", new MapperXmlGenerator()); } diff --git a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/generator/impl/MapperXmlGenerator.java b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/generator/impl/MapperXmlGenerator.java new file mode 100644 index 00000000..2713c721 --- /dev/null +++ b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/generator/impl/MapperXmlGenerator.java @@ -0,0 +1,72 @@ +/** + * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). + *
+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *
+ * http://www.apache.org/licenses/LICENSE-2.0 + *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.mybatisflex.codegen.generator.impl;
+
+import com.mybatisflex.codegen.config.GlobalConfig;
+import com.mybatisflex.codegen.config.PackageConfig;
+import com.mybatisflex.codegen.config.StrategyConfig;
+import com.mybatisflex.codegen.constant.TemplateConst;
+import com.mybatisflex.codegen.entity.Table;
+import com.mybatisflex.codegen.generator.IGenerator;
+
+import java.io.File;
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * MapperXml 生成器。
+ *
+ * @author 王帅
+ * @since 2023-05-17
+ */
+public class MapperXmlGenerator implements IGenerator {
+
+ private final String templatePath;
+
+ public MapperXmlGenerator() {
+ this(TemplateConst.MAPPER_XML);
+ }
+
+ public MapperXmlGenerator(String templatePath) {
+ this.templatePath = templatePath;
+ }
+
+ @Override
+ public void generate(Table table, GlobalConfig globalConfig) {
+
+ if (!globalConfig.isMapperXmlGenerateEnable()) {
+ return;
+ }
+
+ PackageConfig packageConfig = globalConfig.getPackageConfig();
+ StrategyConfig strategyConfig = globalConfig.getStrategyConfig();
+
+ File mapperXmlFile = new File(packageConfig.getMapperXmlPath() + "/" +
+ table.buildMapperXmlFileName() + ".xml");
+
+
+ if (mapperXmlFile.exists() && !strategyConfig.isOverwriteEnable()) {
+ return;
+ }
+
+
+ Map