SqlHelper發佈—比Pagehelper更好用的分頁插件

SqlHelper發佈—比PageHelper性能更高git

起源

前段時間開啓了一個新的項目,在選擇分頁插件時,發現github上很流行的一個是pagehelper,在百度上搜索了一下,使用量。因爲項目緊急,所先拿來用了。可是我知道它並不適合咱們。緣由是它有以下幾個缺點:github

1) 對國產數據庫支持不足spring

2) 擴展不方便sql

3) 配置複雜數據庫

4) 性能底下 (不要噴我, 由於它不是用的佔位符?,發揮不了PrepareSatement的優點)springboot

5) 只支持MyBatismybatis

鑑於它的這些不足,我就趁閒暇時間新開發了一款解決上述缺點的分頁工具,它已經在公司裏的兩個項目獲得了驗證。但它不單單是個分頁工具那麼簡單,目前支持的特性有Pagination、UrlParser,將來會支持更多特性。oracle

關鍵特性

  1. 支持MyBatis, JFinal,Ebean,Mango
  2. 支持 90+ 種數據庫, 支持列表參見 here. 包含了幾乎全部的國產數據庫:
  • TiDB (北京平凱星辰科技))
  • Doris (Apache Doris,百度研發)
  • MaxCompute (阿里巴巴)
  • K-DB (浪潮)
  • GBase (南大通用)
  • DM (達夢)
  • OSCAR (神州通用)
  • HighGo (瀚高)
  • KingBase (金倉)
  • OpenBase (東軟)
  • SequoiaDB (巨杉)

若是你想知道全部的數據庫排名的話,你能夠在這裏找到: DB Engines.app

  1. 同一個應用中支持多種數據庫
  2. 不須要配置dialect,能夠自動的獲取。
  3. 比 Mybatis-PageHelper性能更高, 緣由是limit , offset等參數使用 PrepareStatement placeholder '?' , Mybatis是硬編碼拼接的
  4. 經過Java SPI的方式支持了插件
  5. 支持 spring boot 1.x , 2.x
  6. 支持 mybatis 3.x
  7. 支持 JDK6+

Vs Pagehelper

metric pagehelper sqlhelper
databases 13 90+
multiple databases in runtime
auto detect dialect
plugin
PrepareStatement with '?' X
mybatis 3.x 3.x
spring boot 1.x, 2.x 1.x, 2.x
JDK 1.6+ 1.6+
jFinal X
EBean X
Mango X
國產數據庫 X √ (參見上述列表)

安裝

能夠在多種場景下使用,支持MyBatis,JFinal,EBean等。先就說說MyBatis下如何使用:ide

1)  與Mybatis + SpringBoot結合使用

此應用環境下,只需導入下列包便可:

<groupId>com.github.fangjinuo.sqlhelper</groupId>
        <artifactId>sqlhelper-mybatis-spring-boot-autoconfigure</artifactId>
        <version>${sqlhelper.version}</version>
    </dependency>
    <dependency>
        <groupId>com.github.fangjinuo.sqlhelper</groupId>
        <artifactId>sqlhelper-mybatis-spring-boot-starter</artifactId>
        <version>${sqlhelper.version}</version>
    </dependency>

2)與MyBatis (無spring boot)結合使用

此應用環境下,使用也不麻煩。

第一步,導入依賴:

<dependency>
        <groupId>com.github.fangjinuo.sqlhelper</groupId>
        <artifactId>sqlhelper-dialect</artifactId>
        <version>${sqlhelper.version}</version>
    </dependency>

第二步:配置插件:

<configuration>
        ...
        <databaseIdProvider type="DB_VENDOR">
          <property name="SQL Server" value="sqlserver"/>
          <property name="DB2" value="db2"/>
          <property name="Oracle" value="oracle" />
        </databaseIdProvider>
        ...
        <settings>
            ...
            <setting name="defaultScriptingLanguage" value="com.github.fangjinuo.sqlhelper.mybatis.plugins.pagination.CustomScriptLanguageDriver" />
            ...
        </settings>
        ...
    </configuration>
    
    <plugins>
      <plugin interceptor="com.github.fangjinuo.sqlhelper.mybatis.plugins.pagination.MybatisPaginationPlugin" />
    </plugins>

使用

@GetMapping
    public PagingResult list(){
        User queryCondtion = new User();
        queryCondtion.setAge(10);
        PagingRequest request = new PagingRequest()
                .setPageNo(1)
                .setPageSize(10);
        PagingRequestContextHolder.getContext().setPagingRequest(request);
        List users = userDao.selectByLimit(queryCondtion);
        request.getResult().setItems(users);
        return request.getResult();
    }

從mybatis-pagehelper遷移

爲了兼容已有的應用,特地提供了從mybatis-pagehelper遷移工具。使用也很簡單,把mybatis-pagehelper.jar移除,導入下面的包便可。

<dependency>
        <groupId>com.github.fangjinuo.sqlhelper</groupId>
        <artifactId>sqlhelper-mybatis-over-pagehelper</artifactId>
        <version>${sqlhelper.version}</version>
    </dependency>

支持

https://github.com/fangjinuo/sqlhelper

相關文章
相關標籤/搜索