Explain the use of paging plug in after MybatisPlus version 3.4 in detail

  • 2021-12-11 07:42:53
  • OfStack

Directory 1. MybatisPlusInterceptor 2. Legacy Paging Plug-in Configuration Method (before Mybatis Plus 3.4. 0) 3. New Paging Plug-in Configuration Method (Mybatis Plus version 3.4. 0 and later) 4. How to use paged queries

1. MybatisPlusInterceptor

Starting with Mybatis Plus version 3.4. 0, instead of using the old version of PaginationInterceptor, use MybatisPlusInterceptor.

MybatisPlusInterceptor is a series of interceptor chains implementing InnerInterceptor, which can also be understood as a collection. The following interceptors can be included

Automatic paging: PaginationInnerInterceptor (most commonly used) Multi-tenant: TenantLineInnerInterceptor Dynamic table name: DynamicTableNameInnerInterceptor Optimistic lock: OptimisticLockerInnerInterceptor sql Performance Specification: IllegalSQLInnerInterceptor Preventing full table updates and deletions: BlockAttackInnerInterceptor

2. Legacy Paging Plug-in Configuration Method (before Mybatis Plus 3.4. 0)


@Configuration
@MapperScan(basePackages = {"com.zimug.**.mapper"})
public class MybatisPlusConfig {

    @Bean
    public PaginationInterceptor paginationInterceptor() {
        PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
        //  Operation after setting the requested page larger than the maximum page,  true Go back to the home page, false  Continue request    Default false
        // paginationInterceptor.setOverflow(false);
        //  Set the maximum single page limit, default  500  Article, -1  Unrestricted 
        // paginationInterceptor.setLimit(500);
        //  Open  count  Adj.  join  Optimization , Partially only  left join
        paginationInterceptor.setCountSqlParser(new JsqlParserCountOptimize(true));
        return paginationInterceptor;
    }
}

3. New Paging Plug-in Configuration Method (Mybatis Plus version 3.4. 0 and later)

New paging plug-in, 1 ease 2 slow follow the rules of mybatis, need to set MybatisConfiguration # useDeprecatedExecutor = false to avoid cache problems


@Configuration
@MapperScan(basePackages = {"com.zimug.**.mapper"})
public class MybatisPlusConfig {

  /**
   *  New paging plug-in ,1 Detente 2 Slow compliance mybatis Rules of , Requires setting  MybatisConfiguration#useDeprecatedExecutor = false  Avoid caching problems ( After the old plug-in is removed, this property will 1 With removal )
   */
  @Bean
  public MybatisPlusInterceptor mybatisPlusInterceptor() {
    MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
    // Toward Mybatis Add a page interceptor to the filter chain 
    interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
    // You can also add i His interceptor 
    return interceptor;
  }

  @Bean
  public ConfigurationCustomizer configurationCustomizer() {
    return configuration -> configuration.setUseDeprecatedExecutor(false);
  }
}

4. How to use paged queries

The use of paged queries remains unchanged from version 1 prior to Mybatis. Here is a simple example


Page<SysUserOrg> page = new Page<> (pageNum,pageSize);   // Enquiry number pageNum Pages, each page pageSize Bar data 
// Paging parameters page As Mybatis Or Mybatis Plus The first part of 1 Parameters are passed into the persistence layer function, and the paging query can be completed 
return mySystemMapper.selectUser(page,  Other parameters  );

This article is reproduced from: Alphabet Brother Blog


Related articles: