Переглянути джерело

Fx 修复代码生成 库生成其他不同数据源sql模板错误问题

Yue 5 днів тому
батько
коміт
88f2a2665b

+ 18 - 0
SERVER/VberAdminPlusV3/vber-common/vber-common-mybatis/src/main/java/com/vber/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 语句片段
      * <p>

+ 1 - 1
SERVER/VberAdminPlusV3/vber-modules/vber-generator/src/main/java/com/vber/generator/service/GenTableServiceImpl.java

@@ -648,7 +648,7 @@ public class GenTableServiceImpl implements IGenTableService {
         // 设置主键列信息
         setPkColumn(table);
         // 获取模板列表
-        return VelocityUtils.getTemplateList(table.getTplCategory());
+        return VelocityUtils.getTemplateList(table.getTplCategory(), table.getDataName());
     }
 
     /**

+ 5 - 4
SERVER/VberAdminPlusV3/vber-modules/vber-generator/src/main/java/com/vber/generator/util/VelocityUtils.java

@@ -122,7 +122,7 @@ public class VelocityUtils {
      *
      * @return 模板列表
      */
-    public static List<String> getTemplateList(String tplCategory) {
+    public static List<String> getTemplateList(String tplCategory, String dsName) {
         List<String> templates = new ArrayList<>();
         templates.add("vm/java/domain.java.vm");
         templates.add("vm/java/vo.java.vm");
@@ -133,7 +133,7 @@ public class VelocityUtils {
         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()) {
@@ -158,7 +158,8 @@ public class VelocityUtils {
      * @param template 模板
      */
     public static boolean isUiFile(String template) {
-        return template.contains("api.ts.vm") || template.contains("view.vue.vm") || template.contains("view-tree.vue.vm");
+        return template.contains("api.ts.vm") || template.contains("view.vue.vm")
+                || template.contains("view-tree.vue.vm");
     }
 
     /**
@@ -265,7 +266,7 @@ public class VelocityUtils {
         for (GenTableColumn column : columns) {
             if (!column.isSuperColumn() && StringUtils.isNotEmpty(column.getDictType()) && StringUtils.equalsAny(
                     column.getHtmlType(),
-                    new String[]{GenConstants.HTML_SELECT, GenConstants.HTML_RADIO, GenConstants.HTML_CHECKBOX})) {
+                    new String[] { GenConstants.HTML_SELECT, GenConstants.HTML_RADIO, GenConstants.HTML_CHECKBOX })) {
                 dicts.add("'" + column.getDictType() + "'");
             }
         }