PageHelper支援GreenPlum
- 2020 年 6 月 17 日
- 筆記
- greenplum, pagehelper, springboot
greenplum是pivotal在postgresql的基礎上修改的一個資料庫,語法和postgresql通用。使用PageHelper做分頁插件的時候,發現目前沒有針對greenplum做支援,但是對postgresql做了支援,因為只是分頁的時候用到,所以只需要支援分頁的語法即可。
PageHelper的github、整合Spring Boot、greenplum
依賴:
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.3</version>
</dependency>
1、配置文件
pagehelper:
helperDialect: mysql # 默認的
autoRuntimeDialect: true # 必填
reasonable: true
supportMethodsArguments: true
params: count=countSql
auto-dialect: true # 必填
close-conn: false
offset-as-page-num: true
2、程式碼
com.github.pagehelper.page.PageAutoDialect
中配置了各個方言。
static {
//註冊別名
dialectAliasMap.put("hsqldb", HsqldbDialect.class);
dialectAliasMap.put("h2", HsqldbDialect.class);
dialectAliasMap.put("postgresql", HsqldbDialect.class);
dialectAliasMap.put("phoenix", HsqldbDialect.class);
dialectAliasMap.put("mysql", MySqlDialect.class);
dialectAliasMap.put("mariadb", MySqlDialect.class);
dialectAliasMap.put("sqlite", MySqlDialect.class);
dialectAliasMap.put("oracle", OracleDialect.class);
dialectAliasMap.put("db2", Db2Dialect.class);
dialectAliasMap.put("informix", InformixDialect.class);
dialectAliasMap.put("sqlserver", SqlServerDialect.class);
dialectAliasMap.put("sqlserver2012", SqlServer2012Dialect.class);
dialectAliasMap.put("derby", SqlServer2012Dialect.class);
}
發現其實是使用jdbc的url的連接的jdbc:<資料庫>://<IP>:<PODT>/<DB>
中的<資料庫>
來作為key的。
postgresql的url格式:
jdbc:postgresql://localhost:5432/postgres
greenplum的jdbc配置連接格式為:
jdbc:pivotal:greenplum://localhost:5432;DatabaseName=test
我們需要將pivotal
作為key,HsqldbDialect.class
作為value添加到com.github.pagehelper.page.PageAutoDialect
的dialectAliasMap
中即可。
PageHelper
是從jdbcUrl
獲得數據源的方言的,獲取之後會快取到com.github.pagehelper.page.PageAutoDialect#urlDialectMap
中:
private String fromJdbcUrl(String jdbcUrl) {
for (String dialect : dialectAliasMap.keySet()) {
if (jdbcUrl.indexOf(":" + dialect + ":") != -1) {
return dialect;
}
}
return null;
}
配置類PageHelperConfiguration
實現:
import com.github.pagehelper.autoconfigure.PageHelperAutoConfiguration;
import com.github.pagehelper.dialect.helper.HsqldbDialect;
import com.github.pagehelper.page.PageAutoDialect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.context.annotation.Configuration;
import javax.annotation.PostConstruct;
import java.lang.reflect.Field;
import java.util.Map;
@AutoConfigureAfter(PageHelperAutoConfiguration.class)
@Configuration
public class PageHelperConfiguration {
final static Logger logger = LoggerFactory.getLogger(PageHelperConfiguration.class);
@PostConstruct
public void init() throws Exception {
try{
PageAutoDialect pageAutoDialect = new PageAutoDialect();
Field dialectAliasMap = pageAutoDialect.getClass().getDeclaredField("dialectAliasMap");
dialectAliasMap.setAccessible(true);
Map<String, Class<?>> dialectAliasMapValue = (Map<String, Class<?>>)dialectAliasMap.get(pageAutoDialect);
dialectAliasMapValue.put("pivotal", HsqldbDialect.class);
}catch (Exception e) {
logger.error("修改 PageAutoDialect 出錯:{}", e.getMessage());
throw e;
}
}
}