mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-07 00:58:24 +08:00
feat: 新增设置模板文件位置,使用指定的模板生成文件。
This commit is contained in:
parent
01f5a22a57
commit
328086cec1
@ -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();
|
||||
|
||||
@ -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;
|
||||
|
||||
@ -0,0 +1,157 @@
|
||||
/**
|
||||
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com).
|
||||
* <p>
|
||||
* 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
|
||||
* <p>
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* <p>
|
||||
* 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;
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,36 @@
|
||||
/**
|
||||
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com).
|
||||
* <p>
|
||||
* 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
|
||||
* <p>
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* <p>
|
||||
* 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() {
|
||||
}
|
||||
|
||||
}
|
||||
@ -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<String, IGenerator> 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);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@ -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);
|
||||
|
||||
}
|
||||
|
||||
@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user