当前位置:网站首页>IPage能正常显示数据,但是total一直等于0
IPage能正常显示数据,但是total一直等于0
2022-07-05 06:51:00 【Emmmmm_1】
问题描述:代码中使用Mybatis-Plus的自动分页,getRecords()正确,但是getTotal()一直等于0
代码逻辑没问题,所以找Mybatis-Plus配置上的问题
尝试的方法:
1. 配置类上是否加了@Configuration
2. 统计的SQL被自动优化了,导致统计结果不准确--->关闭Mybatis-Plus的自动优化
Page<?> page = new Page<>(currentPage, size); // 开启分页
page.setOptimizeCountSql(false); // 关闭mybatisPlus的自动优化这样的方法效率较低,可以自己写count sql
page.setSearchCount(false);
page.setTotal(mapper.listCount(sql));参考文章: MyBatis-Plus的IPage分页total不正确问题
3. 设置数据库的类型
PaginationInnerInterceptor page = new PaginationInnerInterceptor();
page.setDbType(DbType.MYSQL);
return page;4. 检查mybatis-plus-boot-starter版本
3.4.0版本对此部分有更新,如果是旧版本升级,会出现分页失效问题,同时idea会提示PaginationInterceptor过时,新版本改用了MybatisPlusInterceptor

最终是把PaginationInnerInterceptor换成了MybatisPlusInterceptor解决
成功的代码如下:
@Configuration
public class MybatisPlusConfig {
/**
* 实现分页配置
* @return
*/
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor()); // 注册乐观锁插件
interceptor.addInnerInterceptor(new PaginationInnerInterceptor()); // 分页插件
return interceptor;
}
}这部分代码来源:Page分页records有数据,但是total=0,解决办法
边栏推荐
猜你喜欢

Get class files and attributes by reflection

ROS2——topic话题(八)

程序中的负数存储及类型转换

PHY驱动调试之 --- MDIO/MDC接口22号和45号条款(一)

Financial risk control practice -- feature derivation based on time series

Marvell 88e1515 PHY loopback mode test

Ros2 - ros2 vs. ros1 (II)

Speedtree01 generator properties

Ret2xx---- common CTF template proposition in PWN

Ros2 - workspace (V)
随机推荐
并发编程 — 死锁排查及处理
SRE核心体系了解
ROS2——功能包(六)
Ros2 - workspace (V)
new和malloc的区别
Application of recyclerview
[MySQL 8.0 does not support capitalization of table names - corresponding scheme]
Page type
The differences and connections among cookies, sessions, JWT, and tokens
摄像头的MIPI接口、DVP接口和CSI接口
Utf8 encoding
SOC_SD_DATA_FSM
ROS2——ROS2对比ROS1(二)
Genesis builds a new generation of credit system
ROS2——topic话题(八)
H5 embedded app adapts to dark mode
Logical structure and physical structure
Ros2 - configuration development environment (V)
[tf] Unknown: Failed to get convolution algorithm. This is probably because cuDNN failed to initial
Huawei bracelet, how to add medicine reminder?