Bladeren bron

Merge remote-tracking branch 'origin/3.0' into 3.0

miemie 7 jaren geleden
bovenliggende
commit
bd66e55c08

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

@@ -27,17 +27,6 @@ import java.util.List;
  */
  */
 public interface IPage<T> {
 public interface IPage<T> {
 
 
-    /**
-     * <p>
-     * 查询总页数
-     * </p>
-     *
-     * @return true 是 / false 否
-     */
-    default boolean searchCount() {
-        return true;
-    }
-
     /**
     /**
      * <p>
      * <p>
      * 降序字段集合
      * 降序字段集合
@@ -116,25 +105,31 @@ public interface IPage<T> {
      *
      *
      * @return 当前对象
      * @return 当前对象
      */
      */
-    IPage setRecords(List<T> records);
+    IPage<T> setRecords(List<T> records);
 
 
     /**
     /**
      * <p>
      * <p>
      * 当前满足条件总行数
      * 当前满足条件总行数
      * </p>
      * </p>
+     * <p>
+     * 当 total 为 null 或者大于 0 分页插件不在查询总数
+     * </p>
      *
      *
      * @return
      * @return
      */
      */
-    long getTotal();
+    Long getTotal();
 
 
     /**
     /**
      * <p>
      * <p>
      * 设置当前满足条件总行数
      * 设置当前满足条件总行数
      * </p>
      * </p>
+     * <p>
+     * 当 total 为 null 或者大于 0 分页插件不在查询总数
+     * </p>
      *
      *
      * @return 当前对象
      * @return 当前对象
      */
      */
-    IPage setTotal(long total);
+    IPage<T> setTotal(Long total);
 
 
     /**
     /**
      * <p>
      * <p>
@@ -152,7 +147,7 @@ public interface IPage<T> {
      *
      *
      * @return
      * @return
      */
      */
-    IPage setSize(long size);
+    IPage<T> setSize(long size);
 
 
     /**
     /**
      * <p>
      * <p>
@@ -170,6 +165,6 @@ public interface IPage<T> {
      *
      *
      * @return 当前对象
      * @return 当前对象
      */
      */
-    IPage setCurrent(long current);
+    IPage<T> setCurrent(long current);
 
 
 }
 }

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

@@ -49,6 +49,7 @@ import com.baomidou.mybatisplus.core.toolkit.StringUtils;
 import com.baomidou.mybatisplus.core.toolkit.sql.SqlUtils;
 import com.baomidou.mybatisplus.core.toolkit.sql.SqlUtils;
 import com.baomidou.mybatisplus.extension.handlers.SqlParserHandler;
 import com.baomidou.mybatisplus.extension.handlers.SqlParserHandler;
 import com.baomidou.mybatisplus.extension.plugins.pagination.DialectFactory;
 import com.baomidou.mybatisplus.extension.plugins.pagination.DialectFactory;
