mybatis分頁插件pagehelper 5.1.2遇到的問題
阿新 • • 發佈:2018-04-03
acl 技術分享 maria fig 配置文件 自動識別 ava batis sqlit
如果你也在用Mybatis,建議嘗試該分頁插件,這個一定是最方便使用的分頁插件。
該插件目前支持Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL六種數據庫分頁。
我在做項目時在Mybatis配置xml中配置攔截器插件如下:
<plugins> <!-- com.github.pagehelper為PageHelper類所在包名 --> <plugin interceptor="com.github.pagehelper.PageHelper"> <!-- 設置數據庫類型 Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL六種數據庫--> <property name="dialect" value="mysql"/> </plugin> </plugins>
運行時卻出現錯誤,報錯java.lang.ClassCastException: com.github.pagehelper.PageHelper cannot be cast to org.apache.ibatis.plugin.Interceptor
pageHelper是如何在mybatis中工作呢,是通過mybatis的pulgin實現了Interceptor接口,從而獲得要執行的sql語句實現分頁技術,而我們的PageHelper5.1.2版本中的這個類,並沒有出現implements Interceptor
然而我們的PageHelper類中,並沒有攔截器
因此,我們需要修改我們的mybatis全局配置文件SqlMapConfig.xml如下:
<configuration> <!-- 配置分頁插件 PageHelper --> <plugins> <plugin interceptor="com.github.pagehelper.PageInterceptor">
<!-- 設置數據庫類型 Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL六種數據庫--> <property name="dialect" value="mysql"/
</plugin> </plugins> </configuration>
再次運行時出現錯誤Cause: com.github.pagehelper.PageException: java.lang.ClassNotFoundException: mysql
這是因為在pagehelper插件4.0.0以後的版本支持自動識別使用的數據庫,可以不用配置 <property name="dialect" value="mysql"/>
再次更改配置文件
<plugins> <plugin interceptor="com.github.pagehelper.PageInterceptor"> <!-- 設置數據庫類型 Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL六種數據庫--> </plugin> </plugins>
這次運行時就能夠成功
mybatis分頁插件pagehelper 5.1.2遇到的問題