这两天学习strut2和spring的整合,启动服务器后总是爆粗如下错误,
org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'sqlSessionFactory' defined in URL [file:/E:/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/StrutsTest/WEB-INF/classes/applicationContext.xml]:
Cause: org.apache.ibatis.builder.BuilderException: Error creating document instance. Cause: org.xml.sax.SAXParseException:Document root element "mapper", must match DOCTYPE root "configuration".
网上有如下解答:
---------------------------------------------------------------------------------------------------------------------------------------
Document root element "mapper", must match DOCTYPE root "configuration"
后来才发现,原来是xml文件的!doctype定义写错了。
mybatis.xml和mapper.xml都用了一个doctype
原来不是这样的。
mybatis.xml:
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
mapper.xml
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
-----------------------------------------------------------------------------------------------------------------------------------------
applicationContext.xml 示例如下
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<bean id="firstAction" class="com.hoperun.action.FirstAction" scope="prototype">
<property name="live" ref="live"/>
</bean>
<bean id="ajaxAction" class="com.hoperun.action.AjaxAction" scope="prototype"></bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/test"></property>
<property name="username" value="root"></property>
<property name="password" value="123"></property>
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="configLocation" value="classpath:Configuration.xml"></property>
<property name="dataSource" ref="dataSource"></property>
</bean>
</beans>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="configLocation" value="classpath:Configuration.xml"></property>
<property name="dataSource" ref="DBPoolDataSource"></property>
</bean>
</beans>
userMap.xml 示例:
<?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">
<mapper namespace="com.hoperun">
<select id="selectUser" parameterType="int"
resultType="com.hoperun.bean.User">
select
*
from
student
where
id = #{Id}
</select>
</mapper>