PageHelper是一种常用的分页工具,按照常规方法在mybatis的配置文件中整合它:

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE configuration
  3. PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-config.dtd">
  5.  
  6. <configuration>
  7.  
  8. <!--全局环境参数-->
  9. <settings>
  10. ...
  11. </settings>
  12.  
  13. <!--插件配置-->
  14. <plugins>
  15. <!--支持PageHelper插件-->
  16. <plugin interceptor="com.github.pagehelper.PageHelper">
  17. ...
  18. </plugin>
  19. </plugins>
  20.  
  21. </configuration>

在启动服务的时候,却抛出如下异常:

Cause: java.lang.ClassCastException: com.github.pagehelper.PageHelper cannot be cast to org.apache.ibatis.plugin.Interceptor

看字面意思是类型转换异常,PageHelper不能转换成org.apache.ibatis.plugin.Interceptor,于是点进PageHelper的源码

  1. /**
  2. * Mybatis - 通用分页拦截器<br/>
  3. * 项目地址 : http://git.oschina.net/free/Mybatis_PageHelper
  4. *
  5. * @author liuzh/abel533/isea533
  6. * @version 5.0.0
  7. */
  8. public class PageHelper extends PageMethod implements Dialect {
  9. ...
  10. }

确实没有实现Interceptor接口,有丶谜,因为以前的项目也是这么配置的,为什么没出这种问题?

找到老项目点进PageHelper源码:

  1. /**
  2. * Mybatis - 通用分页拦截器
  3. *
  4. * @author liuzh/abel533/isea533
  5. * @version 3.3.0
  6. * 项目地址 : http://git.oschina.net/free/Mybatis_PageHelper
  7. */
  8. @SuppressWarnings("rawtypes")
  9. @Intercepts(@Signature(type = Executor.class, method = "query", args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class}))
  10. public class PageHelper implements Interceptor {
  11. ...
  12. }

原因找到了:

PageHelper3、4版本实现了Interceptor接口,PageHelper5不再实现。

那么问题来了,如果使用PageHelper5版本,怎么在mybatis中继承这个插件?从PageHelper所在的com.github.pagehelper包中,发现了一个叫PageInterceptor的类,名字有丶像我们要找的东西,点进源码一看,果然实现了Interceptor接口。

于是我们尝试将这个类放入mybatis的配置文件中,删除不存在的属性,启动服务正常。

结论:使用PageHelper版本3、4时,在mybatis配置文件中用PageHelper类集成,使用PageHelper版本5时,在mybatis配置文件中使用PageInterceptor类集成。

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE configuration
  3. PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-config.dtd">
  5.  
  6. <configuration>
  7.  
  8. <!--全局环境参数-->
  9. <settings>
  10. ...
  11. </settings>
  12.  
  13. <!--插件配置-->
  14. <plugins>
  15. <!--支持PageHelper插件-->
  16. <plugin interceptor="com.github.pagehelper.PageInterceptor">
  17. </plugin>
  18. </plugins>
  19.  
  20. </configuration>

在项目中配置PageHelper插件时遇到类型转换异常的更多相关文章

  1. 如何在实际项目中使用PageHelper分页插件

    PageHelper是一个分页插件,能够简单快速的帮助开发人员完成常见的分页功能,你只需要简单的使用两行代码就可以完成一个分页效果- 最近做一个科创项目,使用Maven+SSM的环境,有分页的功能,于 ...

  2. ckeditor编辑器在java项目中配置

    一.基本使用: 1.所需文件架包 A. Ckeditor基本文件包,比如:ckeditor_3.6.2.zip 下载地址:http://ckeditor.com/download 2.配置使用 A.将 ...

  3. eclipse maven项目中使用tomcat插件部署项目

    maven的tomcat插件部署web项目,我简单认为分两种,一种是部署到内置tomcat,另一种是部署到安装的tomcat. 第一种部署,默认是部署在内置tomcat的8080端口,如果不需要改端口 ...

  4. 项目中整合第三方插件与SpringMVC数据格式化关于ip地址

    一.Bootstrap 响应式按钮 <div calss="col-sm-2"> <button class="btn btn-default btn- ...

  5. 项目中DataTables分页插件的使用

    在项目开发的过程中,一般都会对表格进行分页处理,大多是情况下会在项目中配置好后台分页插件,提高效率,减轻浏览器的压力.但是有时会遇到有些数据不能直接通过分页插件操作数据库进行分页数据查询,那就需要用到 ...

  6. 实战:vue项目中导入swiper插件

    版本选择 swiper是个常用的插件,现在已经迭代到了第四代:swiper4.常用的版本是swiper3和swiper4,我选择的是swiper3. 安装 安装swiper3的最新版本3.4.2: n ...

  7. vue-cli项目中引入第三方插件

    前言 最近有小伙伴问道如何在vue-cli项目中引入第三方插件或者库,例如如果想在项目中使用jQuery中的Ajax请求数据呢?或者我想使用Bootstrap框架呢?等等这些问题,本篇博客将带你学习如 ...

  8. Spring-Boot项目中配置redis注解缓存

    Spring-Boot项目中配置redis注解缓存 在pom中添加redis缓存支持依赖 <dependency> <groupId>org.springframework.b ...

  9. android项目中配置NDK自动编译生成so文件

    1 下载ndk开发包   2 在android 项目中配置编译器(以HelloJni项目为例)  2.1 创建builer  (a)Project->Properties->Builder ...

随机推荐

  1. ssl多人多附件多格式邮件发送

    package com.dfmy.util; import java.io.File; import java.security.Security; import java.util.ArrayLis ...

  2. ios / % 四舍五入 向上取整(ceil()) 向下取整(floor())

    1. / //Test "/"    cout << "Test \"/\"!" << endl;    cout ...

  3. time、date、datetime、timestamp和year

    在此声明mysql数据库 时间上总共有五中表示方法:它们分别是 time.date.datetime.timestamp和year. time : “hh:mm:ss”格式表示的时间值,格式显示TIM ...

  4. 下载bat脚本

    @rem 注释:从ftp服务器每小时下载北向性能文件的脚本 @rem 用vb脚本取昨天 for /f %%a in ('cscript //nologo yester.vbs') do set yes ...

  5. c语言日志打印

    #include <stdio.h> #include <stdlib.h> #include <time.h> #include <stdarg.h> ...

  6. 非root用户随开机而启动mysql服务

    非root用户随开机而启动mysql服务 今天验证了一下,非root用户随开机而启动msyql服务的脚本执行效果,特此简要记录如下: 环境: 192.168.142.130 mysql 5.6.41 ...

  7. Jenkins自动打包并部署(以java -jar形势运行)

    1.打包 与平常maven项目打包一致,不再赘述 2.杀死原有进程 通过 pid=`ps -ef|grep $APP_NAME|grep -v grep|awk '{print $2}' ` 获取当前 ...

  8. java——SimpleDateFormat与DateTimeFormatter

    https://www.jianshu.com/p/b212afa16f1f SimpleDateFormat不是线程安全的 DateTimeFormatter是线程安全的

  9. TypeScript作为前端开发你必须学习的技能(一)

    2019年,TypeScript已经开始渐渐的崭露头角,各大框架都说要使用TypeScript,虽然现在还没有完美,但是TypeScript很有可能会成为下一个主流技术. 废话就不多说了,直接开始吧. ...

  10. .net2.0 Thread 多线程

    序言 第1章  线程基础 System.Threading Join C#中的Thread中的ApartmentState几种状态(STA,MTA,Unknown)详解 System.Threadin ...