1. 程式人生 > >java.lang.ClassCastException: com.github.pagehelper.PageHelpercannot be cast to org.apache.Intercept

java.lang.ClassCastException: com.github.pagehelper.PageHelpercannot be cast to org.apache.Intercept

使用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>

問題解決。