refactor 重构代码生成工具链,复用代码模板以提升代码生成性能

This commit is contained in:
秋辞未寒
2026-03-24 01:56:34 +08:00
parent d547f68adc
commit fe8d15c6a8
14 changed files with 308 additions and 214 deletions

View File

@@ -22,7 +22,7 @@
<springdoc.version>3.0.2</springdoc.version>
<therapi-javadoc.version>0.15.0</therapi-javadoc.version>
<fesod.version>2.0.1-incubating</fesod.version>
<velocity.version>2.3</velocity.version>
<velocity.version>2.4.1</velocity.version>
<satoken.version>1.45.0</satoken.version>
<mybatis-plus.version>3.5.16</mybatis-plus.version>
<mybatis-plus-join.version>1.5.6</mybatis-plus-join.version>

View File

@@ -1,93 +1,45 @@
package org.dromara.generator.config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import cn.hutool.extra.template.TemplateConfig;
import org.dromara.common.core.factory.YmlPropertySourceFactory;
import org.dromara.generator.config.properties.GenProperties;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
/**
* 读取代码生成相关配置
*
* @author ruoyi
*/
@Component
@ConfigurationProperties(prefix = "gen")
@PropertySource(value = {"classpath:generator.yml"}, encoding = "UTF-8")
@AutoConfiguration
@EnableConfigurationProperties(GenProperties.class)
@PropertySource(value = "classpath:generator.yml", factory = YmlPropertySourceFactory.class)
public class GenConfig {
/**
* 作者
*/
public static String author;
private static GenProperties genProperties;
/**
* 生成包路径
*/
public static String packageName;
/**
* 自动去除表前缀默认是false
*/
public static boolean autoRemovePre;
/**
* 表前缀(类名不会包含表前缀)
*/
public static String tablePrefix;
public static String getAuthor() {
return author;
public GenConfig(GenProperties genProperties) {
GenConfig.genProperties = genProperties;
}
/**
* 注入代码生成作者配置。
*
* @param author 作者名称
*/
@Value("${author}")
public void setAuthor(String author) {
GenConfig.author = author;
public static String getAuthor() {
return genProperties.getAuthor();
}
public static String getPackageName() {
return packageName;
}
/**
* 注入代码生成基础包名配置。
*
* @param packageName 基础包名
*/
@Value("${packageName}")
public void setPackageName(String packageName) {
GenConfig.packageName = packageName;
return genProperties.getPackageName();
}
public static boolean getAutoRemovePre() {
return autoRemovePre;
}
/**
* 注入是否自动移除表前缀配置。
*
* @param autoRemovePre 是否自动移除表前缀
*/
@Value("${autoRemovePre}")
public void setAutoRemovePre(boolean autoRemovePre) {
GenConfig.autoRemovePre = autoRemovePre;
return genProperties.isAutoRemovePre();
}
public static String getTablePrefix() {
return tablePrefix;
return genProperties.getTablePrefix();
}
/**
* 注入代码生成表前缀配置。
*
* @param tablePrefix 表前缀字符串
*/
@Value("${tablePrefix}")
public void setTablePrefix(String tablePrefix) {
GenConfig.tablePrefix = tablePrefix;
public static TemplateConfig getTemplateConfig() {
return genProperties.getTemplateConfig();
}
}

View File

@@ -0,0 +1,43 @@
package org.dromara.generator.config.properties;
import cn.hutool.extra.template.TemplateConfig;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import java.nio.charset.StandardCharsets;
/**
* 代码生成配置属性
*
* @author 秋辞未寒
*/
@Data
@ConfigurationProperties(prefix = "gen")
public class GenProperties {
/**
* 作者
*/
private String author;
/**
* 生成包路径
*/
private String packageName;
/**
* 自动去除表前缀默认是false
*/
private boolean autoRemovePre = false;
/**
* 表前缀(类名不会包含表前缀)
*/
private String tablePrefix;
/**
* 模板配置
*/
private TemplateConfig templateConfig = new TemplateConfig(StandardCharsets.UTF_8, null, TemplateConfig.ResourceMode.CLASSPATH);
}

View File

@@ -1,5 +1,9 @@
package org.dromara.generator.constant;
import cn.hutool.core.collection.CollUtil;
import java.util.Set;
/**
* 代码生成通用常量
*
@@ -187,4 +191,48 @@ public interface GenConstants {
* 必填标识,对应前端表单规则中的必填字段配置。
*/
String REQUIRE = "1";
// 后端源码模板
String JAVA_DOMAIN_TEMPLATE_PATH = "vm/java/domain.java.vm";
String JAVA_VO_TEMPLATE_PATH = "vm/java/vo.java.vm";
String JAVA_BO_TEMPLATE_PATH = "vm/java/bo.java.vm";
String JAVA_MAPPER_TEMPLATE_PATH = "vm/java/mapper.java.vm";
String JAVA_SERVICE_TEMPLATE_PATH = "vm/java/service.java.vm";
String JAVA_SERVICE_IMPL_TEMPLATE_PATH = "vm/java/serviceImpl.java.vm";
String JAVA_CONTROLLER_TEMPLATE_PATH = "vm/java/controller.java.vm";
// MyBatis MapperXML 模板
String XML_MAPPER_TEMPLATE_PATH = "vm/xml/mapper.xml.vm";
// 前端接口源码模板
String TS_API_TEMPLATE_PATH = "vm/ts/api.ts.vm";
String TS_TYPES_TEMPLATE_PATH = "vm/ts/types.ts.vm";
// 前端页面源码模板
String VUE_INDEX_TEMPLATE_PATH = "vm/vue/index.vue.vm";
String VUE_INDEX_TREE_TEMPLATE_PATH = "vm/vue/index-tree.vue.vm";
// 数据库SQL模板
String SQL_ORACLE_TEMPLATE_PATH = "vm/sql/oracle.sql.vm";
String SQL_POSTGRES_TEMPLATE_PATH = "vm/sql/postgres.sql.vm";
String SQL_SQLSERVER_TEMPLATE_PATH = "vm/sql/sqlserver.sql.vm";
String SQL_MYSQL_TEMPLATE_PATH = "vm/sql/mysql.sql.vm";
/**
* 所有模板路径集合
*/
Set<String> TEMPLATE_PATHS = CollUtil.newHashSet(
JAVA_DOMAIN_TEMPLATE_PATH
, JAVA_VO_TEMPLATE_PATH
, JAVA_BO_TEMPLATE_PATH
, JAVA_MAPPER_TEMPLATE_PATH
, JAVA_SERVICE_TEMPLATE_PATH
, JAVA_SERVICE_IMPL_TEMPLATE_PATH
, JAVA_CONTROLLER_TEMPLATE_PATH
, XML_MAPPER_TEMPLATE_PATH
, TS_API_TEMPLATE_PATH
, TS_TYPES_TEMPLATE_PATH
, VUE_INDEX_TEMPLATE_PATH
, VUE_INDEX_TREE_TEMPLATE_PATH
, SQL_ORACLE_TEMPLATE_PATH
, SQL_POSTGRES_TEMPLATE_PATH
, SQL_SQLSERVER_TEMPLATE_PATH
, SQL_MYSQL_TEMPLATE_PATH
);
}

View File

@@ -15,10 +15,7 @@ import lombok.extern.slf4j.Slf4j;
import org.anyline.metadata.Column;
import org.anyline.metadata.Table;
import org.anyline.proxy.ServiceProxy;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.dromara.common.core.constant.Constants;
import org.dromara.common.core.domain.PageResult;
import org.dromara.common.core.exception.ServiceException;
import org.dromara.common.core.utils.SpringUtils;
import org.dromara.common.core.utils.StreamUtils;
@@ -26,7 +23,6 @@ import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.core.utils.file.FileUtils;
import org.dromara.common.json.utils.JsonUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.PageResult;
import org.dromara.common.mybatis.utils.IdGeneratorUtil;
import org.dromara.generator.constant.GenConstants;
import org.dromara.generator.domain.GenTable;
@@ -34,15 +30,14 @@ import org.dromara.generator.domain.GenTableColumn;
import org.dromara.generator.mapper.GenTableColumnMapper;
import org.dromara.generator.mapper.GenTableMapper;
import org.dromara.generator.util.GenUtils;
import org.dromara.generator.util.VelocityInitializer;
import org.dromara.generator.util.VelocityUtils;
import org.dromara.generator.util.TemplateEngineUtils;
import org.dromara.generator.util.template.PathNamedTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.StringWriter;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.zip.ZipEntry;
@@ -346,18 +341,14 @@ public class GenTableServiceImpl implements IGenTableService {
table.setMenuIds(menuIds);
// 设置主键列信息
setPkColumn(table);
VelocityInitializer.initVelocity();
VelocityContext context = VelocityUtils.prepareContext(table);
Dict context = TemplateEngineUtils.buildContext(table);
// 获取模板列表
List<String> templates = VelocityUtils.getTemplateList(table.getTplCategory());
for (String template : templates) {
List<PathNamedTemplate> templates = TemplateEngineUtils.getTemplateList(table.getTplCategory());
for (PathNamedTemplate template : templates) {
// 渲染模板
StringWriter sw = new StringWriter();
Template tpl = Velocity.getTemplate(template, Constants.UTF8);
tpl.merge(context, sw);
dataMap.put(template, sw.toString());
String render = template.render(context);
dataMap.put(template.getPathName(), render);
}
return dataMap;
}
@@ -389,21 +380,18 @@ public class GenTableServiceImpl implements IGenTableService {
// 设置主键列信息
setPkColumn(table);
VelocityInitializer.initVelocity();
VelocityContext context = VelocityUtils.prepareContext(table);
Dict context = TemplateEngineUtils.buildContext(table);
// 获取模板列表
List<String> templates = VelocityUtils.getTemplateList(table.getTplCategory());
for (String template : templates) {
if (!StringUtils.containsAny(template, "sql.vm", "api.ts.vm", "types.ts.vm", "index.vue.vm", "index-tree.vue.vm")) {
List<PathNamedTemplate> templates = TemplateEngineUtils.getTemplateList(table.getTplCategory());
for (PathNamedTemplate template : templates) {
String pathName = template.getPathName();
// 渲染模板
if (!StringUtils.containsAny(pathName, "sql.vm", "api.ts.vm", "types.ts.vm", "index.vue.vm", "index-tree.vue.vm")) {
// 渲染模板
StringWriter sw = new StringWriter();
Template tpl = Velocity.getTemplate(template, Constants.UTF8);
tpl.merge(context, sw);
try {
String path = getGenPath(table, template);
FileUtils.writeUtf8String(sw.toString(), path);
String render = template.render(context);
String path = getGenPath(table, pathName);
FileUtils.writeUtf8String(render, path);
} catch (Exception e) {
throw new ServiceException("渲染模板失败,表名:" + table.getTableName());
}
@@ -496,22 +484,17 @@ public class GenTableServiceImpl implements IGenTableService {
// 设置主键列信息
setPkColumn(table);
VelocityInitializer.initVelocity();
VelocityContext context = VelocityUtils.prepareContext(table);
Dict context = TemplateEngineUtils.buildContext(table);
// 获取模板列表
List<String> templates = VelocityUtils.getTemplateList(table.getTplCategory());
for (String template : templates) {
List<PathNamedTemplate> templates = TemplateEngineUtils.getTemplateList(table.getTplCategory());
for (PathNamedTemplate template : templates) {
String pathName = template.getPathName();
// 渲染模板
StringWriter sw = new StringWriter();
Template tpl = Velocity.getTemplate(template, Constants.UTF8);
tpl.merge(context, sw);
try {
String render = template.render(context);
// 添加到zip
zip.putNextEntry(new ZipEntry(VelocityUtils.getFileName(template, table)));
IoUtil.write(zip, StandardCharsets.UTF_8, false, sw.toString());
IoUtil.close(sw);
zip.putNextEntry(new ZipEntry(TemplateEngineUtils.getFileName(pathName, table)));
IoUtil.write(zip, StandardCharsets.UTF_8, false, render);
zip.flush();
zip.closeEntry();
} catch (IOException e) {
@@ -628,9 +611,9 @@ public class GenTableServiceImpl implements IGenTableService {
public static String getGenPath(GenTable table, String template) {
String genPath = table.getGenPath();
if (StringUtils.equals(genPath, "/")) {
return System.getProperty("user.dir") + File.separator + "src" + File.separator + VelocityUtils.getFileName(template, table);
return System.getProperty("user.dir") + File.separator + "src" + File.separator + TemplateEngineUtils.getFileName(template, table);
}
return genPath + File.separator + VelocityUtils.getFileName(template, table);
return genPath + File.separator + TemplateEngineUtils.getFileName(template, table);
}
}

View File

@@ -3,27 +3,33 @@ package org.dromara.generator.util;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.Dict;
import org.dromara.common.mybatis.enums.DataBaseType;
import org.dromara.generator.constant.GenConstants;
import cn.hutool.extra.template.TemplateEngine;
import cn.hutool.extra.template.TemplateUtil;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.core.utils.DateUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.json.utils.JsonUtils;
import org.dromara.common.mybatis.enums.DataBaseType;
import org.dromara.common.mybatis.helper.DataBaseHelper;
import org.dromara.generator.config.GenConfig;
import org.dromara.generator.constant.GenConstants;
import org.dromara.generator.domain.GenTable;
import org.dromara.generator.domain.GenTableColumn;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.apache.velocity.VelocityContext;
import org.dromara.generator.util.template.PathNamedTemplate;
import java.util.*;
import java.util.function.Consumer;
import static org.dromara.generator.constant.GenConstants.TS_TYPES_TEMPLATE_PATH;
/**
* 模板处理工具
*
* @author ruoyi
* 模板引擎工具
*/
@Slf4j
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class VelocityUtils {
public class TemplateEngineUtils {
/**
* 项目空间路径
@@ -40,56 +46,73 @@ public class VelocityUtils {
*/
private static final String DEFAULT_PARENT_MENU_ID = "3";
// 模板引擎
private static final TemplateEngine TEMPLATE_ENGINE;
private static final Map<String, PathNamedTemplate> TEMPLATE_MAPPER;
static {
// 模板引擎初始化
TEMPLATE_ENGINE = TemplateUtil.createEngine(GenConfig.getTemplateConfig());
TEMPLATE_MAPPER = PathNamedTemplate.form(TEMPLATE_ENGINE, GenConstants.TEMPLATE_PATHS);
}
/**
* 设置模板变量信息
* 构建模板上下文
*
* @param contextInit 模板上下文初始化函数
* @return 模板上下文
*/
public static Dict buildContext(Consumer<Dict> contextInit) {
Dict context = new Dict();
contextInit.accept(context);
return context;
}
/**
* 构建模板上下文
*
* @param genTable 代码生成业务表对象
* @return 初始化后的 Velocity 上下文
* @return 模板上下文
*/
public static VelocityContext prepareContext(GenTable genTable) {
public static Dict buildContext(GenTable genTable) {
// 构建上下文
Dict context = new Dict();
String moduleName = genTable.getModuleName();
String businessName = genTable.getBusinessName();
String packageName = genTable.getPackageName();
String tplCategory = genTable.getTplCategory();
String functionName = genTable.getFunctionName();
VelocityContext velocityContext = new VelocityContext();
velocityContext.put("tplCategory", genTable.getTplCategory());
velocityContext.put("tableName", genTable.getTableName());
velocityContext.put("functionName", StringUtils.isNotEmpty(functionName) ? functionName : "【请填写功能名称】");
velocityContext.put("ClassName", genTable.getClassName());
velocityContext.put("className", StringUtils.uncapitalize(genTable.getClassName()));
velocityContext.put("moduleName", genTable.getModuleName());
velocityContext.put("BusinessName", StringUtils.capitalize(genTable.getBusinessName()));
velocityContext.put("businessName", genTable.getBusinessName());
velocityContext.put("basePackage", getPackagePrefix(packageName));
velocityContext.put("packageName", packageName);
velocityContext.put("author", genTable.getFunctionAuthor());
velocityContext.put("datetime", DateUtils.getDate());
velocityContext.put("pkColumn", genTable.getPkColumn());
velocityContext.put("importList", getImportList(genTable));
velocityContext.put("permissionPrefix", getPermissionPrefix(moduleName, businessName));
velocityContext.put("columns", genTable.getColumns());
velocityContext.put("table", genTable);
velocityContext.put("dicts", getDicts(genTable));
setMenuVelocityContext(velocityContext, genTable);
if (GenConstants.TPL_TREE.equals(tplCategory)) {
setTreeVelocityContext(velocityContext, genTable);
}
return velocityContext;
}
/**
* 向模板上下文写入菜单相关变量
*
* @param context 模板上下文
* @param genTable 代码生成业务表对象
*/
public static void setMenuVelocityContext(VelocityContext context, GenTable genTable) {
context.put("tplCategory", genTable.getTplCategory());
context.put("tableName", genTable.getTableName());
context.put("functionName", StringUtils.isNotEmpty(functionName) ? functionName : "【请填写功能名称】");
context.put("ClassName", genTable.getClassName());
context.put("className", StringUtils.uncapitalize(genTable.getClassName()));
context.put("moduleName", moduleName);
context.put("BusinessName", StringUtils.capitalize(businessName));
context.put("businessName", businessName);
context.put("basePackage", getPackagePrefix(packageName));
context.put("packageName", packageName);
context.put("author", genTable.getFunctionAuthor());
context.put("datetime", DateUtils.getDate());
context.put("pkColumn", genTable.getPkColumn());
context.put("importList", getImportList(genTable));
context.put("permissionPrefix", getPermissionPrefix(moduleName, businessName));
context.put("columns", genTable.getColumns());
context.put("table", genTable);
context.put("dicts", getDicts(genTable));
// 向模板上下文写入菜单相关变量
String options = genTable.getOptions();
Dict paramsObj = JsonUtils.parseMap(options);
String parentMenuId = getParentMenuId(paramsObj);
context.put("parentMenuId", parentMenuId);
// 向树形模板上下文写入树字段相关变量
if (GenConstants.TPL_TREE.equals(tplCategory)) {
setTreeContext(context, genTable);
}
return context;
}
/**
@@ -98,10 +121,10 @@ public class VelocityUtils {
* @param context 模板上下文
* @param genTable 代码生成业务表对象
*/
public static void setTreeVelocityContext(VelocityContext context, GenTable genTable) {
public static void setTreeContext(Dict context, GenTable genTable) {
String options = genTable.getOptions();
Dict paramsObj = JsonUtils.parseMap(options);
String treeCode = getTreecode(paramsObj);
String treeCode = getTreeCode(paramsObj);
String treeParentCode = getTreeParentCode(paramsObj);
String treeName = getTreeName(paramsObj);
@@ -122,32 +145,38 @@ public class VelocityUtils {
*
* @return 模板列表
*/
public static List<String> getTemplateList(String tplCategory) {
List<String> templates = new ArrayList<>();
templates.add("vm/java/domain.java.vm");
templates.add("vm/java/vo.java.vm");
templates.add("vm/java/bo.java.vm");
templates.add("vm/java/mapper.java.vm");
templates.add("vm/java/service.java.vm");
templates.add("vm/java/serviceImpl.java.vm");
templates.add("vm/java/controller.java.vm");
templates.add("vm/xml/mapper.xml.vm");
public static List<PathNamedTemplate> getTemplateList(String tplCategory) {
List<PathNamedTemplate> templates = new ArrayList<>();
// 后端源码模板
templates.add(TEMPLATE_MAPPER.get(GenConstants.JAVA_DOMAIN_TEMPLATE_PATH));
templates.add(TEMPLATE_MAPPER.get(GenConstants.JAVA_VO_TEMPLATE_PATH));
templates.add(TEMPLATE_MAPPER.get(GenConstants.JAVA_BO_TEMPLATE_PATH));
templates.add(TEMPLATE_MAPPER.get(GenConstants.JAVA_MAPPER_TEMPLATE_PATH));
templates.add(TEMPLATE_MAPPER.get(GenConstants.JAVA_SERVICE_TEMPLATE_PATH));
templates.add(TEMPLATE_MAPPER.get(GenConstants.JAVA_SERVICE_IMPL_TEMPLATE_PATH));
templates.add(TEMPLATE_MAPPER.get(GenConstants.JAVA_CONTROLLER_TEMPLATE_PATH));
// MyBatis MapperXML 模板
templates.add(TEMPLATE_MAPPER.get(GenConstants.XML_MAPPER_TEMPLATE_PATH));
// 前端接口源码模板
templates.add(TEMPLATE_MAPPER.get(GenConstants.TS_API_TEMPLATE_PATH));
templates.add(TEMPLATE_MAPPER.get(TS_TYPES_TEMPLATE_PATH));
// 数据库模板
DataBaseType dataBaseType = DataBaseHelper.getDataBaseType();
if (dataBaseType.isOracle()) {
templates.add("vm/sql/oracle/sql.vm");
templates.add(TEMPLATE_MAPPER.get(GenConstants.SQL_ORACLE_TEMPLATE_PATH));
} else if (dataBaseType.isPostgreSql()) {
templates.add("vm/sql/postgres/sql.vm");
templates.add(TEMPLATE_MAPPER.get(GenConstants.SQL_POSTGRES_TEMPLATE_PATH));
} else if (dataBaseType.isSqlServer()) {
templates.add("vm/sql/sqlserver/sql.vm");
templates.add(TEMPLATE_MAPPER.get(GenConstants.SQL_SQLSERVER_TEMPLATE_PATH));
} else {
templates.add("vm/sql/sql.vm");
// 默认使用MySQL模板
templates.add(TEMPLATE_MAPPER.get(GenConstants.SQL_MYSQL_TEMPLATE_PATH));
}
templates.add("vm/ts/api.ts.vm");
templates.add("vm/ts/types.ts.vm");
// 前端页面源码模板
if (GenConstants.TPL_CRUD.equals(tplCategory)) {
templates.add("vm/vue/index.vue.vm");
templates.add(TEMPLATE_MAPPER.get(GenConstants.VUE_INDEX_TEMPLATE_PATH));
} else if (GenConstants.TPL_TREE.equals(tplCategory)) {
templates.add("vm/vue/index-tree.vue.vm");
templates.add(TEMPLATE_MAPPER.get(GenConstants.VUE_INDEX_TREE_TEMPLATE_PATH));
}
return templates;
}
@@ -174,7 +203,8 @@ public class VelocityUtils {
String javaPath = PROJECT_PATH + "/" + StringUtils.replace(packageName, ".", "/");
String mybatisPath = MYBATIS_PATH + "/" + moduleName;
String vuePath = "vue";
// templatePath
// genFilePathFormat
if (template.contains("domain.java.vm")) {
fileName = StringUtils.format("{}/domain/{}.java", javaPath, className);
}
@@ -306,7 +336,7 @@ public class VelocityUtils {
* @param paramsObj 生成其他选项
* @return 树编码
*/
public static String getTreecode(Map<String, Object> paramsObj) {
public static String getTreeCode(Map<String, Object> paramsObj) {
if (CollUtil.isNotEmpty(paramsObj) && paramsObj.containsKey(GenConstants.TREE_CODE)) {
return StringUtils.toCamelCase(Convert.toStr(paramsObj.get(GenConstants.TREE_CODE)));
}

View File

@@ -1,37 +0,0 @@
package org.dromara.generator.util;
import org.dromara.common.core.constant.Constants;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.apache.velocity.app.Velocity;
import java.util.Properties;
/**
* VelocityEngine工厂
*
* @author ruoyi
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class VelocityInitializer {
/**
* 初始化vm方法
*
* @throws RuntimeException 初始化 Velocity 引擎失败时抛出
*/
public static void initVelocity() {
Properties p = new Properties();
try {
// 加载classpath目录下的vm文件
p.setProperty("resource.loader.file.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
// 定义字符集
p.setProperty(Velocity.INPUT_ENCODING, Constants.UTF8);
// 初始化Velocity引擎指定配置Properties
Velocity.init(p);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}

View File

@@ -0,0 +1,68 @@
package org.dromara.generator.util.template;
import cn.hutool.extra.template.Template;
import cn.hutool.extra.template.TemplateEngine;
import lombok.Getter;
import java.io.File;
import java.io.OutputStream;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
/**
* 基于路径命名的模板委托实现
*
* @author 秋辞未寒
*/
public class PathNamedTemplate implements Template {
@Getter
private final String pathName;
private final Template delegate;
private PathNamedTemplate(String pathName, Template delegate) {
this.pathName = pathName;
this.delegate = delegate;
}
@Override
public void render(Map<?, ?> bindingMap, Writer writer) {
delegate.render(bindingMap, writer);
}
@Override
public void render(Map<?, ?> bindingMap, OutputStream out) {
delegate.render(bindingMap, out);
}
@Override
public void render(Map<?, ?> bindingMap, File file) {
delegate.render(bindingMap, file);
}
@Override
public String render(Map<?, ?> bindingMap) {
return delegate.render(bindingMap);
}
public static PathNamedTemplate form(String pathName, Template delegate) {
return new PathNamedTemplate(pathName, delegate);
}
public static PathNamedTemplate form(TemplateEngine templateEngine,String pathName) {
return new PathNamedTemplate(pathName,templateEngine.getTemplate(pathName));
}
public static Map<String, PathNamedTemplate> form(TemplateEngine templateEngine, Set<String> pathNames) {
Map<String, PathNamedTemplate> result = new HashMap<>();
for (String pathName : pathNames) {
PathNamedTemplate pathNamedTemplate = form(templateEngine, pathName);
result.put(pathName, pathNamedTemplate);
}
return result;
}
}

View File

@@ -8,3 +8,9 @@ gen:
autoRemovePre: false
# 表前缀(生成类名不会包含表前缀,多个用逗号分隔)
tablePrefix: sys_
# 模板配置
templateConfig:
# 模版加载模式
resourceMode: CLASSPATH
# 模板加载路径
# path: vm