关于使用mybatis自带的page(E page, Wrapper<T> queryWrapper)接口做分页查询时,分页参数不生效,直接查询库表的全部数据问题

记录一下昨天遇到了一个问题,以免下次再遇到相同问题忘记怎么解决的。

问题描述:微服务框架中其中一个服务使用mybatis自带的page(E page, Wrapper<T> queryWrapper)接口做分页查询时,分页参数不生效,直接查询库表的全部数据问题,而其他服务却没有这个问题

害得我找了一天的问题,一开始就觉得时mybatis配置的问题,因为代码都是共用的不会有问题,把所有mybatis的配置文件的对比了一遍,甚至连jar包都对比了一遍发现都一样的,实在找不到是那里的问题

最后第二天想起来了配置类,果然问题就出在这里,出问题的这个服务确实没有mybatis的配置类,加上之后就分页查询就没问题了

先来看一下具体的问题如下

image

 

image

 

再来看看其他没有问题的服务返回的数据的打印的日志

image

 

image

 

最后解决方式如下:

image

 代码在这里

package ****.config.mybatis; //你自己的包名

import cn.***.common.util.IDUtil; //这里id生成工具类,下面要用到
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.autoconfigure.MybatisPlusPropertiesCustomizer;
import com.baomidou.mybatisplus.core.incrementer.IdentifierGenerator;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.DataChangeRecorderInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Description TODO
 * @Date ****
 * @Created ****
 */
@Configuration
@MapperScan("*****.mapper") //注意:这里是你自己的mapper路径,一定不能写错
public class MybatisPlusConfig {
    /**
     * 添加分页插件
     */
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        /**
         * 配置乐观锁插件
         */
        interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
        /**
         * 数据变动记录插件
         */
        DataChangeRecorderInnerInterceptor dataChangeRecorderInnerInterceptor = new DataChangeRecorderInnerInterceptor();
        // 配置安全阈值,限制批量更新或插入的记录数不超过 1000 条
        dataChangeRecorderInnerInterceptor.setBatchUpdateLimit(100);
        // 记录日志
        interceptor.addInnerInterceptor(dataChangeRecorderInnerInterceptor);
        /**
         * 非法SQL拦截插件
         */
      //  IllegalSQLInnerInterceptor illegalSQLInnerInterceptor = new IllegalSQLInnerInterceptor();
      //  interceptor.addInnerInterceptor(illegalSQLInnerInterceptor);
        /**
         * //如果配置多个插件,切记分页最后添加
         */
        PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor();
        // 每页最大限制为 100 条
        paginationInnerInterceptor.setMaxLimit(100L); //
        paginationInnerInterceptor.setDbType(DbType.MYSQL);
        interceptor.addInnerInterceptor(paginationInnerInterceptor);


        return interceptor;
    }

    /**
     * 添加自定义主键生成策略
     * @return
     */
    @Bean
    public MybatisPlusPropertiesCustomizer plusPropertiesCustomizer() {
        return plusProperties -> plusProperties.getGlobalConfig().setIdentifierGenerator(new IdentifierGenerator() {
            @Override
            public Number nextId(Object entity) {
                return 1212L;
            }

            @Override
            public String nextUUID(Object entity) {
                return IDUtil.INSTANCE.genId(); //这里返回生成的主键id,用你们自己的id生成规则
            }
        });
    }

}

这里是我用的mybatsi的jar包版本

     <!--  springboot整合 mybatis-plus     -->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-spring-boot3-starter</artifactId>
            <version>3.5.6</version>
     </dependency>
    <dependency>
       <groupId>org.mybatis</groupId>
       <artifactId>mybatis-spring</artifactId>
       <version>3.03</version>
     </dependency>

 我的mybatis.yaml配置,就是这么简单

#mybatis-plus配置控制台打印完整带参数SQL语句
mybatis-plus:
  configuration:
    # 是否开启自动驼峰命名规则映射:从数据库列名到Java属性驼峰命名的类似映射
    map-underscore-to-camel-case: true
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
  #mybatis-plus 自动忽略逻辑删除条件
  mapper-locations: classpath:/mapper/**/*.xml
  global-config:
    db-config:
      id-type: ASSIGN_UUID
      logic-delete-field: del  # 全局逻辑删除的实体字段名(since 3.3.0,配置后可以忽略不配置步骤2)
      logic-delete-value: 1        # 逻辑已删除值(默认为 1)
      logic-not-delete-value: 0    # 逻辑未删除值(默认为 0)

 

posted @ 2025-08-15 15:35  llili  阅读(115)  评论(0)    收藏  举报