【定位问题】Mybatis-plus的selectPage()分页查询不生效问题

背景:

项目需要从mybits切换到mubits-plus,但是我在进行分页查询的时候,发现一直不生效

问题原因:

添加监听器,配置如下:

@Configuration
@MapperScan("com.baomidou.mybatisplus.samples.deluxe.mapper")
public class MybatisPlusConfig {

    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor());
        interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
        return interceptor;
    }

    /**
     * 自定义 SqlInjector
     * 里面包含自定义的全局方法
     */
    @Bean
    public MyLogicSqlInjector myLogicSqlInjector() {
        return new MyLogicSqlInjector();
    }
}

问题定位过程:

如果不感兴趣着,可以在上面复制答案,直接操作就行

debug看看啥原因:

入参:

debug看没有问题

 

结果:

两条数据同时没有总数,不符合要求

 看日志:

没有拼接分页查询的动作

 那么什么原因的

1.查看官网文档

网管地址:

MyBatis-Plus

官网有些水,没看到具体信息

2.查看案例

github地址

GitHub - baomidou/mybatis-plus-samples: MyBatis-Plus Samples

在案例中找到了错误原因:

需要配置监听器

 按照下面的方式添加就行

@Configuration
@MapperScan("com.baomidou.mybatisplus.samples.deluxe.mapper")
public class MybatisPlusConfig {

    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor());
        interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
        return interceptor;
    }

    /**
     * 自定义 SqlInjector
     * 里面包含自定义的全局方法
     */
    @Bean
    public MyLogicSqlInjector myLogicSqlInjector() {
        return new MyLogicSqlInjector();
    }
}

  • 5
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
你好!关于 MyBatis Plus 分页查询不生效问题,可能有几个原因导致。以下是一些常见的解决方法: 1. 确保你的实体类中使用了正确的注解。在实体类上,你需要使用 `@TableName` 注解来指定数据库表名,使用 `@TableId` 注解来指定主键字段。 2. 确保你的 Mapper 接口继承了 MyBatis Plus 提供的 `BaseMapper` 接口,并且针对分页查询的方法使用了正确的注解。常用的有 `@SelectPage` 或 `@SelectPage`。 3. 确保你的业务逻辑层或服务层中调用了正确的分页查询方法,并且传入了正确的参数。参数包括要查询的页码、每页显示的记录数等。 4. 如果你使用了 MyBatis Plus 的自动分页功能,需要在配置文件中开启自动分页功能。例如,在 Spring Boot 项目中的配置文件中添加以下配置: ``` mybatis-plus: configuration: use-generated-keys: true mapper-locations: classpath*:mapper/**/*.xml map-underscore-to-camel-case: true auto-mapping-behavior: partial auto-mapper-locations: classpath*:mapper/**/*.xml global-config: db-config: id-type: auto field-strategy: not_empty id-type: auto table-prefix: mp_ logic-delete-field: deleted logic-delete-value: 1 logic-not-delete-value: 0 sql-injector: com.baomidou.mybatisplus.mapper.LogicSqlInjector ``` 5. 如果以上方法都没有解决问题,可能是因为你的分页查询条件不正确。请检查你的查询条件是否正确,并且对应的数据库中是否有符合条件的数据。 希望以上解决方法能够帮助到你,如果问题还未解决,请提供更多的具体信息,我将尽力协助你解决问题
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值