springboot mybatis plus 分页插件不生效

本文详细介绍了如何在SpringBoot项目中正确配置和使用分页插件,包括在pom.xml中引入分页插件库,配置yml文件中的分页插件参数,以及在代码中启用分页插件拦截器。

1  pom.xml中  核对是否引入 分页插件库

 <!-- 分页插件 -->
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-starter</artifactId>
            <version>1.2.3</version>
        </dependency>

 

 

 

2  核对 springboot  配置文件中分页插件是否配置 (yml配置如下)

#pagehelper分页插件
pagehelper:
    helperDialect: mysql
    reasonable: true
    supportMethodsArguments: true
    params: count=countSql

3  检查分页插件拦截器是否启用

 

 

@Configuration
public class MybatisPlusConfig
{
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        PaginationInterceptor page = new PaginationInterceptor();
        page.setDialectType("mysql");
        return page;
    }
}
 

### 分页功能不生效的可能原因及解决方法 #### 1. **未正确配置分页插件** MyBatis Plus分页功能依赖于 `MybatisPlusInterceptor`,其中必须添加 `PaginationInnerInterceptor`。如果未在配置类中注册该拦截器,分页查询将无法正常执行,导致返回结果中的 `total` 值为 0 或分页数据完整 [^2]。 示例配置如下: ```java @Configuration public class MyBatisConfig { @Bean public MybatisPlusInterceptor mybatisPlusInterceptor() { MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor(); interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL)); return interceptor; } } ``` #### 2. **数据库方言未正确设置** `PaginationInnerInterceptor` 需要根据数据库类型进行适配,例如 MySQL、PostgreSQL 等。如果未指定正确的 DbType,则可能导致分页 SQL 生成错误,从而影响分页效果 [^3]。 可以在配置中显式指定数据库类型: ```java interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL)); ``` #### 3. **分页查询逻辑使用了错误的 Page 对象** 确保使用的是 `com.baomidou.mybatisplus.extension.plugins.pagination.Page` 类来构建分页参数。若使用了自定义或错误的 Page 实现,可能导致分页逻辑失效 [^2]。 正确的使用方式如下: ```java IPage<User> page = new Page<>(pageNum, pageSize); userMapper.selectUserListPage(page, "张三"); long total = page.getTotal(); // 获取总记录数 ``` #### 4. **SQL 查询结构复杂导致 COUNT 查询失败** 当分页查询涉及多表 JOIN 或子查询时,MyBatis Plus 自动生成的 COUNT 查询可能无法正确执行,导致 `total` 始终为 0。此时需要手动指定 COUNT 查询语句,并通过 `@SelectCount` 注解绑定到对应的查询方法上 [^1]。 示例如下: ```java @Select("selectUserListPage") @SelectProvider(type = UserSqlProvider.class, method = "selectUserListPage") @SelectCount("selectUserListPageCount") // 指定 COUNT 查询方法 IPage<User> selectUserListPage(IPage<User> page, @Param("name") String name); ``` #### 5. **Mapper 接口未被 Spring 扫描加载** 如果 Mapper 接口未被正确扫描加载,调用分页方法时将抛出异常或返回空数据。需确保主应用类上标注了 `@MapperScan`,并指定了正确的 Mapper 包路径 [^1]。 示例如下: ```java @SpringBootApplication @MapperScan("com.example.mapper") public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` #### 6. **日志输出未开启,难以排查问题** 启用 SQL 日志输出可以帮助查看实际执行的 SQL 语句,确认是否包含分页逻辑或是否存在语法错误。可在 application.yml 中开启日志调试模式 [^1]。 配置如下: ```yaml logging: level: com.example.mapper: debug # 替换为你的 Mapper 包路径 ``` ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值