pagehelper / Mybatis-PageHelper

Mybatis通用分页插件
https://mybatis.io
MIT License
12.2k stars 3.13k forks source link

使用offset分页时,total不准确 #760

Closed Pink-Starfish closed 11 months ago

Pink-Starfish commented 1 year ago

问题描述

我在使用offset分页时,total不准确,total只会返回我查询到的数量,而不是在执行select count(0) from table返回的数量

使用环境

com.github.pagehelper pagehelper-spring-boot-starter 1.4.6

### 分页参数
![image](https://github.com/pagehelper/Mybatis-PageHelper/assets/139975397/a7b548d9-e46c-4d74-b5e4-4458f76248a6)

### SQL 

JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@443a37b0] will not be managed by Spring ==> Preparing: SELECT count(0) FROM t_community_video_comment tcvc WHERE video_id = ? AND tcvc.level = 1 ==> Parameters: 27896(Integer) <== Columns: count(0) <== Row: 3 <== Total: 1 ==> Preparing: SELECT tcvc.*, (SELECT count(id) FROM t_community_video_comment WHERE super_comment_id = tcvc.id) reply_count FROM t_community_video_comment tcvc WHERE video_id = ? AND tcvc.level = 1 order by create_time asc LIMIT ? ==> Parameters: 27896(Integer), 2(Integer) <== Columns: id, level, parent_comment_id, super_comment_id, video_id, user_id, nick_name, content, approve_status, is_deleted, create_by, create_time, update_by, update_time, reply_count <== Row: 1, 1, null, null, 27896, 1, 管理员, 我是一级评论, null, 0, admin, 2023-07-15 17:34:43, null, null, 3 <== Row: 2, 1, null, null, 27896, 2, 游客2, 我也是一级评论, null, 0, admin, 2023-07-15 17:35:13, null, null, 0 <== Total: 2 Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@6b376ae7]

### 分页代码
``` java
Integer offset = 0;
Integer limit = 2;
PageHelper.offsetPage(offset, limit);
Pink-Starfish commented 1 year ago

在执行new PageInfo时

public PageSerializable(List<? extends T> list) {
    this.list = (List<T>) list;
    if(list instanceof Page){
        this.total = ((Page<?>)list).getTotal();
    } else {
        this.total = list.size();
    }
}

这个if总是为false,因为这个查询是我自己写的,返回的就是一个List。

Pink-Starfish commented 1 year ago

我还测试了,startPage,结果也是如此,sql跟offset分页时的sql是一模一样的。 image

abel533 commented 1 year ago

如果pagehelper分页插件起作用,返回的List实际类型一定是Page。

Pink-Starfish commented 1 year ago

如果pagehelper分页插件起作用,返回的List实际类型一定是Page。 你的意思是我的分页他没有生效是吗?没生效它也会执行select count(0) from table吗?

Pink-Starfish commented 1 year ago

你的意思是我的分页他没有生效是吗?没生效它也会执行select count(0) from table吗?

在 2023-07-20 13:58:59,"Liuzh" @.***> 写道:

如果pagehelper分页插件起作用,返回的List实际类型一定是Page。

— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you authored the thread.Message ID: @.***>

Pink-Starfish commented 1 year ago

如果pagehelper分页插件起作用,返回的List实际类型一定是Page。

我大概知道了,是因为我从数据库查出来是DTO,然后在封装响应体时把DTO转换成了VO,导致这个list不是原来那个list

jidaojiuyou commented 10 months ago

牛。已经选择从var page = PageHelper.startPage()中获取总条数了