ソースを参照

fix:新增是否启用分页校验,修复租户用户切换数据源失效问题,数据条不完整不会返回前端异常信息

韩帛霖 1 年間 前
コミット
5a6077ef49

+ 0 - 18
ruoyi-common/src/main/java/com/ruoyi/common/config/datasource/config/RedisDataClear.java

@@ -1,18 +0,0 @@
-package com.ruoyi.common.config.datasource.config;
-
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.boot.CommandLineRunner;
-import org.springframework.data.redis.core.RedisTemplate;
-import org.springframework.stereotype.Component;
-
-@Component
-
-public class RedisDataClear implements CommandLineRunner {
-    @Autowired
-    private RedisTemplate redisTemplate;
-
-    @Override
-    public void run(String... args) throws Exception {
-        redisTemplate.getConnectionFactory().getConnection().flushDb();
-    }
-}

+ 12 - 1
ruoyi-common/src/main/java/com/ruoyi/common/core/page/PageDomain.java

@@ -4,7 +4,7 @@ import com.ruoyi.common.utils.StringUtils;
 
 /**
  * 分页数据
- * 
+ *
  * @author ruoyi
  */
 public class PageDomain
@@ -24,6 +24,9 @@ public class PageDomain
     /** 分页参数合理化 */
     private Boolean reasonable = true;
 
+    /** 是否进行分页操作 */
+    private Boolean isEnablePaging = true;
+
     public String getOrderBy()
     {
         if (StringUtils.isEmpty(orderByColumn))
@@ -98,4 +101,12 @@ public class PageDomain
     {
         this.reasonable = reasonable;
     }
+
+    public Boolean isEnablePaging() {
+        return isEnablePaging;
+    }
+
+    public void setIsEnablePaging(Boolean enablePaging) {
+        isEnablePaging = enablePaging;
+    }
 }

+ 11 - 7
ruoyi-common/src/main/java/com/ruoyi/common/core/page/TableSupport.java

@@ -1,15 +1,15 @@
 package com.ruoyi.common.core.page;
 
 import com.ruoyi.common.core.text.Convert;
+import com.ruoyi.common.utils.SecurityUtils;
 import com.ruoyi.common.utils.ServletUtils;
 
 /**
  * 表格数据处理
- * 
+ *
  * @author ruoyi
  */
