mybatis-plus pageSize 最大值

mybatis-plus 处于性能和安全的考虑,默认分页查询的单页最大数量为500,也可以根据需要进行调整,如:

@Configuration
public class MybatisPlusAutoConfigure {

    /**
     * 单页分页条数限制(默认无限制,参见 插件#handlerLimit 方法)
     */
    private static final Long MAX_LIMIT = 1000L;

    /**
     * 新的分页插件,一缓和二缓遵循mybatis的规则,
     * 需要设置 MybatisConfiguration#useDeprecatedExecutor = false
     * 避免缓存出现问题(该属性会在旧插件移除后一同移除)
     */
    @Bean
    public MybatisPlusInterceptor paginationInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();

        //分页插件: PaginationInnerInterceptor
        PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor();
        paginationInnerInterceptor.setMaxLimit(MAX_LIMIT);

        //防止全表更新与删除插件: BlockAttackInnerInterceptor
        BlockAttackInnerInterceptor blockAttackInnerInterceptor = new BlockAttackInnerInterceptor();
        interceptor.addInnerInterceptor(paginationInnerInterceptor);
        interceptor.addInnerInterceptor(blockAttackInnerInterceptor);

        return interceptor;
    }
}

但是有时候我们需要突破这个的限制,只需要设置limit的值小于等于0就可了。

虽然是可以解决分页查询突破单页最大500条记录的问题,但是这个是全局的配置,会导致所有的分页都可以突破这个限制,有可能会引发安全或者性能隐患。所以在这里我列举了两种方式:

Mybatis Plus 3.4版本

如果你的服务中使用的是 mybatis plus 3.4版本之后想要突破查询最大记录数的限制非常方便,只需要在Page信息中设置maxLimit的值就可了:

Page<UserEntity> page = new Page<>(pageNum, 1000);
page.setMaxLimit(1000L);

在源码中查看到maxLimit的作用,默认使用Page信息中的maxLimit的值来作为size处理超出分页条数限制,默认归为限制数:

protected void handlerLimit(IPage<?> page) {
    long size = page.getSize();
    Long pageMaxLimit = page.maxLimit();
    Long limit = pageMaxLimit != null ? pageMaxLimit : this.maxLimit;
    if (limit != null && limit > 0L && size > limit) {
        page.setSize(limit);
    }
}

Mybatis Plus 3.3之前版本

如果服务中使用的是 mybatis plus3.3 及之前版本突破限制,需要对 PaginationInterceptor 类中的 handlerLimit 方法做修改就可以。

protected void handlerLimit(IPage<?> page) {
    page.setSize(this.limit);
}

为了使单页最大500的限制默认生效,还要支持突破限制查询,我重写了Page和PaginationInterceptor 来解决问题。

ExtPage

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

public class ExtPage<T> extends Page<T> {

    /**
     * 用来判断是否允许超过pageSize max值=500的限制
     */
    private boolean isBreachDefaultMaxLimit = false;

    public ExtPage() {
    }

    public ExtPage(boolean isBreachDefaultMaxLimit, long current, long size) {
        if (current > 1) {
            this.setCurrent(current);
        }
        this.setSize(size);
        this.setTotal(0);
        this.setSearchCount(true);
        this.isBreachDefaultMaxLimit = isBreachDefaultMaxLimit;
    }

    public boolean isBreachDefaultMaxLimit() {
        return isBreachDefaultMaxLimit;
    }

    public void setBreachDefaultMaxLimit(boolean breachDefaultMaxLimit) {
        isBreachDefaultMaxLimit = breachDefaultMaxLimit;
    }
}

ExtPaginationInterceptor

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import org.apache.ibatis.executor.statement.StatementHandler;
import org.apache.ibatis.plugin.Intercepts;
import org.apache.ibatis.plugin.Signature;

import java.sql.Connection;

@Intercepts({@Signature(
        type = StatementHandler.class,
        method = "prepare",
        args = {Connection.class, Integer.class}
)})
public class ExtPaginationInterceptor extends PaginationInterceptor {

    @Override
    protected void handlerLimit(IPage<?> page) {
        if (page instanceof ExtPage) {
            if (((ExtPage<?>) page).isBreachDefaultMaxLimit()) {
                return;
            }
        }
        super.handlerLimit(page);
    }
}

MybatisPlusAutoConfigure

使用自定义分页 ExtPage 进行查询,当需要突破单页最大500记录限制时 isBreachDefaultMaxLimit 设为true 

@Override
public Page<MsgContentEntity> pageTagByStatus(Integer status, Integer limit, Integer page) {

    ExtPage<MsgContentEntity> extPage = new ExtPage<>(true, page, limit);
    return msgContentMapper.selectTagsPage(extPage, status);
}

转载自:https://www.csdn.net/tags/NtzaggwsMDc2MzYtYmxvZwO0O0OO0O0O.html

1. 首先在pom.xml中添加Mybatis-Plus的依赖: ``` <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.4.2</version> </dependency> ``` 2. 在application.yml中配置Mybatis-Plus的分页插件: ``` mybatis-plus: global-config: db-config: id-type: auto # 主键策略 table-prefix: mp_ # 表前缀 logic-delete-value: 1 #逻辑删除值 logic-not-delete-value: 0 #逻辑未删除值 sql-injector: com.baomidou.mybatisplus.core.injector.LogicSqlInjector # 逻辑删除插件 # 分页插件配置 page-params: # 分页参数 limit: 10 # 每页显示条数,默认 10 max-limit: 1000 # 最大限制数,默认 500 ``` 3. 在Mapper接口中继承Mybatis-Plus的BaseMapper接口,并使用@Mapper注解标注该接口: ``` @Mapper public interface UserMapper extends BaseMapper<User> { } ``` 4. 在Service层的方法中,使用Page对象进行分页查询: ``` @Service public class UserServiceImpl implements UserService { @Autowired private UserMapper userMapper; @Override public Page<User> getUserPage(int pageNum, int pageSize) { Page<User> page = new Page<>(pageNum, pageSize); QueryWrapper<User> wrapper = new QueryWrapper<>(); wrapper.orderByDesc("id"); userMapper.selectPage(page, wrapper); return page; } } ``` 其中,Page的构造方法需要传入当前页码和每页显示的记录数,selectPage方法需要传入Page对象和查询条件的Wrapper对象。 5. 最后,在Controller层中,调用Service层的方法并将分页结果返回给前端: ``` @RestController public class UserController { @Autowired private UserService userService; @GetMapping("/users") public Page<User> getUsers(@RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "10") int pageSize) { return userService.getUserPage(pageNum, pageSize); } } ``` 其中,@RequestParam注解用于接收前端传来的参数,如果没有传入参数,则使用默认值。返回的结果会被自动转化为JSON格式。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值