SqlHelper發布—比PageHelper性能更高
起源
前段時間開啟了一個新的項目,在選擇分頁插件時,發現github上很流行的一個是pagehelper,在百度上搜索了一下,使用量。由於項目緊急,所先拿來用了。但是我知道它並不適合我們。原因是它有如下幾個缺點:
1) 對國產數據庫支持不足
2) 擴展不方便
3) 配置復雜
4) 性能底下 (不要噴我, 因為它不是用的占位符?,發揮不了PrepareSatement的優勢)
5) 只支持MyBatis
鑒於它的這些不足,我就趁閑暇時間新開發了一款解決上述缺點的分頁工具,它已經在公司里的兩個項目得到了驗證。但它不僅僅是個分頁工具那么簡單,目前支持的特性有Pagination、UrlParser,未來會支持更多特性。
關鍵特性
- 支持MyBatis, JFinal,Ebean,Mango
- 支持 90+ 種數據庫, 支持列表參見 here. 包含了幾乎所有的國產數據庫:
- TiDB (北京平凱星辰科技))
- Doris (Apache Doris,百度研發)
- MaxCompute (阿里巴巴)
- K-DB (浪潮)
- GBase (南大通用)
- DM (達夢)
- OSCAR (神州通用)
- HighGo (瀚高)
- KingBase (金倉)
- OpenBase (東軟)
- SequoiaDB (巨杉)
如果你想知道所有的數據庫排名的話,你可以在這里找到: DB Engines.
- 同一個應用中支持多種數據庫
- 不需要配置dialect,可以自動的獲取。
- 比 Mybatis-PageHelper性能更高, 原因是limit , offset等參數使用 PrepareStatement placeholder '?' , Mybatis是硬編碼拼接的
- 通過Java SPI的方式支持了插件
- 支持 spring boot 1.x , 2.x
- 支持 mybatis 3.x
- 支持 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>