[SpringBoot+MyBatis-plus] Background paging operation

Here is a record of how to use SpringBoot + Mybatis-plus to perform paging operations in IDEA

Please refer to the official document Click to enter

1 Make sure that the Mybatis-plus dependency has been introduced into the project

      <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-generator</artifactId>
            <!-- <version>3.4.0</version> -->
        </dependency>

2 Related codes

Let's first look at the project structure, that is, two submodules are included in a SpringBoot project,
insert image description here
and the code is inserted in the following files. Since paging may be a common module in multiple subtasks, the configuration file is created in the common subproject.
insert image description here
Here, SpringBoot is directly used. way of doing

//Spring boot方式
@Configuration
//如果启动类中已经配置了 MapperScan 则在这里可以省略,如果没有配置,则需要添加此句
//里面内容为 *mapper.java 所在的文件夹
@MapperScan("com.baomidou.cloud.service.*.mapper*")
public class MybatisPlusConfig {
    
    

    // 旧版
    @Bean
    public PaginationInterceptor paginationInterceptor() {
    
    
        PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
        // 设置请求的页面大于最大页后操作, true调回到首页,false 继续请求  默认false
        // paginationInterceptor.setOverflow(false);
        // 设置最大单页限制数量,默认 500 条,-1 不受限制
        // paginationInterceptor.setLimit(500);
        // 开启 count 的 join 优化,只针对部分 left join
        paginationInterceptor.setCountSqlParser(new JsqlParserCountOptimize(true));
        return paginationInterceptor;
    }

    // 最新版
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
    
    
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.H2));
        return interceptor;
    }

}

As shown in the figure below, this article has been configured, so the above MapperScanner can be removed
insert image description here

test

Indicates that the query is successful
** bold style
**
The front-end page test passed
insert image description here

Guess you like

Origin blog.csdn.net/qq_29750461/article/details/122523702