天天看點

SqlHelper釋出——比你期望的還要多的多(例如比MyBatis-Pagehelper性能更高)

SqlHelper釋出——比Mybatis-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 mybatis-pagehelper sqlhelper
databases 13 90+
multiple databases in runtime
auto detect dialect
plugin
PrepareStatement with '?' X
mybatis 3.x
spring boot 1.x, 2.x
JDK 1.6+
jFinal
EBean
Mango
國産資料庫 √ (參見上述清單)

安裝

可以在多種場景下使用,支援MyBatis,JFinal,EBean等。先就說說MyBatis下如何使用:

1)  與Mybatis + SpringBoot結合使用

此應用環境下,隻需導入下列包即可:

<dependency>
        <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.jn.sqlhelper.mybatis.plugins.pagination.CustomScriptLanguageDriver" />
            ...
        </settings>
        ...
    </configuration>
    
    <plugins>
      <plugin interceptor="com.jn.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

 更高版本、更簡易的用法,可以去GitHub (wiki,  turtoial, 支援QQ群)

作者: 房繼諾

出處:http://www.cnblogs.com/f1194361820

版權:本文版權歸作者和部落格園共有

歡迎轉載,轉載請需要注明部落格出處

技術交流QQ:1194361820,加好友請注明:來自部落格園,不要說你是部落格園,也可以掃描圖像二維碼直接加我。

繼續閱讀