Przeglądaj źródła

IPage 增加 isSearchCount 开关

miemie 6 lat temu
rodzic
commit
479e2d617c

+ 11 - 0
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/metadata/IPage.java

@@ -76,6 +76,17 @@ public interface IPage<T> extends Serializable {
         return true;
     }
 
+    /**
+     * <p>
+     * 进行 count 查询 【 默认: true 】
+     * </p>
+     *
+     * @return true 是 / false 否
+     */
+    default boolean isSearchCount() {
+        return true;
+    }
+
     /**
      * <p>
      * 计算当前分页偏移量

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

@@ -164,7 +164,7 @@ public class PaginationInterceptor extends AbstractSqlParserHandler implements I
             : JdbcUtils.getDbType(connection.getMetaData().getURL());
 
         boolean orderBy = true;
-        if (page.getTotal() == 0) {
+        if (page.isSearchCount()) {
             // total 为0 才进行 count
             SqlInfo sqlInfo = SqlParserUtils.getOptimizeCountSql(page.optimizeCountSql(), sqlParser, originalSql);
             orderBy = sqlInfo.isOrderBy();

+ 29 - 1
mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/plugins/pagination/Page.java

@@ -38,7 +38,7 @@ public class Page<T> implements IPage<T> {
      */
     private List<T> records = Collections.emptyList();
     /**
-     * 总数,当 total 为 0 时,分页插件才会进行 count 查询
+     * 总数
      */
     private long total = 0;
     /**
@@ -67,6 +67,12 @@ public class Page<T> implements IPage<T> {
      * </p>
      */
     private boolean optimizeCountSql = true;
+    /**
+     * <p>
+     * 是否进行 count 查询
+     * </p>
+     */
+    private boolean isSearchCount = true;
 
     public Page() {
         // to do nothing
@@ -85,11 +91,23 @@ public class Page<T> implements IPage<T> {
     }
 
     public Page(long current, long size, long total) {
+        this(current, size, total, true);
+    }
+
+    public Page(long current, long size, boolean isSearchCount) {
+        this(current, size, 0, isSearchCount);
+    }
+
+    public Page(long current, long size, long total, boolean isSearchCount) {
         if (current > 1) {
             this.current = current;
         }
         this.size = size;
         this.total = total;
+        this.isSearchCount = isSearchCount;
+        if (total < 0) {
+            this.isSearchCount = false;
+        }
     }
 
     /**
@@ -212,6 +230,16 @@ public class Page<T> implements IPage<T> {
         return optimizeCountSql;
     }
 
+    @Override
+    public boolean isSearchCount() {
+        return isSearchCount;
+    }
+
+    Page<T> setSearchCount(boolean isSearchCount) {
+        this.isSearchCount = isSearchCount;
+        return this;
+    }
+
     public Page<T> setOptimizeCountSql(boolean optimizeCountSql) {
         this.optimizeCountSql = optimizeCountSql;
         return this;