From 14df8e0d9ac0421eb68173a92788c5d686838db6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=96=AF=E7=8B=82=E7=9A=84=E7=8B=AE=E5=AD=90Li?= <15040126243@163.com> Date: Fri, 10 Apr 2026 14:50:31 +0800 Subject: [PATCH] =?UTF-8?q?fix=20=E4=BF=AE=E5=A4=8D=20=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E7=94=9F=E6=88=90=20=E4=B8=BB=E5=BA=93=E7=94=9F=E6=88=90?= =?UTF-8?q?=E5=85=B6=E4=BB=96=E4=B8=8D=E5=90=8C=E6=95=B0=E6=8D=AE=E6=BA=90?= =?UTF-8?q?sql=E6=A8=A1=E6=9D=BF=E9=94=99=E8=AF=AF=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/mybatis/helper/DataBaseHelper.java | 18 ++++++++++++++++++ .../generator/service/GenTableServiceImpl.java | 6 +++--- .../dromara/generator/util/VelocityUtils.java | 4 ++-- 3 files changed, 23 insertions(+), 5 deletions(-) diff --git a/ruoyi-common/ruoyi-common-mybatis/src/main/java/org/dromara/common/mybatis/helper/DataBaseHelper.java b/ruoyi-common/ruoyi-common-mybatis/src/main/java/org/dromara/common/mybatis/helper/DataBaseHelper.java index 84a8a5c2a..4e410736d 100644 --- a/ruoyi-common/ruoyi-common-mybatis/src/main/java/org/dromara/common/mybatis/helper/DataBaseHelper.java +++ b/ruoyi-common/ruoyi-common-mybatis/src/main/java/org/dromara/common/mybatis/helper/DataBaseHelper.java @@ -46,6 +46,24 @@ public class DataBaseHelper { } } + /** + * 获取指定数据源对应的数据库类型 + * + * @param dsName 数据源名称 + * @return 指定数据库对应的 DataBaseType 枚举,找不到时默认返回 MY_SQL + * @throws ServiceException 当获取数据库连接或元数据出现异常时抛出业务异常 + */ + public static DataBaseType getDataBaseType(String dsName) { + DataSource dataSource = DS.getDataSource(dsName); + try (Connection conn = dataSource.getConnection()) { + DatabaseMetaData metaData = conn.getMetaData(); + String databaseProductName = metaData.getDatabaseProductName(); + return DataBaseType.find(databaseProductName); + } catch (SQLException e) { + throw new RuntimeException("获取数据库类型失败", e); + } + } + /** * 根据当前数据库类型,生成兼容的 FIND_IN_SET 语句片段 *

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 455f8d9e6..cb4737dbc 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 @@ -331,7 +331,7 @@ public class GenTableServiceImpl implements IGenTableService { VelocityContext context = VelocityUtils.prepareContext(table); // 获取模板列表 - List templates = VelocityUtils.getTemplateList(table.getTplCategory()); + List templates = VelocityUtils.getTemplateList(table.getTplCategory(), table.getDataName()); for (String template : templates) { // 渲染模板 StringWriter sw = new StringWriter(); @@ -374,7 +374,7 @@ public class GenTableServiceImpl implements IGenTableService { VelocityContext context = VelocityUtils.prepareContext(table); // 获取模板列表 - List templates = VelocityUtils.getTemplateList(table.getTplCategory()); + List templates = VelocityUtils.getTemplateList(table.getTplCategory(), table.getDataName()); for (String template : templates) { if (!StringUtils.containsAny(template, "sql.vm", "api.ts.vm", "types.ts.vm", "index.vue.vm", "index-tree.vue.vm")) { // 渲染模板 @@ -478,7 +478,7 @@ public class GenTableServiceImpl implements IGenTableService { VelocityContext context = VelocityUtils.prepareContext(table); // 获取模板列表 - List templates = VelocityUtils.getTemplateList(table.getTplCategory()); + List templates = VelocityUtils.getTemplateList(table.getTplCategory(), table.getDataName()); for (String template : templates) { // 渲染模板 StringWriter sw = new StringWriter(); 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/VelocityUtils.java index 44c5979ee..f6b4b44aa 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/VelocityUtils.java @@ -109,7 +109,7 @@ public class VelocityUtils { * * @return 模板列表 */ - public static List getTemplateList(String tplCategory) { + public static List getTemplateList(String tplCategory, String dsName) { List templates = new ArrayList<>(); templates.add("vm/java/domain.java.vm"); templates.add("vm/java/vo.java.vm"); @@ -119,7 +119,7 @@ public class VelocityUtils { templates.add("vm/java/serviceImpl.java.vm"); templates.add("vm/java/controller.java.vm"); templates.add("vm/xml/mapper.xml.vm"); - DataBaseType dataBaseType = DataBaseHelper.getDataBaseType(); + DataBaseType dataBaseType = DataBaseHelper.getDataBaseType(dsName); if (dataBaseType.isOracle()) { templates.add("vm/sql/oracle/sql.vm"); } else if (dataBaseType.isPostgreSql()) {