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

SqlHelper發佈—比PageHelper性能更高

起源

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

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

2) 擴展不方便

3) 配置複雜

4) 性能底下 (不要噴我, 因爲它不是用的佔位符?,發揮不了PrepareSatement的優勢)

5) 只支持MyBatis

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

關鍵特性

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

如果你想知道所有的數據庫排名的話,你可以在這裏找到: DB Engines.

  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下如何使用:

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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章