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 c6b482d5..2b1de937 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
@@ -32,6 +32,7 @@ public class GlobalConfig {
private final JavadocConfig javadocConfig;
private final PackageConfig packageConfig;
private final StrategyConfig strategyConfig;
+ private final TemplateConfig templateConfig;
// === 可选配置 ===
@@ -61,6 +62,7 @@ public class GlobalConfig {
this.javadocConfig = new JavadocConfig();
this.packageConfig = new PackageConfig();
this.strategyConfig = new StrategyConfig();
+ this.templateConfig = new TemplateConfig();
}
public JavadocConfig getJavadocConfig() {
@@ -75,6 +77,10 @@ public class GlobalConfig {
return strategyConfig;
}
+ public TemplateConfig getTemplateConfig() {
+ return templateConfig;
+ }
+
public EntityConfig getEntityConfig() {
if (entityConfig == null) {
entityConfig = new EntityConfig();
diff --git a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/StrategyConfig.java b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/StrategyConfig.java
index 0b1c53ed..d538e0be 100644
--- a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/StrategyConfig.java
+++ b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/StrategyConfig.java
@@ -1,7 +1,7 @@
package com.mybatisflex.codegen.config;
-import com.mybatisflex.codegen.template.EnjoyTemplate;
import com.mybatisflex.codegen.template.ITemplate;
+import com.mybatisflex.codegen.template.impl.EnjoyTemplate;
import java.util.HashMap;
import java.util.HashSet;
diff --git a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/TemplateConfig.java b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/TemplateConfig.java
new file mode 100644
index 00000000..b0e0af8f
--- /dev/null
+++ b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/TemplateConfig.java
@@ -0,0 +1,157 @@
+/**
+ * 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.config;
+
+import com.mybatisflex.codegen.constant.GenTypeConst;
+import com.mybatisflex.codegen.generator.GeneratorFactory;
+import com.mybatisflex.codegen.template.ITemplate;
+import com.mybatisflex.codegen.template.impl.EnjoyTemplate;
+
+/**
+ * 模板配置。
+ *
+ * @author 王帅
+ * @since 2023-05-17
+ */
+@SuppressWarnings("unused")
+public class TemplateConfig {
+
+ /**
+ * 生成代码的模板引擎。
+ */
+ private ITemplate template = new EnjoyTemplate();
+
+ /**
+ * 获取模板引擎。
+ */
+ public ITemplate getTemplate() {
+ return template;
+ }
+
+ /**
+ * 设置模板引擎。
+ */
+ public TemplateConfig setTemplate(ITemplate template) {
+ this.template = template;
+ return this;
+ }
+
+ /**
+ * 获取生成 Entity 模板文件的位置。
+ */
+ public String getEntity() {
+ return GeneratorFactory.getGenerator(GenTypeConst.ENTITY).getTemplatePath();
+ }
+
+ /**
+ * 设置生成 Entity 模板文件的位置。
+ */
+ public TemplateConfig setEntity(String entity) {
+ GeneratorFactory.getGenerator(GenTypeConst.ENTITY).setTemplatePath(entity);
+ return this;
+ }
+
+ /**
+ * 获取生成 Mapper 模板文件的位置。
+ */
+ public String getMapper() {
+ return GeneratorFactory.getGenerator(GenTypeConst.MAPPER).getTemplatePath();
+ }
+
+ /**
+ * 设置生成 Mapper 模板文件的位置。
+ */
+ public TemplateConfig setMapper(String mapper) {
+ GeneratorFactory.getGenerator(GenTypeConst.MAPPER).setTemplatePath(mapper);
+ return this;
+ }
+
+ /**
+ * 获取生成 Service 模板文件的位置。
+ */
+ public String getService() {
+ return GeneratorFactory.getGenerator(GenTypeConst.SERVICE).getTemplatePath();
+ }
+
+ /**
+ * 设置生成 Service 模板文件的位置。
+ */
+ public TemplateConfig setService(String service) {
+ GeneratorFactory.getGenerator(GenTypeConst.SERVICE).setTemplatePath(service);
+ return this;
+ }
+
+ /**
+ * 获取生成 ServiceImpl 模板文件的位置。
+ */
+ public String getServiceImpl() {
+ return GeneratorFactory.getGenerator(GenTypeConst.SERVICE_IMPL).getTemplatePath();
+ }
+
+ /**
+ * 设置生成 ServiceImpl 模板文件的位置。
+ */
+ public TemplateConfig setServiceImpl(String serviceImpl) {
+ GeneratorFactory.getGenerator(GenTypeConst.SERVICE_IMPL).setTemplatePath(serviceImpl);
+ return this;
+ }
+
+ /**
+ * 获取生成 Controller 模板文件的位置。
+ */
+ public String getController() {
+ return GeneratorFactory.getGenerator(GenTypeConst.CONTROLLER).getTemplatePath();
+ }
+
+ /**
+ * 设置生成 Controller 模板文件的位置。
+ */
+ public TemplateConfig setController(String controller) {
+ GeneratorFactory.getGenerator(GenTypeConst.CONTROLLER).setTemplatePath(controller);
+ return this;
+ }
+
+ /**
+ * 获取生成 TableDef 模板文件的位置。
+ */
+ public String getTableDef() {
+ return GeneratorFactory.getGenerator(GenTypeConst.TABLE_DEF).getTemplatePath();
+ }
+
+ /**
+ * 设置生成 TableDef 模板文件的位置。
+ */
+ public TemplateConfig setTableDef(String tableDef) {
+ GeneratorFactory.getGenerator(GenTypeConst.TABLE_DEF).setTemplatePath(tableDef);
+ return this;
+ }
+
+ /**
+ * 获取生成 MapperXml 模板文件的位置。
+ */
+ public String getMapperXml() {
+ return GeneratorFactory.getGenerator(GenTypeConst.MAPPER_XML).getTemplatePath();
+ }
+
+ /**
+ * 设置生成 MapperXml 模板文件的位置。
+ */
+ public TemplateConfig setMapperXml(String mapperXml) {
+ GeneratorFactory.getGenerator(GenTypeConst.MAPPER_XML).setTemplatePath(mapperXml);
+ return this;
+ }
+
+}
\ No newline at end of file
diff --git a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/constant/GenTypeConst.java b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/constant/GenTypeConst.java
new file mode 100644
index 00000000..915b1550
--- /dev/null
+++ b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/constant/GenTypeConst.java
@@ -0,0 +1,36 @@
+/**
+ * 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.constant;
+
+/**
+ * 生成类型常量。
+ *
+ * @author 王帅
+ * @since 2023-05-17
+ */
+public class GenTypeConst {
+
+ public static final String ENTITY = "entity";
+ public static final String MAPPER = "mapper";
+ public static final String SERVICE = "service";
+ public static final String SERVICE_IMPL = "serviceImpl";
+ public static final String CONTROLLER = "controller";
+ public static final String TABLE_DEF = "tableDef";
+ public static final String MAPPER_XML = "mapperXml";
+ private GenTypeConst() {
+ }
+
+}
\ No newline at end of file
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 e44fcd6e..ce5b4e85 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
@@ -15,6 +15,7 @@
*/
package com.mybatisflex.codegen.generator;
+import com.mybatisflex.codegen.constant.GenTypeConst;
import com.mybatisflex.codegen.generator.impl.*;
import java.util.Collection;
@@ -26,13 +27,17 @@ public class GeneratorFactory {
private static final Map generators = new HashMap<>();
static {
- registerGenerator("entity", new EntityGenerator());
- registerGenerator("mapper", new MapperGenerator());
- registerGenerator("service", new ServiceGenerator());
- registerGenerator("serviceImpl", new ServiceImplGenerator());
- registerGenerator("controller", new ControllerGenerator());
- registerGenerator("tableDef", new TableDefGenerator());
- registerGenerator("mapperXml", new MapperXmlGenerator());
+ registerGenerator(GenTypeConst.ENTITY, new EntityGenerator());
+ registerGenerator(GenTypeConst.MAPPER, new MapperGenerator());
+ registerGenerator(GenTypeConst.SERVICE, new ServiceGenerator());
+ registerGenerator(GenTypeConst.SERVICE_IMPL, new ServiceImplGenerator());
+ registerGenerator(GenTypeConst.CONTROLLER, new ControllerGenerator());
+ registerGenerator(GenTypeConst.TABLE_DEF, new TableDefGenerator());
+ registerGenerator(GenTypeConst.MAPPER_XML, new MapperXmlGenerator());
+ }
+
+ public static IGenerator getGenerator(String genType) {
+ return generators.get(genType);
}
diff --git a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/generator/IGenerator.java b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/generator/IGenerator.java
index 5f9e57fe..4d653e85 100644
--- a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/generator/IGenerator.java
+++ b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/generator/IGenerator.java
@@ -18,6 +18,34 @@ package com.mybatisflex.codegen.generator;
import com.mybatisflex.codegen.config.GlobalConfig;
import com.mybatisflex.codegen.entity.Table;
+/**
+ * 文件生成器接口。
+ *
+ * @author Michael Yang
+ * @author 王帅
+ */
public interface IGenerator {
+
+ /**
+ * 获取模板文件位置。
+ *
+ * @return 路径
+ */
+ String getTemplatePath();
+
+ /**
+ * 设置模板文件位置。
+ *
+ * @param templatePath
+ */
+ void setTemplatePath(String templatePath);
+
+ /**
+ * 根据模板生成文件。
+ *
+ * @param table 表内容
+ * @param globalConfig 全局配置
+ */
void generate(Table table, GlobalConfig globalConfig);
+
}
diff --git a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/generator/impl/ControllerGenerator.java b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/generator/impl/ControllerGenerator.java
index cc03dc06..885cc130 100644
--- a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/generator/impl/ControllerGenerator.java
+++ b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/generator/impl/ControllerGenerator.java
@@ -34,7 +34,7 @@ import java.util.Map;
*/
public class ControllerGenerator implements IGenerator {
- private final String templatePath;
+ private String templatePath;
public ControllerGenerator() {
this(TemplateConst.CONTROLLER);
@@ -70,6 +70,15 @@ public class ControllerGenerator implements IGenerator {
params.put("javadocConfig", globalConfig.getJavadocConfig());
params.put("controllerConfig", globalConfig.getControllerConfig());
- strategyConfig.getTemplateEngine().generate(params, templatePath, controllerJavaFile);
+ globalConfig.getTemplateConfig().getTemplate().generate(params, templatePath, controllerJavaFile);
}
+
+ public String getTemplatePath() {
+ return templatePath;
+ }
+
+ public void setTemplatePath(String templatePath) {
+ this.templatePath = templatePath;
+ }
+
}
diff --git a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/generator/impl/EntityGenerator.java b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/generator/impl/EntityGenerator.java
index 5e0c56a6..e2a11003 100644
--- a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/generator/impl/EntityGenerator.java
+++ b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/generator/impl/EntityGenerator.java
@@ -34,7 +34,7 @@ import java.util.Map;
*/
public class EntityGenerator implements IGenerator {
- private final String templatePath;
+ private String templatePath;
public EntityGenerator() {
this(TemplateConst.ENTITY);
@@ -70,6 +70,15 @@ public class EntityGenerator implements IGenerator {
params.put("entityConfig", globalConfig.getEntityConfig());
params.put("javadocConfig", globalConfig.getJavadocConfig());
- strategyConfig.getTemplateEngine().generate(params, templatePath, entityJavaFile);
+ globalConfig.getTemplateConfig().getTemplate().generate(params, templatePath, entityJavaFile);
}
+
+ public String getTemplatePath() {
+ return templatePath;
+ }
+
+ public void setTemplatePath(String templatePath) {
+ this.templatePath = templatePath;
+ }
+
}
diff --git a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/generator/impl/MapperGenerator.java b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/generator/impl/MapperGenerator.java
index 2511e3bc..6720a538 100644
--- a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/generator/impl/MapperGenerator.java
+++ b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/generator/impl/MapperGenerator.java
@@ -34,7 +34,7 @@ import java.util.Map;
*/
public class MapperGenerator implements IGenerator {
- private final String templatePath;
+ private String templatePath;
public MapperGenerator() {
this(TemplateConst.MAPPER);
@@ -70,6 +70,15 @@ public class MapperGenerator implements IGenerator {
params.put("mapperConfig", globalConfig.getMapperConfig());
params.put("javadocConfig", globalConfig.getJavadocConfig());
- strategyConfig.getTemplateEngine().generate(params, templatePath, mapperJavaFile);
+ globalConfig.getTemplateConfig().getTemplate().generate(params, templatePath, mapperJavaFile);
}
+
+ public String getTemplatePath() {
+ return templatePath;
+ }
+
+ public void setTemplatePath(String templatePath) {
+ this.templatePath = templatePath;
+ }
+
}
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
index 2713c721..d309485d 100644
--- 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
@@ -34,7 +34,7 @@ import java.util.Map;
*/
public class MapperXmlGenerator implements IGenerator {
- private final String templatePath;
+ private String templatePath;
public MapperXmlGenerator() {
this(TemplateConst.MAPPER_XML);
@@ -67,6 +67,15 @@ public class MapperXmlGenerator implements IGenerator {
params.put("table", table);
params.put("packageConfig", packageConfig);
- strategyConfig.getTemplateEngine().generate(params, templatePath, mapperXmlFile);
+ globalConfig.getTemplateConfig().getTemplate().generate(params, templatePath, mapperXmlFile);
}
+
+ public String getTemplatePath() {
+ return templatePath;
+ }
+
+ public void setTemplatePath(String templatePath) {
+ this.templatePath = templatePath;
+ }
+
}
diff --git a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/generator/impl/ServiceGenerator.java b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/generator/impl/ServiceGenerator.java
index 1a479112..d2242d00 100644
--- a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/generator/impl/ServiceGenerator.java
+++ b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/generator/impl/ServiceGenerator.java
@@ -34,7 +34,7 @@ import java.util.Map;
*/
public class ServiceGenerator implements IGenerator {
- private final String templatePath;
+ private String templatePath;
public ServiceGenerator() {
this(TemplateConst.SERVICE);
@@ -70,6 +70,15 @@ public class ServiceGenerator implements IGenerator {
params.put("serviceConfig", globalConfig.getServiceConfig());
params.put("javadocConfig", globalConfig.getJavadocConfig());
- strategyConfig.getTemplateEngine().generate(params, templatePath, serviceJavaFile);
+ globalConfig.getTemplateConfig().getTemplate().generate(params, templatePath, serviceJavaFile);
}
+
+ public String getTemplatePath() {
+ return templatePath;
+ }
+
+ public void setTemplatePath(String templatePath) {
+ this.templatePath = templatePath;
+ }
+
}
diff --git a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/generator/impl/ServiceImplGenerator.java b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/generator/impl/ServiceImplGenerator.java
index 13f921b4..ebe9b2e5 100644
--- a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/generator/impl/ServiceImplGenerator.java
+++ b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/generator/impl/ServiceImplGenerator.java
@@ -34,7 +34,7 @@ import java.util.Map;
*/
public class ServiceImplGenerator implements IGenerator {
- private final String templatePath;
+ private String templatePath;
public ServiceImplGenerator() {
this(TemplateConst.SERVICE_IMPL);
@@ -70,6 +70,15 @@ public class ServiceImplGenerator implements IGenerator {
params.put("javadocConfig", globalConfig.getJavadocConfig());
params.put("serviceImplConfig", globalConfig.getServiceImplConfig());
- strategyConfig.getTemplateEngine().generate(params, templatePath, serviceImplJavaFile);
+ globalConfig.getTemplateConfig().getTemplate().generate(params, templatePath, serviceImplJavaFile);
}
+
+ public String getTemplatePath() {
+ return templatePath;
+ }
+
+ public void setTemplatePath(String templatePath) {
+ this.templatePath = templatePath;
+ }
+
}
diff --git a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/generator/impl/TableDefGenerator.java b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/generator/impl/TableDefGenerator.java
index ebb8dc70..f35e1402 100644
--- a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/generator/impl/TableDefGenerator.java
+++ b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/generator/impl/TableDefGenerator.java
@@ -34,7 +34,7 @@ import java.util.Map;
*/
public class TableDefGenerator implements IGenerator {
- private final String templatePath;
+ private String templatePath;
public TableDefGenerator() {
this(TemplateConst.TABLE_DEF);
@@ -70,6 +70,15 @@ public class TableDefGenerator implements IGenerator {
params.put("javadocConfig", globalConfig.getJavadocConfig());
params.put("tableDefConfig", globalConfig.getTableDefConfig());
- strategyConfig.getTemplateEngine().generate(params, templatePath, tableDefJavaFile);
+ globalConfig.getTemplateConfig().getTemplate().generate(params, templatePath, tableDefJavaFile);
}
+
+ public String getTemplatePath() {
+ return templatePath;
+ }
+
+ public void setTemplatePath(String templatePath) {
+ this.templatePath = templatePath;
+ }
+
}
diff --git a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/template/EnjoyTemplate.java b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/template/impl/EnjoyTemplate.java
similarity index 67%
rename from mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/template/EnjoyTemplate.java
rename to mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/template/impl/EnjoyTemplate.java
index d1e92323..3ea8fbe4 100644
--- a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/template/EnjoyTemplate.java
+++ b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/template/impl/EnjoyTemplate.java
@@ -13,10 +13,15 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-package com.mybatisflex.codegen.template;
+package com.mybatisflex.codegen.template.impl;
import com.jfinal.template.Engine;
import com.jfinal.template.expr.ast.FieldGetters;
+import com.jfinal.template.source.ClassPathSource;
+import com.jfinal.template.source.FileSource;
+import com.jfinal.template.source.ISource;
+import com.jfinal.template.source.ISourceFactory;
+import com.mybatisflex.codegen.template.ITemplate;
import com.mybatisflex.core.util.StringUtil;
import java.io.File;
@@ -29,8 +34,8 @@ public class EnjoyTemplate implements ITemplate {
public EnjoyTemplate() {
engine = Engine.create("mybatis-flex", engine -> {
- engine.setToClassPathSourceFactory();
engine.addSharedMethod(StringUtil.class);
+ engine.setSourceFactory(new FileAndClassPathSourceFactory());
});
// 以下配置将支持 user.girl 表达式去调用 user 对象的 boolean isGirl() 方法
Engine.addFieldGetterToFirst(new FieldGetters.IsMethodFieldGetter());
@@ -48,4 +53,18 @@ public class EnjoyTemplate implements ITemplate {
e.printStackTrace();
}
}
+
+ public static class FileAndClassPathSourceFactory implements ISourceFactory {
+
+ @Override
+ public ISource getSource(String baseTemplatePath, String fileName, String encoding) {
+ // 先从文件寻找资源,找不到再从类路径寻找资源
+ if (new File(fileName).exists()) {
+ return new FileSource(baseTemplatePath, fileName, encoding);
+ }
+ return new ClassPathSource(baseTemplatePath, fileName, encoding);
+ }
+
+ }
+
}