浏览代码

fixed github issues/234

= 7 年之前
父节点
当前提交
d952e189d3

+ 3 - 3
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/plugins/pagination/DialectFactory.java

@@ -53,9 +53,9 @@ public class DialectFactory {
      * @throws Exception
      */
     public static String buildPaginationSql(Pagination page, String buildSql, DBType dbType, String dialectClazz)
-            throws Exception {
+        throws Exception {
         // fix #172, 196
-        return getDialect(dbType, dialectClazz).buildPaginationSql(buildSql, page.offsetCurrent(), page.getSize());
+        return getDialect(dbType, dialectClazz).buildPaginationSql(buildSql, PageHelper.offsetCurrent(page), page.getSize());
     }
 
     /**
@@ -70,7 +70,7 @@ public class DialectFactory {
      * @throws Exception
      */
     public static String buildPaginationSql(RowBounds rowBounds, String buildSql, DBType dbType, String dialectClazz)
-            throws Exception {
+        throws Exception {
         // fix #196
         return getDialect(dbType, dialectClazz).buildPaginationSql(buildSql, rowBounds.getOffset(), rowBounds.getLimit());
     }

+ 33 - 2
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/plugins/pagination/PageHelper.java → mybatis-plus-support/src/main/java/com/baomidou/mybatisplus/plugins/pagination/PageHelper.java

@@ -28,7 +28,9 @@ import com.baomidou.mybatisplus.exceptions.MybatisPlusException;
 
 public class PageHelper {
 
-    // 分页本地线程变量
+    /**
+     * 分页本地线程变量
+     */
     private static final ThreadLocal<Pagination> LOCAL_PAGE = new ThreadLocal<>();
 
     /**
@@ -44,6 +46,34 @@ public class PageHelper {
         }
     }
 
+    /**
+     * <p>
+     * 计算当前分页偏移量
+     * </p>
+     *
+     * @param current 当前页
+     * @param size    每页显示数量
+     * @return
+     */
+    public static int offsetCurrent(int current, int size) {
+        if (current > 0) {
+            return (current - 1) * size;
+        }
+        return 0;
+    }
+
+    /**
+     * <p>
+     * Pagination 分页偏移量
+     * </p>
+     */
+    public static int offsetCurrent(Pagination pagination) {
+        if (null == pagination) {
+            return 0;
+        }
+        return offsetCurrent(pagination.getCurrent(), pagination.getSize());
+    }
+
     /**
      * <p>
      * 释放资源并获取总条数
@@ -51,7 +81,8 @@ public class PageHelper {
      */
     public static int freeTotal() {
         int total = getTotal();
-        remove();// 释放资源
+        // 释放资源
+        remove();
         return total;
     }
 

+ 1 - 12
mybatis-plus-support/src/main/java/com/baomidou/mybatisplus/plugins/pagination/Pagination.java

@@ -131,7 +131,7 @@ public class Pagination extends RowBounds implements Serializable {
     }
 
     public Pagination(int current, int size, boolean searchCount, boolean openSort) {
-        super(offsetCurrent(current, size), size);
+        super(PageHelper.offsetCurrent(current, size), size);
         if (current > 1) {
             this.current = current;
         }
@@ -140,17 +140,6 @@ public class Pagination extends RowBounds implements Serializable {
         this.openSort = openSort;
     }
 
-    protected static int offsetCurrent(int current, int size) {
-        if (current > 0) {
-            return (current - 1) * size;
-        }
-        return 0;
-    }
-
-    public int offsetCurrent() {
-        return offsetCurrent(this.current, this.size);
-    }
-
     public boolean hasPrevious() {
         return this.current > 1;
     }