746 Star 2.8K Fork 834

abel533 / Mybatis_PageHelper

 / 详情

使用RPC时,因Page类引用了RowBounds,导致反序列化失败

已完成
创建于  
2015-09-09 16:50

在使用dubbo时,消费端RPC调用后端服务,消费端并没有任何连接数据库的权限,因此不需要引入mybatis依赖,此时后端返回的集合是分页插件的Page类,这个类引用了RowBounds,导致消费端反序列化Page类时,找不到RowBounds类。

From 2958c17800771fb313cb1acd150d087c11a7a133 Mon Sep 17 00:00:00 2001
From: "Jinkai.Ma" <majinkai@handu.com>
Date: Wed, 9 Sep 2015 16:44:59 +0800
Subject: [PATCH] =?UTF-8?q?=E4=B8=8D=E4=BE=9D=E8=B5=96RowBounds?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 src/main/java/com/github/pagehelper/Page.java    | 17 +++++------------
 src/main/java/com/github/pagehelper/SqlUtil.java |  6 ++----
 2 files changed, 7 insertions(+), 16 deletions(-)

diff --git a/src/main/java/com/github/pagehelper/Page.java b/src/main/java/com/github/pagehelper/Page.java
index 314b9dc..4c30948 100644
--- a/src/main/java/com/github/pagehelper/Page.java
+++ b/src/main/java/com/github/pagehelper/Page.java
@@ -24,8 +24,6 @@

 package com.github.pagehelper;

-import org.apache.ibatis.session.RowBounds;
-
 import java.util.ArrayList;
 import java.util.List;

@@ -105,23 +103,18 @@ public class Page<E> extends ArrayList<E> {
         setReasonable(reasonable);
     }

-    public Page(RowBounds rowBounds, boolean count) {
-        this(rowBounds, count ? Page.SQL_COUNT : Page.NO_SQL_COUNT);
-    }
-
-
-    public Page(RowBounds rowBounds, int total) {
+    public Page(int offset, int limit, int total) {
         super(0);
-        if (rowBounds.getOffset() == 0 && rowBounds.getLimit() == Integer.MAX_VALUE) {
+        if (offset == 0 && limit == Integer.MAX_VALUE) {
             pageSizeZero = true;
             this.pageSize = 0;
         } else {
-            this.pageSize = rowBounds.getLimit();
+            this.pageSize = limit;
         }
-        this.startRow = rowBounds.getOffset();
+        this.startRow = offset;
         //RowBounds方式默认不求count总数,如果想求count,可以修改这里为SQL_COUNT
         this.total = total;
-        this.endRow = this.startRow + rowBounds.getLimit();
+        this.endRow = this.startRow + limit;
     }

     public List<E> getResult() {
diff --git a/src/main/java/com/github/pagehelper/SqlUtil.java b/src/main/java/com/github/pagehelper/SqlUtil.java
index 81e3675..241bcdf 100644
--- a/src/main/java/com/github/pagehelper/SqlUtil.java
+++ b/src/main/java/com/github/pagehelper/SqlUtil.java
@@ -294,10 +294,8 @@ public class SqlUtil implements Constant {
         if (page == null) {
             if (params instanceof RowBounds) {
                 RowBounds rowBounds = (RowBounds) params;
-                if (offsetAsPageNum) {
-                    page = new Page(rowBounds.getOffset(), rowBounds.getLimit(), rowBoundsWithCount);
-                } else {
-                    page = new Page(rowBounds, rowBoundsWithCount);
+                page = new Page(rowBounds.getOffset(), rowBounds.getLimit(), rowBoundsWithCount);
+                if (!offsetAsPageNum) {
                     //offsetAsPageNum=false的时候,由于PageNum问题,不能使用reasonable,这里会强制为false
                     page.setReasonable(false);
                 }
--
2.3.2 (Apple Git-55)

评论 (8)

按照你这种方式修改后,实际调用的:

public Page(int pageNum, int pageSize, boolean count) {
    this(pageNum, pageSize, count ? Page.SQL_COUNT : Page.NO_SQL_COUNT, null);
}

offset就直接当成了pageNum,所以不能直接这么改。

Page类的构造方法由于1.0版本的原因,看着很乱。。

你有没有更好的修改方法?

不影响代码逻辑的话,可以提取出一个vo类,来代替RowBounds,但是看代码,只有SqlUtil.java的public Page getPage(Object params)使用到了,把方法参数改成int[]就行了,这样也不影响代码逻辑

@马金凯 确实可以考虑int[]

@马金凯 我看了PR就是int[],我直接合并了。

状态更改为 已关闭

使用dubbo回调时,返回的list对象分页信息丢失,
原因:fastjson,hessian对对象继承实现Collection做了特殊实现,导致序列化时并未对属性进行序列化。

@Lowis 这种情况建议你参考PageInfo把结果转换为非List类型后返回

登录 后才可以发表评论

状态
负责人
里程碑
Pull Requests
关联的 Pull Requests 被合并后可能会关闭此 issue
分支
开始日期   -   截止日期
-
置顶选项
优先级
参与者(4)
8363 free 1578914558 97476 lowis 1578917355 312794 handu 1578919945
Java
1
https://gitee.com/free/Mybatis_PageHelper.git
git@gitee.com:free/Mybatis_PageHelper.git
free
Mybatis_PageHelper
Mybatis_PageHelper

搜索帮助