Pārlūkot izejas kodu

新增 localPage 分页模式

= 8 gadi atpakaļ
vecāks
revīzija
13ba70180b

+ 0 - 65
src/main/java/com/baomidou/mybatisplus/plugins/PageHelper.java

@@ -1,65 +0,0 @@
-package com.baomidou.mybatisplus.plugins;
-
-import com.baomidou.mybatisplus.exceptions.MybatisPlusException;
-import com.baomidou.mybatisplus.plugins.pagination.Pagination;
-
-/**
- * 分页工具类
- *
- * @author liutao
- * 2017-06-24 上午10:42
- */
-
-public class PageHelper {
-
-    /**
-     * 分页本地线程变量
-     */
-    private static final ThreadLocal<Pagination> pageThl = new ThreadLocal<>();
-
-    /**
-     * 获取总条数
-     * @return
-     */
-    public static int getTotal(){
-        if (isPageable()) {
-            return pageThl.get().getTotal();
-        } else {
-            throw new MybatisPlusException("当前线程并没有启动分页,请在查询前调用PageHelper.startPage");
-        }
-    }
-
-    /**
-     * 获取分页
-     * @return
-     */
-    public static Pagination getPagination(){
-        return pageThl.get();
-    }
-
-    /**
-     * 设置分页
-     * @param page
-     */
-    public static void setPagination(Pagination page){
-        pageThl.set(page);
-    }
-
-    /**
-     * 启动分页
-     * @param current 当前页
-     * @param size 页大小
-     */
-    public static void startPage(int current, int size){
-        Pagination page = new Pagination(current, size);
-        pageThl.set(page);
-    }
-
-    /**
-     * 是否存在分页
-     * @return
-     */
-    public static boolean isPageable(){
-        return pageThl.get() != null ? true : false;
-    }
-}

+ 16 - 6
src/main/java/com/baomidou/mybatisplus/plugins/PaginationInterceptor.java

@@ -37,10 +37,11 @@ import org.apache.ibatis.scripting.defaults.DefaultParameterHandler;
 import org.apache.ibatis.session.RowBounds;
 import org.apache.ibatis.session.RowBounds;
 
 
 import com.baomidou.mybatisplus.MybatisDefaultParameterHandler;
 import com.baomidou.mybatisplus.MybatisDefaultParameterHandler;
-import com.baomidou.mybatisplus.plugins.pagination.DialectFactory;
-import com.baomidou.mybatisplus.plugins.pagination.Pagination;
 import com.baomidou.mybatisplus.parser.AbstractSqlParser;
 import com.baomidou.mybatisplus.parser.AbstractSqlParser;
 import com.baomidou.mybatisplus.parser.SqlInfo;
 import com.baomidou.mybatisplus.parser.SqlInfo;
+import com.baomidou.mybatisplus.plugins.pagination.DialectFactory;
+import com.baomidou.mybatisplus.plugins.pagination.PageHelper;
+import com.baomidou.mybatisplus.plugins.pagination.Pagination;
 import com.baomidou.mybatisplus.toolkit.JdbcUtils;
 import com.baomidou.mybatisplus.toolkit.JdbcUtils;
 import com.baomidou.mybatisplus.toolkit.PluginUtils;
 import com.baomidou.mybatisplus.toolkit.PluginUtils;
 import com.baomidou.mybatisplus.toolkit.SqlUtils;
 import com.baomidou.mybatisplus.toolkit.SqlUtils;