-public class TableSupport
-{
+public class TableSupport {
     /**
      * 当前记录起始索引
      */
@@ -35,22 +35,26 @@ public class TableSupport
      */
     public static final String REASONABLE = "reasonable";
 
+    /**
+     * 是否进行分页操作
+     */
+    public static final String ISENABLEPAGING = "isEnablePaging";
+
     /**
      * 封装分页对象
      */
-    public static PageDomain getPageDomain()
-    {
+    public static PageDomain getPageDomain() {
         PageDomain pageDomain = new PageDomain();
         pageDomain.setPageNum(Convert.toInt(ServletUtils.getParameter(PAGE_NUM), 1));
         pageDomain.setPageSize(Convert.toInt(ServletUtils.getParameter(PAGE_SIZE), 10));
         pageDomain.setOrderByColumn(ServletUtils.getParameter(ORDER_BY_COLUMN));
         pageDomain.setIsAsc(ServletUtils.getParameter(IS_ASC));
         pageDomain.setReasonable(ServletUtils.getParameterToBool(REASONABLE));
+        pageDomain.setIsEnablePaging(ServletUtils.getParameterToBool(ISENABLEPAGING));
         return pageDomain;
     }
 
-    public static PageDomain buildPageRequest()
-    {
+    public static PageDomain buildPageRequest() {
         return getPageDomain();
     }
 }

+ 11 - 12
ruoyi-common/src/main/java/com/ruoyi/common/utils/PageUtils.java

@@ -7,29 +7,28 @@ import com.ruoyi.common.utils.sql.SqlUtil;
 
 /**
  * 分页工具类
- * 
+ *
  * @author ruoyi
  */
-public class PageUtils extends PageHelper
-{
+public class PageUtils extends PageHelper {
     /**
      * 设置请求分页数据
      */
-    public static void startPage()
-    {
+    public static void startPage() {
         PageDomain pageDomain = TableSupport.buildPageRequest();
-        Integer pageNum = pageDomain.getPageNum();
-        Integer pageSize = pageDomain.getPageSize();
-        String orderBy = SqlUtil.escapeOrderBySql(pageDomain.getOrderBy());
-        Boolean reasonable = pageDomain.getReasonable();
-        PageHelper.startPage(pageNum, pageSize, orderBy).setReasonable(reasonable);
+        if (pageDomain.isEnablePaging()) {
+            Integer pageNum = pageDomain.getPageNum();
+            Integer pageSize = pageDomain.getPageSize();
+            String orderBy = SqlUtil.escapeOrderBySql(pageDomain.getOrderBy());
+            Boolean reasonable = pageDomain.getReasonable();
+            PageHelper.startPage(pageNum, pageSize, orderBy).setReasonable(reasonable);
+        }
     }
 
     /**
      * 清理分页的线程变量
      */
-    public static void clearPage()
-    {
+    public static void clearPage() {
         PageHelper.clearPage();
     }
 }

+ 32 - 6
ruoyi-framework/src/main/java/com/ruoyi/framework/aspectj/SqlInterceptor.java

@@ -25,16 +25,25 @@ public class SqlInterceptor implements Interceptor {
     public Object intercept(Invocation invocation) throws Throwable, TenantDataSource {
         StatementHandler statementHandler = (StatementHandler) invocation.getTarget();
         String sql = statementHandler.getBoundSql().getSql();
-        // 修改SQL语句
-        String modifiedSql = BeforeSQL(sql);
+        // SQL执行前
+        // String modifiedSql = BeforeSQL(sql);
+        // 拼接sql
+        String modifiedSql = modifySql(sql);
+
+
         if (modifiedSql.equals("error")) {
             // 终止程序
             return AjaxResult.error("当前用户没有数据源信息!");
         } else {
+            // 将修改后的SQL语句设置回StatementHandler
+//            ReflectUtils.setFieldValue(statementHandler.getBoundSql(), "sql", modifiedSql);
+//            invocation.proceed();
+//            ReflectUtils.setFieldValue(statementHandler.getBoundSql(), "sql", sql);
+//            return invocation.proceed();
+
+
             // 将修改后的SQL语句设置回StatementHandler
             ReflectUtils.setFieldValue(statementHandler.getBoundSql(), "sql", modifiedSql);
-            invocation.proceed();
-            ReflectUtils.setFieldValue(statementHandler.getBoundSql(), "sql", sql);
             return invocation.proceed();
         }
     }
@@ -50,8 +59,25 @@ public class SqlInterceptor implements Interceptor {
             return "USE `" + SecurityUtils.getDatabaseName() + "`; " + sql;
         if (SecurityUtils.getDatabaseType().equals("dm"))
             return "set schema " + SecurityUtils.getDatabaseName() + "; " + sql;
-        if (SecurityUtils.getDatabaseType().equals("mysql"))
-            return "USE `" + SecurityUtils.getDatabaseName() + "`; " + sql;
+        if (SecurityUtils.getDatabaseType().equals("mysql")) {
+            if (sql.contains("{DBNAME}.")) {
+                return sql.replace("{DBNAME}.", "`" + SecurityUtils.getDatabaseName() + "`.");
+            } else {
+                if (sql.contains("information_schema")) return sql;  // 执行当前sql不需要选择数据源
+                StringBuilder sb = new StringBuilder(sql);
+
+                if (sb.indexOf(" from ") != -1) {
+                    sb.insert(sb.indexOf(" from ") + 6, "`" + SecurityUtils.getDatabaseName() + "`.");  // 在目标字符后插入新的字符串
+                } else if (sb.indexOf(" FROM ") != -1) {
+                    sb.insert(sb.indexOf(" FROM ") + 6, "`" + SecurityUtils.getDatabaseName() + "`.");  // 在目标字符后插入新的字符串
+                } else if (sb.indexOf(" into ") != -1) {
+                    sb.insert(sb.indexOf(" into ") + 6, "`" + SecurityUtils.getDatabaseName() + "`.");  // 在目标字符后插入新的字符串
+                } else if (sb.indexOf("update ") != -1) {
+                    sb.insert(sb.indexOf("update ") + 7, "`" + SecurityUtils.getDatabaseName() + "`.");  // 在目标字符后插入新的字符串
+                }
+                return sb.toString();
+            }
+        }
         return "error";
     }
 

+ 38 - 0
ruoyi-framework/src/main/java/com/ruoyi/framework/aspectj/SqlLogAspect.java

@@ -0,0 +1,38 @@
+package com.ruoyi.framework.aspectj;
+
+import org.aspectj.lang.ProceedingJoinPoint;
+import org.aspectj.lang.annotation.Around;
+import org.aspectj.lang.annotation.Aspect;
+import org.springframework.stereotype.Component;
+
+import java.lang.reflect.Field;
+import java.util.List;
+
+import org.aspectj.lang.reflect.MethodSignature;
+
+@Aspect
+@Component
+public class SqlLogAspect {
+
+    @Around("execution(* com.ruoyi.system.mapper.*.*(..))") // 匹配所有Mapper接口的方法
+    public Object logSql(ProceedingJoinPoint joinPoint) throws Throwable {
+        // 获取目标对象、方法和参数
+        Object target = joinPoint.getTarget();
+        String methodName = joinPoint.getSignature().getName();
+        Object[] args = joinPoint.getArgs();
+        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
+        Class<?> returnType = signature.getReturnType();
+
+        // 执行目标方法
+        Object result = joinPoint.proceed();
+
+        // 判断结果集类型
+        if (!(result instanceof List)) {
+            if (returnType == null){
+                return Class.forName(returnType.toString().split("class ")[1]).newInstance();
+            }
+        }
+
+        return result;
+    }
+}

+ 2 - 2
ruoyi-framework/src/main/java/com/ruoyi/framework/web/exception/GlobalExceptionAspect.java

@@ -13,8 +13,8 @@ import org.springframework.stereotype.Component;
  * @author hzh
  * @date 2023-09-21
  */
-@Aspect
-@Component
+//@Aspect
+//@Component
 public class GlobalExceptionAspect {
     // com.ruoyi.system.service.impl
     @Around("execution(* com.ruoyi.system..*(..))") // 切入点表达式

+ 0 - 21
ruoyi-system/src/main/java/com/ruoyi/system/entity/DragTable.java

@@ -254,26 +254,5 @@ public class DragTable extends BaseEntity {
         this.primaryKey = primaryKey;
     }
 
-    public DragTable() {
 
-    }
-
-    public DragTable(Long tId, String dtName, String dtNickname, String tableKey, String sqlKey, String dtTableName, String dtNotes, String dtColumnName, String timeFormat, String spare, String spare1, String delFlag, String isSelection, String echoData, Long menuId, String primaryKey) {
-        this.tId = tId;
-        this.dtName = dtName;
-        this.dtNickname = dtNickname;
-        this.tableKey = tableKey;
-        this.sqlKey = sqlKey;
-        this.dtTableName = dtTableName;
-        this.dtNotes = dtNotes;
-        this.dtColumnName = dtColumnName;
-        this.timeFormat = timeFormat;
-        this.spare = spare;
-        this.spare1 = spare1;
-        this.delFlag = delFlag;
-        this.isSelection = isSelection;
-        this.echoData = echoData;
-        this.menuId = menuId;
-        this.primaryKey = primaryKey;
-    }
 }

+ 0 - 14
ruoyi-system/src/main/java/com/ruoyi/system/entity/TableSql.java

@@ -152,19 +152,5 @@ public class TableSql extends BaseEntity {
         this.sortOrder = sortOrder;
     }
 
-    public TableSql() {
 
-    }
-
-    public TableSql(Long tId, String tableSql, String tableCondition, String tableAlias, String sqlKey, String tableExportField, String delFlag, String orderByColumn, String sortOrder) {
-        this.tId = tId;
-        this.tableSql = tableSql;
-        this.tableCondition = tableCondition;
-        this.tableAlias = tableAlias;
-        this.sqlKey = sqlKey;
-        this.tableExportField = tableExportField;
-        this.delFlag = delFlag;
-        this.orderByColumn = orderByColumn;
-        this.sortOrder = sortOrder;
-    }
 }

+ 1 - 1
ruoyi-system/src/main/java/com/ruoyi/system/service/impl/CommonServiceImpl.java

@@ -152,7 +152,7 @@ public class CommonServiceImpl implements ICommonService {
 //        String tableName = commonEntity.getQueryMap().get("tableName").toString();
         //查询标题信息
         TableSql tableSql = tableSqlMapper.selectTableSqlByTSqlKey(sqlKey);
-
+        if (tableSql == null) return;
         //查询列表信息
         List<Map<String, Object>> mapList = null;
         //根据条件获取列表信息

+ 1 - 1
ruoyi-system/src/main/resources/mapper/dragmapper/DragTableConditionMapper.xml

@@ -105,7 +105,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
         where t_id = #{tId}
     </select>
 
-    <select id="selectDragTableSearchField" resultType="string">
+    <select id="selectDragTableSearchField" resultType="java.lang.String">
         select condition_field from drag_table_condition where t_id = #{tId}
     </select>
 

+ 1 - 3
ruoyi-system/src/main/resources/mapper/dragmapper/DragTableMapper.xml

@@ -162,9 +162,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
         SELECT
             dt.*,
             ts.table_sql,
-            ts.table_export_field
-        FROM
-            drag_table dt
+            ts.table_export_field FROM drag_table dt
                 LEFT JOIN table_sql ts ON dt.sql_key = ts.sql_key
         WHERE
             dt.t_id = #{tId}