+import com.baomidou.mybatisplus.extension.plugins.pagination.PageHelper;
 import com.baomidou.mybatisplus.extension.toolkit.JdbcUtils;
 import com.baomidou.mybatisplus.extension.toolkit.JdbcUtils;
 
 
 /**
 /**
@@ -121,13 +122,14 @@ public class PaginationInterceptor extends SqlParserHandler implements Intercept
             }
             }
         }
         }
 
 
-        /* 不需要分页的场合 */
-        if (page == null) {
-            // 本地线程分页
+        /**
+         * 不需要分页的场合
+         */
+        if (null == page) {
             if (localPage) {
             if (localPage) {
                 // 采用ThreadLocal变量处理的分页
                 // 采用ThreadLocal变量处理的分页
-//                page = PageHelper.getPagination();
-                if (page == null) {
+                page = PageHelper.getPage();
+                if (null == page) {
                     return invocation.proceed();
                     return invocation.proceed();
                 }
                 }
             } else {
             } else {
@@ -141,7 +143,7 @@ public class PaginationInterceptor extends SqlParserHandler implements Intercept
         DbType dbType = StringUtils.isNotEmpty(dialectType) ? DbType.getDbType(dialectType) : JdbcUtils.getDbType(connection.getMetaData().getURL());
         DbType dbType = StringUtils.isNotEmpty(dialectType) ? DbType.getDbType(dialectType) : JdbcUtils.getDbType(connection.getMetaData().getURL());
 
 
         boolean orderBy = true;
         boolean orderBy = true;
-        if (page.searchCount()) {
+        if (null != page.getTotal() && page.getTotal().equals(0L)) {
             SqlInfo sqlInfo = SqlUtils.getOptimizeCountSql(page.optimizeCountSql(), sqlParser, originalSql);
             SqlInfo sqlInfo = SqlUtils.getOptimizeCountSql(page.optimizeCountSql(), sqlParser, originalSql);
             orderBy = sqlInfo.isOrderBy();
             orderBy = sqlInfo.isOrderBy();
             this.queryTotal(overflow, sqlInfo.getSql(), mappedStatement, boundSql, page, connection);
             this.queryTotal(overflow, sqlInfo.getSql(), mappedStatement, boundSql, page, connection);
@@ -222,10 +224,10 @@ public class PaginationInterceptor extends SqlParserHandler implements Intercept
         try (PreparedStatement statement = connection.prepareStatement(sql)) {
         try (PreparedStatement statement = connection.prepareStatement(sql)) {
             DefaultParameterHandler parameterHandler = new MybatisDefaultParameterHandler(mappedStatement, boundSql.getParameterObject(), boundSql);
             DefaultParameterHandler parameterHandler = new MybatisDefaultParameterHandler(mappedStatement, boundSql.getParameterObject(), boundSql);
             parameterHandler.setParameters(statement);
             parameterHandler.setParameters(statement);
-            int total = 0;
+            long total = 0;
             try (ResultSet resultSet = statement.executeQuery()) {
             try (ResultSet resultSet = statement.executeQuery()) {
                 if (resultSet.next()) {
                 if (resultSet.next()) {
-                    total = resultSet.getInt(1);
+                    total = resultSet.getLong(1);
                 }
                 }
             }
             }
             page.setTotal(total);
             page.setTotal(total);

+ 21 - 20
mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/plugins/pagination/PageHelper.java

@@ -16,6 +16,7 @@
 package com.baomidou.mybatisplus.extension.plugins.pagination;
 package com.baomidou.mybatisplus.extension.plugins.pagination;
 
 
 import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
 import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
+import com.baomidou.mybatisplus.core.metadata.IPage;
 
 
 
 
 /**
 /**
@@ -32,20 +33,32 @@ public class PageHelper {
     /**
     /**
      * 分页本地线程变量
      * 分页本地线程变量
      */
      */
-    private static final ThreadLocal<Pagination> LOCAL_PAGE = new ThreadLocal<>();
+    private static final ThreadLocal<IPage> LOCAL_PAGE = new ThreadLocal<>();
 
 
     /**
     /**
      * <p>
      * <p>
      * 获取总条数
      * 获取总条数
      * </p>
      * </p>
      */
      */
-    public static long getTotal() {
+    public static Long getTotal() {
         if (isPageable()) {
         if (isPageable()) {
             return LOCAL_PAGE.get().getTotal();
             return LOCAL_PAGE.get().getTotal();
         }
         }
         throw new MybatisPlusException("The current thread does not start paging. Please call before PageHelper.startPage");
         throw new MybatisPlusException("The current thread does not start paging. Please call before PageHelper.startPage");
     }
     }
 
 
+    /**
+     * <p>
+     * 释放资源并获取总条数
+     * </p>
+     */
+    public static Long freeTotal() {
+        Long total = getTotal();
+        // 释放资源
+        remove();
+        return total;
+    }
+
     /**
     /**
      * <p>
      * <p>
      * 计算当前分页偏移量
      * 计算当前分页偏移量
@@ -67,23 +80,11 @@ public class PageHelper {
      * Pagination 分页偏移量
      * Pagination 分页偏移量
      * </p>
      * </p>
      */
      */
-    public static long offsetCurrent(Pagination pagination) {
-        if (null == pagination) {
+    public static long offsetCurrent(IPage page) {
+        if (null == page) {
             return 0;
             return 0;
         }
         }
-        return offsetCurrent(pagination.getCurrent(), pagination.getSize());
-    }
-
-    /**
-     * <p>
-     * 释放资源并获取总条数
-     * </p>
-     */
-    public static long freeTotal() {
-        long total = getTotal();
-        // 释放资源
-        remove();
-        return total;
+        return offsetCurrent(page.getCurrent(), page.getSize());
     }
     }
 
 
     /**
     /**
@@ -91,7 +92,7 @@ public class PageHelper {
      * 获取分页
      * 获取分页
      * </p>
      * </p>
      */
      */
-    public static Pagination getPagination() {
+    public static IPage getPage() {
         return LOCAL_PAGE.get();
         return LOCAL_PAGE.get();
     }
     }
 
 
@@ -100,7 +101,7 @@ public class PageHelper {
      * 设置分页
      * 设置分页
      * </p>
      * </p>
      */
      */
-    public static void setPagination(Pagination page) {
+    public static void setPage(IPage page) {
         LOCAL_PAGE.set(page);
         LOCAL_PAGE.set(page);
     }
     }
 
 
@@ -112,7 +113,7 @@ public class PageHelper {
      * @param current 当前页
      * @param current 当前页
      * @param size    页大小
      * @param size    页大小
      */
      */
-    public static void startPage(int current, int size) {
+    public static void startPage(long current, long size) {
         LOCAL_PAGE.set(new Pagination(current, size));
         LOCAL_PAGE.set(new Pagination(current, size));
     }
     }
 
 

+ 14 - 31
mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/plugins/pagination/Pagination.java

@@ -37,26 +37,18 @@ public class Pagination<T> implements IPage<T>, Serializable {
      * 查询数据列表
      * 查询数据列表
      */
      */
     private List<T> records = Collections.emptyList();
     private List<T> records = Collections.emptyList();
-
     /**
     /**
-     * 总数
+     * 总数,当 total 为 null 或者大于 0 分页插件不在查询总数
      */
      */
-    private long total;
-
+    private Long total = 0L;
     /**
     /**
      * 每页显示条数,默认 10
      * 每页显示条数,默认 10
      */
      */
     private long size = 10;
     private long size = 10;
-
     /**
     /**
      * 当前页
      * 当前页
      */
      */
     private long current = 1;
     private long current = 1;
-
-    /**
-     * 查询总记录数(默认 true)
-     */
-    private boolean searchCount = true;
     /**
     /**
      * <p>
      * <p>
      * SQL 排序 ASC 集合
      * SQL 排序 ASC 集合
@@ -84,15 +76,15 @@ public class Pagination<T> implements IPage<T>, Serializable {
      * @param size    每页显示条数
      * @param size    每页显示条数
      */
      */
     public Pagination(long current, long size) {
     public Pagination(long current, long size) {
-        this(current, size, true);
+        this(current, size, 0L);
     }
     }
 
 
-    public Pagination(long current, long size, boolean searchCount) {
+    public Pagination(long current, long size, Long total) {
         if (current > 1) {
         if (current > 1) {
             this.current = current;
             this.current = current;
         }
         }
         this.size = size;
         this.size = size;
-        this.searchCount = searchCount;
+        this.total = total;
     }
     }
 
 
     /**
     /**
@@ -123,24 +115,24 @@ public class Pagination<T> implements IPage<T>, Serializable {
     }
     }
 
 
     @Override
     @Override
-    public IPage setRecords(List records) {
+    public IPage<T> setRecords(List records) {
         this.records = records;
         this.records = records;
         return this;
         return this;
     }
     }
 
 
     @Override
     @Override
-    public Pagination setTotal(long total) {
+    public Pagination<T> setTotal(Long total) {
         this.total = total;
         this.total = total;
         return this;
         return this;
     }
     }
 
 
     @Override
     @Override
-    public long getTotal() {
+    public Long getTotal() {
         return this.total;
         return this.total;
     }
     }
 
 
     @Override
     @Override
-    public Pagination setSize(long size) {
+    public Pagination<T> setSize(long size) {
         this.size = size;
         this.size = size;
         return this;
         return this;
     }
     }
@@ -151,7 +143,7 @@ public class Pagination<T> implements IPage<T>, Serializable {
     }
     }
 
 
     @Override
     @Override
-    public Pagination setCurrent(long current) {
+    public Pagination<T> setCurrent(long current) {
         this.current = current;
         this.current = current;
         return this;
         return this;
     }
     }
@@ -161,26 +153,17 @@ public class Pagination<T> implements IPage<T>, Serializable {
         return this.current;
         return this.current;
     }
     }
 
 
-    @Override
-    public boolean searchCount() {
-        return searchCount;
-    }
-
-    public void setSearchCount(boolean searchCount) {
-        this.searchCount = searchCount;
-    }
-
     @Override
     @Override
     public List<String> ascs() {
     public List<String> ascs() {
         return ascs;
         return ascs;
     }
     }
 
 
-    public Pagination setAscs(List<String> ascs) {
+    public Pagination<T> setAscs(List<String> ascs) {
         this.ascs = ascs;
         this.ascs = ascs;
         return this;
         return this;
     }
     }
 
 
-    public Pagination setAscs(String... ascs) {
+    public Pagination<T> setAscs(String... ascs) {
         if (ArrayUtils.isNotEmpty(ascs)) {
         if (ArrayUtils.isNotEmpty(ascs)) {
             this.ascs = Arrays.asList(ascs);
             this.ascs = Arrays.asList(ascs);
         }
         }
@@ -192,12 +175,12 @@ public class Pagination<T> implements IPage<T>, Serializable {
         return descs;
         return descs;
     }
     }
 
 
-    public Pagination setDescs(List<String> descs) {
+    public Pagination<T> setDescs(List<String> descs) {
         this.descs = descs;
         this.descs = descs;
         return this;
         return this;
     }
     }
 
 
-    public Pagination setDescs(String... descs) {
+    public Pagination<T> setDescs(String... descs) {
         if (ArrayUtils.isNotEmpty(descs)) {
         if (ArrayUtils.isNotEmpty(descs)) {
             this.descs = Arrays.asList(descs);
             this.descs = Arrays.asList(descs);
         }
         }