@@ -70,6 +71,8 @@ public class PaginationInterceptor implements Interceptor {
     private String dialectType;
     private String dialectType;
     /* 方言实现类 */
     /* 方言实现类 */
     private String dialectClazz;
     private String dialectClazz;
+    /* 是否开启 PageHelper localPage 模式 */
+    private boolean localPage = false;
 
 
     /**
     /**
      * Physical Pagination Interceptor for all the queries with parameter {@link org.apache.ibatis.session.RowBounds}
      * Physical Pagination Interceptor for all the queries with parameter {@link org.apache.ibatis.session.RowBounds}
@@ -85,9 +88,11 @@ public class PaginationInterceptor implements Interceptor {
         RowBounds rowBounds = (RowBounds) metaStatementHandler.getValue("delegate.rowBounds");
         RowBounds rowBounds = (RowBounds) metaStatementHandler.getValue("delegate.rowBounds");
         /* 不需要分页的场合 */
         /* 不需要分页的场合 */
         if (rowBounds == null || rowBounds == RowBounds.DEFAULT) {
         if (rowBounds == null || rowBounds == RowBounds.DEFAULT) {
-            // @lt 采用ThreadLocal变量处理的分页
-            rowBounds = PageHelper.getPagination();
-            if (rowBounds == null){
+            if (localPage) {
+                // 采用ThreadLocal变量处理的分页
+                rowBounds = PageHelper.getPagination();
+            }
+            if (rowBounds == null) {
                 return invocation.proceed();
                 return invocation.proceed();
             }
             }
         }
         }
@@ -118,7 +123,8 @@ public class PaginationInterceptor implements Interceptor {
         }
         }
 
 
 		/*
 		/*
-         * <p> 禁用内存分页 </p> <p> 内存分页会查询所有结果出来处理(这个很吓人的),如果结果变化频繁这个数据还会不准。</p>
+         * <p> 禁用内存分页 </p>
+         * <p> 内存分页会查询所有结果出来处理(这个很吓人的),如果结果变化频繁这个数据还会不准。</p>
 		 */
 		 */
         metaStatementHandler.setValue("delegate.boundSql.sql", originalSql);
         metaStatementHandler.setValue("delegate.boundSql.sql", originalSql);
         metaStatementHandler.setValue("delegate.rowBounds.offset", RowBounds.NO_ROW_OFFSET);
         metaStatementHandler.setValue("delegate.rowBounds.offset", RowBounds.NO_ROW_OFFSET);
@@ -200,4 +206,8 @@ public class PaginationInterceptor implements Interceptor {
     public void setSqlParser(AbstractSqlParser sqlParser) {
     public void setSqlParser(AbstractSqlParser sqlParser) {
         this.sqlParser = sqlParser;
         this.sqlParser = sqlParser;
     }
     }
+
+    public void setLocalPage(boolean localPage) {
+        this.localPage = localPage;
+    }
 }
 }

+ 103 - 0
src/main/java/com/baomidou/mybatisplus/plugins/pagination/PageHelper.java

@@ -0,0 +1,103 @@
+/**
+ * Copyright (c) 2011-2020, hubin (jobob@qq.com).
+ * <p>
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package com.baomidou.mybatisplus.plugins.pagination;
+
+import com.baomidou.mybatisplus.exceptions.MybatisPlusException;
+import com.baomidou.mybatisplus.plugins.pagination.Pagination;
+
+/**
+ * <p>
+ * 分页辅助类
+ * </p>
+ *
+ * @author liutao , hubin
+ * @since 2017-06-24
+ */
+
+public class PageHelper {
+
+    // 分页本地线程变量
+    private static final ThreadLocal<Pagination> LOCAL_PAGE = new ThreadLocal<>();
+
+    /**
+     * <p>
+     * 获取总条数
+     * </p>
+     *
+     * @return
+     */
+    public static int getTotal() {
+        if (isPageable()) {
+            return LOCAL_PAGE.get().getTotal();
+        } else {
+            throw new MybatisPlusException("The current thread does not start paging. Please call before PageHelper.startPage");
+        }
+    }
+
+    /**
+     * <p>
+     * 获取分页
+     * </p>
+     *
+     * @return
+     */
+    public static Pagination getPagination() {
+        return LOCAL_PAGE.get();
+    }
+
+    /**
+     * <p>
+     * 设置分页
+     * </p>
+     *
+     * @param page
+     */
+    public static void setPagination(Pagination page) {
+        LOCAL_PAGE.set(page);
+    }
+
+    /**
+     * <p>
+     * 启动分页
+     * </p>
+     *
+     * @param current 当前页
+     * @param size    页大小
+     */
+    public static void startPage(int current, int size) {
+        LOCAL_PAGE.set(new Pagination(current, size));
+    }
+
+    /**
+     * <p>
+     * 是否存在分页
+     * </p>
+     *
+     * @return
+     */
+    public static boolean isPageable() {
+        return LOCAL_PAGE.get() != null ? true : false;
+    }
+
+    /**
+     * <p>
+     * 释放资源
+     * </p>
+     */
+    public static void remove() {
+        LOCAL_PAGE.remove();
+    }
+}