Преглед изворни кода

fix:修复在执行sql时选择数据库执行不返回结果

韩帛霖 пре 1 година
родитељ
комит
5ef4a30d58

+ 21 - 2
ruoyi-framework/src/main/java/com/ruoyi/framework/aspectj/SqlInterceptor.java

@@ -23,9 +23,11 @@ public class SqlInterceptor implements Interceptor {
         StatementHandler statementHandler = (StatementHandler) invocation.getTarget();
         String sql = statementHandler.getBoundSql().getSql();
         // 修改SQL语句
-        String modifiedSql = modifySql(sql);
+        String modifiedSql = BeforeSQL(sql);
         // 将修改后的SQL语句设置回StatementHandler
         ReflectUtils.setFieldValue(statementHandler.getBoundSql(), "sql", modifiedSql);
+        invocation.proceed();
+        ReflectUtils.setFieldValue(statementHandler.getBoundSql(), "sql", sql);
         return invocation.proceed();
     }
 
@@ -42,9 +44,26 @@ public class SqlInterceptor implements Interceptor {
             return "set schema " + SecurityUtils.getDatabaseName() + "; " + sql;
         if (SecurityUtils.getDatabaseType().equals("mysql"))
             return "USE `" + SecurityUtils.getDatabaseName() + "`; " + sql;
-        return "";
+        return sql;
     }
 
+    // 根据类型设置不同的选择数据源格式
+    private String BeforeSQL(String sql) {
+        try {
+            SecurityUtils.getDatabaseType();
+        } catch (Exception e) {
+            return "";
+        }
+        if (SecurityUtils.getDatabaseType().equals("sqlserver"))
+            return "USE `" + SecurityUtils.getDatabaseName() + "`; ";
+        if (SecurityUtils.getDatabaseType().equals("dm"))
+            return "set schema " + SecurityUtils.getDatabaseName() + "; ";
+        if (SecurityUtils.getDatabaseType().equals("mysql"))
+            return "USE `" + SecurityUtils.getDatabaseName() + "`; ";
+        return sql;
+    }
+
+
     @Override
     public Object plugin(Object target) {
         return Plugin.wrap(target, this);