spring mvc+mybatis+velocity开发遇到的一个错误

用spring mvc+mybatis+velocity开发时,遇到了一个报错,折腾了几个小时:

严重: Context initialization failed
org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name 'org.springframework.web.servlet.mvc.
annotation.DefaultAnnotationHandlerMapping#0' defined in ServletContext
 resource [/WEB-INF/applicationContext.xml]: Initialization of bean failed;
 nested exception is org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name 'loginController': Injection of autowired 
dependencies failed; nested exception is org.springframework.beans.factory.
BeanCreationException: Could not autowire field: private com.adump.schedule
.service.UserService com.adump.schedule.controller.LoginController.userService; 
nested exception is org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name 'userService': Injection of autowired dependencies
 failed; nested exception is org.springframework.beans.factory.BeanCreationException: 
Could not autowire field: private com.adump.schedule.mybatis.MyBatisDao com.adump.schedule.service.UserService.myBatisDao; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean
 with name 'MyBatisDao' defined in ServletContext resource
 [/WEB-INF/applicationContext.xml]: Initialization of bean failed; nested
 exception is org.springframework.beans.factory.BeanCreationException:
 Error creating bean with name 'sqlSessionFactory' defined in ServletContext
 resource [/WEB-INF/applicationContext.xml]: Invocation of init method failed; 
nested exception is java.lang.NoSuchMethodError: org.apache.ibatis.builder.xml.
XMLConfigBuilder.<init>(Ljava/io/InputStream;Ljava/lang/String;
Ljava/util/Properties;)V
	at org.springframework.beans.factory.support
.AbstractAutowireCapableBeanFactory.doCreateBean(
AbstractAutowireCapableBeanFactory.java:527)
	at org.springframework.beans.factory.support
.AbstractAutowireCapableBeanFactory.createBean(
AbstractAutowireCapableBeanFactory.java:456)

异常信息一大堆,关键的地方是“java.lang.NoSuchMethodError: org.apache.ibatis.builder.xml.XMLConfigBuilder.(Ljava/io/InputStream;Ljava/lang/String;Ljava/util/Properties;)”。

网上搜了下,说把 applicationContext.xml 里面的这行去掉就可以正常运行:

<property name="configLocation" value="mybatis-config.xml" />

其实就是mybatis的版本问题,我用的版本是:

		<!-- mybatis -->
		<dependency>
			<groupId>org.mybatis</groupId>
			<artifactId>mybatis</artifactId>
			<version>${mybatis.version}</version>
		</dependency>
		<dependency>
			<groupId>org.mybatis</groupId>
			<artifactId>mybatis-spring</artifactId>
			<version>${mybatis-spring.version}</version>
		</dependency>
         <properties>
		<org.springframework.version>3.0.6.RELEASE
</org.springframework.version>
		<mybatis.version>3.0.2</mybatis.version>
		<mybatis-spring.version>1.0.0</mybatis-spring.version>
		<velocity.version>1.6.4</velocity.version>
		<velocity.tools.version>2.0</velocity.tools.version>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>

把 mybatis.version 改为3.0.4就好了。囧

发表评论

电子邮件地址不会被公开。 必填项已用*标注