如果你也在用Mybatis,建议尝试该分页插件,这个一定是最方便使用的分页插件。
该插件目前支持Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL六种数据库分页。
我在做项目时在Mybatis配置xml中配置拦截器插件如下:
<plugins><plugin interceptor&#61;"com.github.pagehelper.PageHelper"> <property name&#61;"dialect" value&#61;"mysql"/>plugin>
plugins>
运行时却出现错误&#xff0c;报错&#xff1a;
java.lang.ClassCastException: com.github.pagehelper.PageHelper cannot be cast to org.apache.ibatis.plugin.Interceptor
pageHelper是如何在mybatis中工作呢&#xff0c;是通过mybatis的pulgin实现了Interceptor接口&#xff0c;从而获得要执行的sql语句实现分页技术&#xff0c;而我们的PageHelper5.1.2版本中的这个类&#xff0c;并没有出现实现拦截器
然而我们的PageHelper类中&#xff0c;并没有拦截器&#xff1a;
public class PageHelper extends PageMethod implements Dialect {private PageParams pageParams;private PageAutoDialect autoDialect;public PageHelper() {}......
}
因此&#xff0c;我们需要修改我们的mybatis全局配置文件SqlMapConfig.xml如下&#xff1a;
<configuration> <plugins> <plugin interceptor&#61;"com.github.pagehelper.PageInterceptor"> <property name&#61;"dialect" value&#61;"mysql"/>plugin>plugins>
configuration>
再次运行时出现错误&#xff1a;
Cause: com.github.pagehelper.PageException: java.lang.ClassNotFoundException: mysql
这是因为在pagehelper插件4.0.0以后的版本支持自动识别使用的数据库&#xff0c;可以不用配置数据库方言
<plugins><plugin interceptor&#61;"com.github.pagehelper.PageInterceptor">plugin>
plugins>
这次运行时就能够成功。