mybatis-plus pageHelper 合理化配置

ジ绯色月下ぎ發表於2024-05-13

mybatis 分頁設定

// 自定義分頁攔截器
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;

public class CustomPaginationInterceptor extends PaginationInnerInterceptor {
    @Override
    protected void handlerOverflow(IPage<?> page) {
        //原來的邏輯是超出範圍返回第一頁
        //page.setCurrent(1L);

        //修改成返回最後一頁
        page.setCurrent(page.getPages());
    }
}


// 註冊攔截器
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


@Configuration
public class MyBatisPlusConfig {


    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor(DbType.KINGBASE_ES);
        // 設定最大單頁限制數量,預設 500 條,-1 不受限制
        paginationInnerInterceptor.setMaxLimit(-1L);
        // 分頁合理化
        paginationInnerInterceptor.setOverflow(true);
        interceptor.addInnerInterceptor(paginationInnerInterceptor);
        return interceptor;
    }

}

相關文章