问题描述
- spring+iBatis的xml解析问题
-
使用Spring+iBatis开发,测试是发生如下异常,百思不得其解啊!求高人指点`Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlMapClient' defined in class path resource [applicationContext.xml]: Invocation of init method failed; nested exception is org.springframework.core.NestedIOException: Failed to parse config resource: class path resource [sql-map-config.xml]; nested exception is com.ibatis.common.xml.NodeletException: Error parsing XML. Cause: java.lang.RuntimeException: Error parsing XPath '/sqlMapConfig/sqlMap'. Cause: com.ibatis.common.xml.NodeletException: Error parsing XML. Cause: org.xml.sax.SAXParseException; lineNumber: 2; columnNumber: 1; 文件提前结束。`
以下是我的代码
applicationContext.xml:
<?xml version="1.0" encoding="UTF-8"?>
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd"><!-- lob字段配置 --> <bean id="nativeJdbcExtractor" lazy-init="true" class="org.springframework.jdbc.support.nativejdbc.CommonsDbcpNativeJdbcExtractor" /> <!-- class="org.springframework.jdbc.support.nativejdbc.WebSphereNativeJdbcExtractor" /> --> <bean id="lobHandler" lazy-init="true" class="org.springframework.jdbc.support.lob.OracleLobHandler"> <property name="nativeJdbcExtractor"> <ref bean="nativeJdbcExtractor" /> </property> </bean> <!-- 加载系统数据源配置文件 --> <!-- <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName"> <value>org.logicalcobwebs.proxool.ProxoolDriver </value> </property> <property name="url"> <value>proxool.cloud</value> </property> </bean> -->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location" value="jdbc.properties" /> <property name="fileEncoding" value="UTF-8" /> </bean> <bean id="dataSource" class="org.logicalcobwebs.proxool.ProxoolDataSource"> <!-- <property name="driver"> <value>com.mysql.jdbc.Driver</value> </property> <property name="driverUrl"> <value>jdbc:mysql://localhost/mytest?characterEncoding=utf-8</value> </property> --> <property name="driver"> <value>${jdbc.driver}</value> </property> <property name="driverUrl"> <value>${jdbc.url}</value> </property> <property name="user"> <value>${jdbc.username}</value> </property> <property name="password"> <value>${jdbc.password}</value> </property> <!-- <property name="user"> <value>root</value> </property> <property name="password"> <value>root</value> </property> --> <property name="alias"> <value>wstudy</value> </property> <property name="houseKeepingSleepTime"> <value>60000</value> </property> <property name="prototypeCount"> <value>5</value> </property> <property name="simultaneousBuildThrottle"> <value>20</value> </property> <property name="maximumActiveTime"> <value>60000</value> </property> <property name="houseKeepingTestSql"> <value>select CURRENT_DATE</value> </property> <property name="maximumConnectionCount"> <value>10</value> </property> <property name="minimumConnectionCount"> <value>1</value> </property> <property name="trace"> <value>true</value> </property> <property name="verbose"> <value>true</value> </property> </bean> <!-- *************************************************************************** --> <!-- 事务管理 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource"> <ref bean="dataSource"/> </property> </bean> <!-- 事务通知 --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="find*" propagation="REQUIRED" timeout="60" rollback-for="Exception"/> <tx:method name="add*" propagation="REQUIRED" timeout="60" rollback-for="Exception"/> <tx:method name="delete*" propagation="REQUIRED" timeout="60" rollback-for="Exception"/> <tx:method name="modify*" propagation="REQUIRED" timeout="60" rollback-for="Exception"/> <tx:method name="save*" propagation="REQUIRED" timeout="60" rollback-for="Exception"/> <tx:method name="insert*" propagation="REQUIRED" timeout="60" rollback-for="Exception"/> <tx:method name="update*" propagation="REQUIRED" timeout="60" rollback-for="Exception"/> <tx:method name="*" read-only="true" timeout="60"/> </tx:attributes> </tx:advice> <!-- WEB-INF/classes/ --> <bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean"> <property name="configLocations" value="sql-map-config.xml" /> <property name="dataSource" ref="dataSource" /> </bean> <bean id="sqlMapClientTemplate" class="org.springframework.orm.ibatis.SqlMapClientTemplate"> <property name="sqlMapClient"> <ref bean="sqlMapClient" /> </property> </bean> <bean id="baseDao" class="com.wstudy.core.dao.BaseDaoImpl"> <property name="sqlMapClientTemplate"> <ref bean="sqlMapClientTemplate" /> </property> </bean> <!--将上面的模版类织入到我们的DAO对象中 --> <bean id="studentDao" class="test.ibatis.StudentDao"> <property name="sqlMapClientTemplate"> <ref bean="sqlMapClientTemplate" /> </property> </bean> <bean id="stDao" class="com.wstudy.dao.impl.StudentDaoImpl" parent="baseDao" scope="prototype"> </bean> <!-- <bean id="dbtest" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource"> <ref local="dataSource" /> </property> </bean> --> <!-- 小柴的beans开始 --> <!-- 导入其他配置模块 --> <import resource="com/wstudy/questions/service/Question.xml" /> <!-- 导入其他配置模块 --> <import resource="com/wstudy/service/teacher/teacher.xml" />
sql-map-config.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMapConfig
PUBLIC "-//iBATIS.com//DTD SQL Map Config 2.0//EN"
"http://www.ibatis.com/dtd/sql-map-config-2.dtd"><settings cacheModelsEnabled="true" enhancementEnabled="true" lazyLoadingEnabled="true" errorTracingEnabled="true" maxRequests="50" maxSessions="10" maxTransactions="5" useStatementNamespaces="true" /> <!-- <sqlMap resource="com/wstudy/test/AdminUserEntity.xml" /> --> <sqlMap resource="com/wstudy/entity/teacher/TeacherEntity.xml" /> <sqlMap resource="com/wstudy/entity/teacher/EvaluationEntity.xml" /> <sqlMap resource="com/wstudy/entity/teacher/TeacherStarEntity.xml" /> <sqlMap resource="com/wstudy/entity/teacher/QueAnsEntity.xml" /> <sqlMap resource="com/wstudy/entity/teacher/EvaTeacherEntity.xml" /> <sqlMap resource="com/wstudy/core/entity/StudentEntity.xml" /> <sqlMap resource="test/ibatis/Student.xml" /> <sqlMap resource="com/wstudy/questions/entity/ComQuesAnsEntity.xml" /> <sqlMap resource="com/wstudy/questions/entity/ComQuesEntity.xml" /> <sqlMap resource="com/wstudy/questions/entity/TypicalQuesEntity.xml" />
解决方案
你文件应该没贴全吧,sql-map-config.xml文件里头 提前结束了,所以才报SAX解析XML异常。