Browse Source

优化判断是否继续执行 Limit 逻辑

hubin 5 năm trước cách đây
mục cha
commit
3056073f52

+ 26 - 6
mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/plugins/PaginationInterceptor.java

@@ -198,7 +198,7 @@ public class PaginationInterceptor extends AbstractSqlParserHandler implements I
         if (page.isSearchCount() && !page.isHitCount()) {
             SqlInfo sqlInfo = SqlParserUtils.getOptimizeCountSql(page.optimizeCountSql(), countSqlParser, originalSql);
             this.queryTotal(sqlInfo.getSql(), mappedStatement, boundSql, page, connection);
-            if (page.getTotal() <= 0) {
+            if (!this.continueLimit(page)) {
                 return null;
             }
         }
@@ -215,6 +215,28 @@ public class PaginationInterceptor extends AbstractSqlParserHandler implements I
         return invocation.proceed();
     }
 
+    /**
+     * 判断是否继续执行 Limit 逻辑
+     *
+     * @param page 分页对象
+     * @return
+     */
+    protected boolean continueLimit(IPage<?> page) {
+        if(page.getTotal() <= 0) {
+            return false;
+        }
+        if (page.getCurrent() > page.getPages()) {
+            if (this.overflow) {
+                //溢出总页数处理
+                handlerOverflow(page);
+            } else {
+                // 超过最大范围,未设置溢出逻辑中断 list 执行
+                return false;
+            }
+        }
+        return true;
+    }
+
     /**
      * 处理超出分页条数限制,默认归为限制数
      *
@@ -232,6 +254,7 @@ public class PaginationInterceptor extends AbstractSqlParserHandler implements I
      * @param boundSql        BoundSql
      * @param page            IPage
      * @param connection      Connection
+     * @return true 继续执行 false 中断 list 执行
      */
     protected void queryTotal(String sql, MappedStatement mappedStatement, BoundSql boundSql, IPage<?> page, Connection connection) {
         try (PreparedStatement statement = connection.prepareStatement(sql)) {
@@ -244,15 +267,12 @@ public class PaginationInterceptor extends AbstractSqlParserHandler implements I
                 }
             }
             page.setTotal(total);
-            if (this.overflow && page.getCurrent() > page.getPages()) {
-                //溢出总页数处理
-                handlerOverflow(page);
-            }
         } catch (Exception e) {
             throw ExceptionUtils.mpe("Error: Method queryTotal execution error of sql : \n %s \n", e, sql);
         }
     }
 
+
     /**
      * 处理页数溢出,默认设置为第一页
      *
@@ -291,7 +311,7 @@ public class PaginationInterceptor extends AbstractSqlParserHandler implements I
             setLimit(Long.parseLong(limit));
         }
     }
-    
+
     /**
      * 设置方言类型
      *