From fe8d15c6a8f1d0f979ce4bef5f9e6b6df8d29b46 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A7=8B=E8=BE=9E=E6=9C=AA=E5=AF=92?= <545073804@qq.com> Date: Tue, 24 Mar 2026 01:56:34 +0800 Subject: [PATCH] =?UTF-8?q?refactor=20=E9=87=8D=E6=9E=84=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E7=94=9F=E6=88=90=E5=B7=A5=E5=85=B7=E9=93=BE=EF=BC=8C=E5=A4=8D?= =?UTF-8?q?=E7=94=A8=E4=BB=A3=E7=A0=81=E6=A8=A1=E6=9D=BF=E4=BB=A5=E6=8F=90?= =?UTF-8?q?=E5=8D=87=E4=BB=A3=E7=A0=81=E7=94=9F=E6=88=90=E6=80=A7=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 2 +- .../dromara/generator/config/GenConfig.java | 84 ++------- .../config/properties/GenProperties.java | 43 +++++ .../generator/constant/GenConstants.java | 48 +++++ .../service/GenTableServiceImpl.java | 69 +++----- ...ityUtils.java => TemplateEngineUtils.java} | 164 +++++++++++------- .../generator/util/VelocityInitializer.java | 37 ---- .../util/template/PathNamedTemplate.java | 68 ++++++++ ...ot.autoconfigure.AutoConfiguration.imports | 1 + .../src/main/resources/generator.yml | 6 + .../resources/vm/sql/{sql.vm => mysql.sql.vm} | 0 .../vm/sql/{oracle/sql.vm => oracle.sql.vm} | 0 .../sql/{postgres/sql.vm => postgres.sql.vm} | 0 .../{sqlserver/sql.vm => sqlserver.sql.vm} | 0 14 files changed, 308 insertions(+), 214 deletions(-) create mode 100644 ruoyi-modules/ruoyi-generator/src/main/java/org/dromara/generator/config/properties/GenProperties.java rename ruoyi-modules/ruoyi-generator/src/main/java/org/dromara/generator/util/{VelocityUtils.java => TemplateEngineUtils.java} (71%) delete mode 100644 ruoyi-modules/ruoyi-generator/src/main/java/org/dromara/generator/util/VelocityInitializer.java create mode 100644 ruoyi-modules/ruoyi-generator/src/main/java/org/dromara/generator/util/template/PathNamedTemplate.java create mode 100644 ruoyi-modules/ruoyi-generator/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports rename ruoyi-modules/ruoyi-generator/src/main/resources/vm/sql/{sql.vm => mysql.sql.vm} (100%) rename ruoyi-modules/ruoyi-generator/src/main/resources/vm/sql/{oracle/sql.vm => oracle.sql.vm} (100%) rename ruoyi-modules/ruoyi-generator/src/main/resources/vm/sql/{postgres/sql.vm => postgres.sql.vm} (100%) rename ruoyi-modules/ruoyi-generator/src/main/resources/vm/sql/{sqlserver/sql.vm => sqlserver.sql.vm} (100%) diff --git a/pom.xml b/pom.xml index 41ed143e2..d52f3b8d1 100644 --- a/pom.xml +++ b/pom.xml @@ -22,7 +22,7 @@ 3.0.2 0.15.0 2.0.1-incubating - 2.3 + 2.4.1 1.45.0 3.5.16 1.5.6 diff --git a/ruoyi-modules/ruoyi-generator/src/main/java/org/dromara/generator/config/GenConfig.java b/ruoyi-modules/ruoyi-generator/src/main/java/org/dromara/generator/config/GenConfig.java index f020772c7..764b8db38 100644 --- a/ruoyi-modules/ruoyi-generator/src/main/java/org/dromara/generator/config/GenConfig.java +++ b/ruoyi-modules/ruoyi-generator/src/main/java/org/dromara/generator/config/GenConfig.java @@ -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(); } } diff --git a/ruoyi-modules/ruoyi-generator/src/main/java/org/dromara/generator/config/properties/GenProperties.java b/ruoyi-modules/ruoyi-generator/src/main/java/org/dromara/generator/config/properties/GenProperties.java new file mode 100644 index 000000000..0e24ccfbc --- /dev/null +++ b/ruoyi-modules/ruoyi-generator/src/main/java/org/dromara/generator/config/properties/GenProperties.java @@ -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); + +} diff --git a/ruoyi-modules/ruoyi-generator/src/main/java/org/dromara/generator/constant/GenConstants.java b/ruoyi-modules/ruoyi-generator/src/main/java/org/dromara/generator/constant/GenConstants.java index ab396a6c5..d01156091 100644 --- a/ruoyi-modules/ruoyi-generator/src/main/java/org/dromara/generator/constant/GenConstants.java +++ b/ruoyi-modules/ruoyi-generator/src/main/java/org/dromara/generator/constant/GenConstants.java @@ -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 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 + ); } diff --git a/ruoyi-modules/ruoyi-generator/src/main/java/org/dromara/generator/service/GenTableServiceImpl.java b/ruoyi-modules/ruoyi-generator/src/main/java/org/dromara/generator/service/GenTableServiceImpl.java index cd94cd250..a1bcd29ea 100644 --- a/ruoyi-modules/ruoyi-generator/src/main/java/org/dromara/generator/service/GenTableServiceImpl.java +++ b/ruoyi-modules/ruoyi-generator/src/main/java/org/dromara/generator/service/GenTableServiceImpl.java @@ -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 templates = VelocityUtils.getTemplateList(table.getTplCategory()); - for (String template : templates) { + List 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 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 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 templates = VelocityUtils.getTemplateList(table.getTplCategory()); - for (String template : templates) { + List 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); } } diff --git a/ruoyi-modules/ruoyi-generator/src/main/java/org/dromara/generator/util/VelocityUtils.java b/ruoyi-modules/ruoyi-generator/src/main/java/org/dromara/generator/util/TemplateEngineUtils.java similarity index 71% rename from ruoyi-modules/ruoyi-generator/src/main/java/org/dromara/generator/util/VelocityUtils.java rename to ruoyi-modules/ruoyi-generator/src/main/java/org/dromara/generator/util/TemplateEngineUtils.java index d4f7e7304..113b1277a 100644 --- a/ruoyi-modules/ruoyi-generator/src/main/java/org/dromara/generator/util/VelocityUtils.java +++ b/ruoyi-modules/ruoyi-generator/src/main/java/org/dromara/generator/util/TemplateEngineUtils.java @@ -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 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 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 getTemplateList(String tplCategory) { - List 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 getTemplateList(String tplCategory) { + List 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 paramsObj) { + public static String getTreeCode(Map paramsObj) { if (CollUtil.isNotEmpty(paramsObj) && paramsObj.containsKey(GenConstants.TREE_CODE)) { return StringUtils.toCamelCase(Convert.toStr(paramsObj.get(GenConstants.TREE_CODE))); } diff --git a/ruoyi-modules/ruoyi-generator/src/main/java/org/dromara/generator/util/VelocityInitializer.java b/ruoyi-modules/ruoyi-generator/src/main/java/org/dromara/generator/util/VelocityInitializer.java deleted file mode 100644 index 842c7f79f..000000000 --- a/ruoyi-modules/ruoyi-generator/src/main/java/org/dromara/generator/util/VelocityInitializer.java +++ /dev/null @@ -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); - } - } - -} diff --git a/ruoyi-modules/ruoyi-generator/src/main/java/org/dromara/generator/util/template/PathNamedTemplate.java b/ruoyi-modules/ruoyi-generator/src/main/java/org/dromara/generator/util/template/PathNamedTemplate.java new file mode 100644 index 000000000..aa74fe997 --- /dev/null +++ b/ruoyi-modules/ruoyi-generator/src/main/java/org/dromara/generator/util/template/PathNamedTemplate.java @@ -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 form(TemplateEngine templateEngine, Set pathNames) { + Map result = new HashMap<>(); + for (String pathName : pathNames) { + PathNamedTemplate pathNamedTemplate = form(templateEngine, pathName); + result.put(pathName, pathNamedTemplate); + } + return result; + } + +} diff --git a/ruoyi-modules/ruoyi-generator/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports b/ruoyi-modules/ruoyi-generator/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports new file mode 100644 index 000000000..abd2aee9d --- /dev/null +++ b/ruoyi-modules/ruoyi-generator/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports @@ -0,0 +1 @@ +org.dromara.generator.config.GenConfig diff --git a/ruoyi-modules/ruoyi-generator/src/main/resources/generator.yml b/ruoyi-modules/ruoyi-generator/src/main/resources/generator.yml index d779d9794..4faf169a5 100644 --- a/ruoyi-modules/ruoyi-generator/src/main/resources/generator.yml +++ b/ruoyi-modules/ruoyi-generator/src/main/resources/generator.yml @@ -8,3 +8,9 @@ gen: autoRemovePre: false # 表前缀(生成类名不会包含表前缀,多个用逗号分隔) tablePrefix: sys_ + # 模板配置 + templateConfig: + # 模版加载模式 + resourceMode: CLASSPATH + # 模板加载路径 + # path: vm diff --git a/ruoyi-modules/ruoyi-generator/src/main/resources/vm/sql/sql.vm b/ruoyi-modules/ruoyi-generator/src/main/resources/vm/sql/mysql.sql.vm similarity index 100% rename from ruoyi-modules/ruoyi-generator/src/main/resources/vm/sql/sql.vm rename to ruoyi-modules/ruoyi-generator/src/main/resources/vm/sql/mysql.sql.vm diff --git a/ruoyi-modules/ruoyi-generator/src/main/resources/vm/sql/oracle/sql.vm b/ruoyi-modules/ruoyi-generator/src/main/resources/vm/sql/oracle.sql.vm similarity index 100% rename from ruoyi-modules/ruoyi-generator/src/main/resources/vm/sql/oracle/sql.vm rename to ruoyi-modules/ruoyi-generator/src/main/resources/vm/sql/oracle.sql.vm diff --git a/ruoyi-modules/ruoyi-generator/src/main/resources/vm/sql/postgres/sql.vm b/ruoyi-modules/ruoyi-generator/src/main/resources/vm/sql/postgres.sql.vm similarity index 100% rename from ruoyi-modules/ruoyi-generator/src/main/resources/vm/sql/postgres/sql.vm rename to ruoyi-modules/ruoyi-generator/src/main/resources/vm/sql/postgres.sql.vm diff --git a/ruoyi-modules/ruoyi-generator/src/main/resources/vm/sql/sqlserver/sql.vm b/ruoyi-modules/ruoyi-generator/src/main/resources/vm/sql/sqlserver.sql.vm similarity index 100% rename from ruoyi-modules/ruoyi-generator/src/main/resources/vm/sql/sqlserver/sql.vm rename to ruoyi-modules/ruoyi-generator/src/main/resources/vm/sql/sqlserver.sql.vm