Spring、SpringMVC和Mybatis的整合调试
一、整合Spring、SpringMVC和Mybatis用到的jar包:
Spring lib目录下的所有jar包和Mybatis相关的jar包(mybatis.jar和mybatis-spring.jar)
Mybatis
二、遇到的问题
1. xxx not bounded
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
在applicationContext.xml表头中引入相关的约束文件:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mybatis="http://mybatis.org/schema/mybatis-spring"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://mybatis.org/schema/mybatis-spring
http://mybatis.org/schema/mybatis-spring.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<mybatis:scan base-package="org.fkit.mapper" />
<context:component-scan base-package="org.fkit" />
配置文件中用到<mvc:annotation-driven />时,需要引入mvc相关的约束文件:
<beans
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"> "
<context:component-scan base-package="org.fkit.controller" />
<mvc:annotation-driven />
2. org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'bookController':
Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException:
Could not autowire field: private org.fkit.service.BookService org.fkit.controller.BookController.bookService;
nested exception is org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'bookService':
Injection of autowired dependencies failed;
nested exception is org.springframework.beans.factory.BeanCreationException
: Could not autowire field: private org.fkit.mapper.BookMapper org.fkit.service.impl.BookServiceImpl.bookMapper;
nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'bookMapper' defined in file
[D:\Soft\Eclipse_Workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\fkbookapp\WEB-INF\classes\org\fkit\mapper\BookMapper.class]:
Unsatisfied dependency expressed through bean property 'sqlSessionFactory':
: Error creating bean with name 'sqlSessionFactory' defined in ServletContext resource [/WEB-INF/applicationContext.xml]:
Invocation of init method failed;
nested exception is java.lang.BootstrapMethodError:
java.lang.NoSuchMethodError: org.apache.ibatis.session.Configuration.setVfsImpl(Ljava/lang/Class;)V; n
ested 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.BootstrapMethodError: java.lang.NoSuchMethodError: org.apache.ibatis.session.Configuration.setVfsImpl(Ljava/lang/Class;)V-
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
mybatis-spring.jar换成这个版本mybatis-spring-1.3.1.jar
3. org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 14 in XML document from ServletContext resource
[/WEB-INF/springmvc-config.xml] is invalid;
nested exception is org.xml.sax.SAXParseException; lineNumber: 14; columnNumber: 76; cvc-elt.1: 找不到元素 'beans' 的声明
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
错误原因:xmlns:beans="http://www.springframework.org/schema/beans"
解决办法:把标注红色的‘“:beans”去掉就可以了。|
4. cvc-complex-type.2.3: Element 'beans' cannot have character [children], because the type's content type is element-only.
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
原因:
copy过来的文件有特殊字符或编码格式不同。
解决方法:
将文字内容复制到Notepad等编辑器,转为UTF-8编码格式,并用ANSI编码格式查看,将不在注释内的乱码全部删除,再复制进xml即可