本文代码所用框架为 springboot2+mybatis+postgresql/mysql 多数据源结构。需要动态切换数据源;同时pagehelper也需要动态切换数据库分页方式
一、pom.xml 引入 maven依赖
<!-- pagehelper 分页插件 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.4.0</version>
</dependency>
二、 application.properties 或yml中配置 helper 分页自适应
# PageHelper分页插件
pagehelper:
#helperDialect: mysql
# 自动选择合适的分页方式
auto-dialect: true
# 自动识别对应方言的分页
auto-runtime-dialect: true
supportMethodsArguments: true
params: count=countSql
三、验证
1. postgresql 时分页sql如下
select * from xxtable where 1=1 OFFSET ? LIMIT ?
2. mysql 时分页sql 如下
select * from xxtable where 1=1 LIMIT ?, ?