使用mybatis-plus 分页插件 PostgreSQL

news/2024/7/9 21:20:52 标签: postgresql, java, 数据库, 分页查询, mybatis

使用mybatis-plus 分页插件 PostgreSQL

  • 前言
  • 环境
  • 一、使用步骤
    • 1.引入库
    • 2.mybatis-plus配置类
    • 使用分页
      • 分页类构造函数
      • 使用页索引
      • 使用元素偏移量
    • mybatis-plus分页实现内部
    • Postgre 数据库分页语句组装实现
  • 文章导读


前言

mybatis-plus的查询默认提供了分页类和方法,但是需要引入三方依赖和载入配置类来启用。
老版mybatis-plus只要引入pagehelper依赖即可,但是新版springboot和mybatis-plus需要再引入jsqlparser来解决依赖冲突会产生的两个问题:

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


环境

组件版本
JDK11
springboot2.5.2
mybatis-plus-boot-starter3.4.2

以下是本篇文章正文内容

一、使用步骤

1.引入库

代码如下(示例):

        <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>

mybatisplus_46">2.mybatis-plus配置类

代码如下(示例):

java">@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;
    }
}

使用分页

分页查询接口的时候方面有两类:
一是使用页索引来翻页;
二是使用记录偏移量来翻页;
下面对两种使用方式各做了示例。

分页类构造函数

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

使用页索引

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

使用元素偏移量

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

mybatisplus_103">mybatis-plus分页实现内部

分页实现是使用分页拦截器PaginationInnerInterceptor,拦截IPage参数,然后组装limit sql来实现的,因为各个数据库limit语法不同,所以sql组装类也是针对各个数据库有多个。

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

Postgre 数据库分页语句组装实现

java">/**
 * 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);
        }
    }
}

可以看出PostgreSql的limit组装是通过页索引算出偏移量然后组装的SQL。


文章导读

Spring动态数据源:Mybatis-plus、C3P0
mysql pgsql 多行记录转JSON数组字段 行转json列
mysql pgsql 实现多行记录合并成一行 分组合并 用指定字符做分割


http://www.niftyadmin.cn/n/1307379.html

相关文章

springboot-admin 监控

项目里面用着的完整的监控 直接上干货 创建监控项目admin pom.xml <parent><groupId>com.wkxhotel</groupId><artifactId>wkxhotel-parent</artifactId><version>1.0</version><relativePath>../pom.xml</relativePath>…

Unicode编码标准以及UTF

编码标准 ASCII 美国信息交换标准代码 American Standard Code for Information Interchange 显示现代英语和其他西欧语言 128个字符 GB2312-80 1980 年制定的中国汉字编码国家标准。共收录 7445 个字符&#xff0c;其中汉字 6763 个。GB2312 兼容标准 ASCII码&#xff0c;…

java8 stream api (三)

高级复杂的流操作 private static List<Person> list Arrays.asList(new Person("aa", 12), new Person("bb", 13), new Person("bb", 14),new Person("bb", 15), new Person("bb", 16));public static void main(S…

设置ssh登录Linux服务器和git上ssh的使用

设置ssh登录Linux服务器前言环境配置客户端服务器端可能碰到的问题使用密钥命令登录依然提示输入密码&#xff1f;&#xff1f;git如何使用ssh-key前言 我们在远程Linux服务器或者使用git的时候都需要输入密码登录后进行操作&#xff0c;在进行频繁操作的时候每次都要输入密码…

多线程编程 java.util.concurrent包下类

concurrentHashMap 使用的是分段式锁(分段式锁就是Segment)在迭代的过程中&#xff0c;ConcurrentHashMap仅仅锁定map的某个部分&#xff0c;而Hashtable则会锁定整个map。HashTable的迭代器是强一致性的&#xff0c;而ConcurrentHashMap是弱一致的。 ConcurrentHashMap的get&a…

线程池参数设计技巧

ThreadPoolExecutor线程池参数设置技巧 一、ThreadPoolExecutor的重要参数 corePoolSize&#xff1a;核心线程数 核心线程会一直存活&#xff0c;及时没有任务需要执行 当线程数小于核心线程数时&#xff0c;即使有线程空闲&#xff0c;线程池也会优先创建新线程处理 设置all…

C++与正则表达式

提示&#xff1a;文章写完后&#xff0c;目录可以自动生成&#xff0c;如何生成可参考右边的帮助文档 目录 ​​​​​​正则表达式 特性 元字符 分组&#xff08;子模式&#xff09; C regex 以R开头的字符串字面值 代码示例 相关文章导读 Unicode编码标准以及UTF&#xff08;…

springBoot 并行任务 @Async配置和使用

Springboot 提供了一个异步执行方法的注解Asyn,添加了这个注解的方法执行的时候&#xff0c;会在维护好的线程池中选取一个线程异步的去执行这个方法。下面看一下它的配置和使用方法。 启动类上添加注解 EnableAsync EnableAsync SpringBootApplication MapperScan({"com…