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

来源:互联网 发布:知乎,京九高铁 编辑:程序博客网 时间:2024/06/05 16:09

问题背景:

        在某商城的学习过程中用到的分页插件的版本是4.0以下的版本,基本不支持多边查询的分页,学习资料中是经过高手重新编写的,后来查资料发现最新的5.0及以上的版本已经有了解决方案,便直接用了5.0的版本,除了引用这个pagehelper-x.x.x.jar 和其依赖包 jsqlparser-0.9.5.jar,开始的配置就照着原来的配置在SqlMapConfig.xml进行了配置

<?xml version="1.0" encoding="UTF-8" ?>  <!DOCTYPE configuration          PUBLIC "-//mybatis.org//DTD Config 3.0//EN"          "http://mybatis.org/dtd/mybatis-3-config.dtd">  <configuration>    <!-- 配置分页插件 PageHelper -->     <plugins>       <plugin interceptor="com.github.pagehelper.PageHelper">    

<!-- 指定使用的数据库是什么 -->

          <property name="dialect"value="mysql"/> </plugin> </plugins> </configuration>

        开始运行也没有问题,结果第二天来了就报错了

 Exception encountered during context initialization - cancelling refresh attemptorg.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactory' defined in file [D:\eclipse\taotao-manager\
taotao-manager-web\target\classes\spring\applicationContext-dao.xml]: Invocation of init method failed; nested exception is org.springframework.core.
NestedIOException: Failed to parse config resource: class path resource [mybatis/SqlMapConfig.xml]; nested exception is org.apache.ibatis.builder.
BuilderException: Error parsing SQL Mapper Configuration. Cause: com.github.pagehelper.PageException: java.lang.ClassNotFoundException: mysqlat org.
springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1566)

分析

         整合后的mybaties和spring都是通过配置文件联系起来的,如果配置文件有问题一些映射就会出错,也就会导致一些class或者bean无法成功生成或找到,这是我当时的第一想法,所以看到

 com.github.pagehelper.PageException: java.lang.ClassNotFoundException:
       第一想法是SqlMapConfig.xml配置可能有误,核查了一遍依然报错,这时有点蒙了,经过朋友的提醒想是不是版本的问题呢,一搜看到了这篇博客,这篇博客对5.0.2版本的介绍,配置以及使用做了很详细的介绍,发现他用的配置文件的类和我的不一样,他用的是PageInterceptor,我用的是pagehelper。

 <bean class="com.github.pagehelper.PageInterceptor">                    <!--这里的几个配置主要演示如何使用,如果不理解,一定要去掉下面的配置helperDialect:有别于3.0+版本,现在必须是helperDialect,否则spring启动加载时会报错-->                    <property name="properties">                        <value>                            helperDialect=mysql                            reasonable=true                            supportMethodsArguments=true                            params=count=countSql                            autoRuntimeDialect=true                        </value>                    </property>                </bean>

           刚接触不多,于是便按照人的注解把里面的配置去掉了,然后加上了自己的配置,一运行还是报错,有点着急了,没办法,只能继续查资料,又看到一篇博客才发现为什么会报题目中的那个错误,原来PageHelper5.0版本pagehelper是继承了PageMethod和实现了Dialect,PageInterceptor是实现了Interceptor接口





        

        pageHelper是如何在mybatis中工作呢,是通过mybatis的pulgin实现了Interceptor接口,从而获得要执行的sql语句实现分页技术。所以最开始报错能解释了,可改成了PageInterceptor又报错是怎么回事呢,这这篇博客中,才注意到了错误日志里的这句话

Error parsing SQL Mapper Configuration. Cause: com.github.pagehelper.PageException: java.lang.ClassNotFoundException: mysql


          给出的解释是PageHelper插件4.0.0以后的版本支持自动识别使用的数据库,可以不用配置 <property name="dialect" value="mysql"/>    这,抱着侥幸的心理,我再一次试了,然后就成功了,真是一波三折,后来打开这个类发现了这么几句代码






       我猜测就是这通过读取properties里面的配置文件来自动识别数据库的,至于为什么再次进行声明会报错以后再研究,而且pagehelper也被引到了这个类里面


感悟

           碰到一个新的东西时主要还是要学习其工作的原理,版本问题以及一些源码的简单理解





阅读全文
0 0