记录下MyBatis整合Spring时用JUnit测试过程中出现的错误,只附上主要报错信息
首先是:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [spring/applicationContext.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.apache.commons.dbcp.BasicDataSource]: No default constructor found; nested exception is java.lang.NoClassDefFoundError: org/apache/commons/pool/impl/GenericObjectPool
错误原因是缺少GenericObjectPool的相关包,我之前已经导入了commons-dbcp-1.2.2.jar,还缺少commons-pool-1.3.jar,导入即可。
然后是:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactory' defined in class path resource [spring/applicationContext.xml]: Invocation of init method failed; nested exception is java.io.FileNotFoundException: class path resource [spring_mybatis/config/mybatis/SqlMapConfig.xml] cannot be opened because it does not exist
错误原因是在定义数据库链接池、SqlSessionFactory的applicationContext.xml文件中配置加载MyBatis的相关文件时将property标签中的value属性即路径设置错误,改正即可。
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 加载mybatis的配置文件 -->
<property name="configLocation" value="/mybatis/SqlMapConfig.xml" />
<!-- 数据源 -->
<property name="dataSource" ref="dataSource" />
</bean>
最后是:
java.lang.AbstractMethodError: org.mybatis.spring.transaction.SpringManagedTransaction.getTimeout()Ljava/lang/Integer;
经查询错误原因是原mybatis整合spring的jar包版本过低,将mybatis-spring-1.2.2.jar替换为mybatis-spring-1.3.1.jar即可解决。