Using mybatis-plus paging plug-in PostgreSQL


Preface

The query of mybatis-plus provides paging classes and methods by default, but it needs to introduce third-party dependencies and load configuration classes to enable them.
The old version of mybatis-plus only needs to introduce pagehelper dependencies, but the new versions of springboot and mybatis-plus need to introduce jsqlparser to solve two problems caused by dependency conflicts:

1: Use 分页查询时报错 java.lang.NoSuchMethodError: net.sf.jsqlparser…
2:分页和查total失效


environment

components Version
JDK 11
springboot 2.5.2
mybatis-plus-boot-starter 3.4.2

以下是本篇文章正文内容

1. Usage steps

1. Import the library

The code is as follows (example):

        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot</artifactId>
            <version>1.4.2</version>
            <type>pom</type>
        </dependency>
        <dependency>
            <groupId>com.github.jsqlparser</groupId>
            <artifactId>jsqlparser</artifactId>
            <version>3.1</version>
<!--            <type>bundle</type>-->
        </dependency>

2.mybatis-plus configuration class

The code is as follows (example):

@Configuration
@ConditionalOnClass(value = {
    
    PaginationInterceptor.class})
public class MybatisPagePluginConfig {
    
    

    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
    
    
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor();
        paginationInnerInterceptor.setOptimizeJoin(true);
        paginationInnerInterceptor.setDbType(DbType.POSTGRE_SQL);
        paginationInnerInterceptor.setOverflow(true);
        interceptor.addInnerInterceptor(paginationInnerInterceptor);
        OptimisticLockerInnerInterceptor optimisticLockerInnerInterceptor = new OptimisticLockerInnerInterceptor();
        interceptor.addInnerInterceptor(optimisticLockerInnerInterceptor);
        return interceptor;
    }
}

use pagination

There are two types of paging query interfaces:
one is to use the page index to turn pages;
the other is to use the record offset to turn pages;
the following examples are given for each of the two usage methods.

paging class constructor

    /**
     * 分页构造函数
     *
     * @param current 当前页
     * @param size    每页显示条数
     * @param isSearchCount  是否查询记录总数
     */
     public Page(long current, long size, boolean isSearchCount);

use page index

        IPage<SconlinePredictionOrders> page = new Page<>( queryParams.getOffset() , queryParams.getLimit(), true);

use element offset

        IPage<SconlinePredictionOrders> page = new Page<>( queryParams.getOffset() / queryParams.getLimit() + 1 ,
                queryParams.getLimit(), true);

mybatis-plus paging implementation internal

The paging implementation is realized by using the paging interceptor PaginationInnerInterceptor, intercepting the IPage parameters, and then assembling the limit sql. Because the limit syntax of each database is different, there are multiple sql assembly classes for each database.

/**
 * 分页拦截器
 * <p>
 * 默认对 left join 进行优化,虽然能优化count,但是加上分页的话如果1对多本身结果条数就是不正确的
 *
 * @author hubin
 * @since 3.4.0
 */
@Data
@NoArgsConstructor
@SuppressWarnings({
    
    "rawtypes"})
public class PaginationInnerInterceptor

Postgre database paging statement assembly implementation

/**
 * Postgre 数据库分页语句组装实现
 *
 * @author hubin
 * @since 2016-01-23
 */
public class PostgreDialect implements IDialect {
    
    

    @Override
    public DialectModel buildPaginationSql(String originalSql, long offset, long limit) {
    
    
        StringBuilder sql = new StringBuilder(originalSql).append(" LIMIT ").append(FIRST_MARK);
        if (offset != 0L) {
    
    
            sql.append(" OFFSET ").append(SECOND_MARK);
            return new DialectModel(sql.toString(), limit, offset).setConsumerChain();
        } else {
    
    
            return new DialectModel(sql.toString(), limit).setConsumer(true);
        }
    }
}

It can be seen that PostgreSql's limit assembly is a SQL that calculates the offset through the page index and then assembles it.


Article introduction

Spring dynamic data source: Mybatis-plus, C3P0
mysql pgsql Multi-line records are converted to JSON array field rows to json columns
mysql pgsql realizes the combination of multiple lines of records into one line grouping and merging and splitting with specified characters

Guess you like

Origin blog.csdn.net/wangxudongx/article/details/124541990