使用pagehelper插件时出现的异常。
我原先mybatis的配置如下
<?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>
<plugins>
<plugin interceptor="com.github.pagehelper.PageHelper">
<property name="dialect" value="mysql"/>
</plugin>
</plugins>
</configuration>
异常:Error creating bean with name 'sqlSessionFactory' defined in file [D:\eclipse-workspace2\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\taotao-manager-web\WEB-INF\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/sqlSessionConfig.xml]; nested exception is org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: java.lang.ClassCastException: com.github.pagehelper.PageHelper cannot be cast to org.apache.ibatis.plugin.Interceptor 。
意思是强制转换不成功。
原因分析:
<pagehelper.version>5.1.2</pagehelper.version>
我使用的pagehelper的版本是5.1,5.0之后的版本使用com.github.pagehelper.PageInterceptor这个类。
解决方案:
将mybatis配置文件的
<plugin interceptor="com.github.pagehelper.PageHelper">
改为
<plugin interceptor="com.github.pagehelper.PageInterceptor">
<?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>
<plugins>
<plugin interceptor="com.github.pagehelper.PageInterceptor">
<property name="dialect" value="mysql"/>
</plugin>
</plugins>
</configuration>
改正之后又爆出了错误。
Error creating bean with name 'sqlSessionFactory' defined in file [D:\eclipse-workspace2\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\taotao-manager-web\WEB-INF\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/sqlSessionConfig.xml]; nested exception is org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: com.github.pagehelper.PageException: java.lang.ClassNotFoundException: mysql
错误的信息很明显,oracle类不识别,最终原因还是因为版本的问题,自4.0.0以后的版本已经可以自动识别数据库了,所以不需要我们再去指定数据库,所以,修改配置:
<?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>
<plugins>
<plugin interceptor="com.github.pagehelper.PageInterceptor">
</plugin>
</plugins>
</configuration>
问题解决。