zoukankan      html  css  js  c++  java
  • PageHelper支持GreenPlum

    greenplum是pivotal在postgresql的基础上修改的一个数据库,语法和postgresql通用。使用PageHelper做分页插件的时候,发现目前没有针对greenplum做支持,但是对postgresql做了支持,因为只是分页的时候用到,所以只需要支持分页的语法即可。

    PageHelper的github整合Spring Bootgreenplum

    依赖:

    <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.PageAutoDialectdialectAliasMap中即可。

    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;
            }
        }
    
    }
    
  • 相关阅读:
    hdu 5352 匈牙利(多重匹配)
    hdu 2112 最短路+stl
    hdu 1811 拓扑排序+并查集+细心
    hdu5407 CRB and Candies
    hdu1018 Big Number
    hdu5410 CRB and His Birthday
    hdu1023 Train Problem II
    hdu4812 D Tree
    hdu1085 Holding Bin-Laden Captive!
    hdu4810 Wall Painting
  • 原文地址:https://www.cnblogs.com/bartggg/p/13152474.html
Copyright © 2011-2022 走看看