最近在学习ibatis,但是在配置文件的地方出现了错误,老是提示org.xml.sax.SAXParseException: The content of element type “sqlMapConfig” must match “(properties?,settings?,resultObjectFactory?,ypeAlias*,typeHandler*,transactionManager?,sqlMap+)+”,
经过网上不断的搜索,终于找到了原因,原来ibatis配置文件的属性是有顺序的。
这个是我以前的代码:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE sqlMapConfig
PUBLIC "-//ibatis.apache.org//DTD sql Map Config 2.0//EN"
"http://ibatis.apache.org/dtd/sql-map-config-2.dtd">
<sqlMapConfig>
<settings
cacheModelsEnabled="true"
enhancementEnabled="true"
lazyLoadingEnabled="true"
maxRequests="32"
maxSessions="10"
maxTransactions="5"
useStatementNamespaces="false"
/>
<properties resource="com/ibatis/utils/sqlMap.properties"/>
<transactionManager type="JDBC" commitrequired="false">
<dataSource type="SIMPLE">
<property name="JDBC.Driver" value="${driver}"/>
<property name="JDBC.ConnectionURL" value="${url}"/>
<property name="JDBC.Username" value="${username}"/>
<property name="JDBC.Password" value="${password}"/>
</dataSource>
</transactionManager>
<sqlMap resource="com/ibatis/utils/User.xml"/>
</sqlMapConfig>
注意红色部分的顺序。
然而正确的顺序应该是
properties?,resultObjectFactory?ypeAlias*,sqlMap
所以改成如下的顺序就OK了:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE sqlMapConfig
PUBLIC "-//ibatis.apache.org//DTD sql Map Config 2.0//EN"
"http://ibatis.apache.org/dtd/sql-map-config-2.dtd">
<sqlMapConfig>
<properties resource="com/ibatis/utils/sqlMap.properties"/>
<settings
cacheModelsEnabled="true"
enhancementEnabled="true"
lazyLoadingEnabled="true"
maxRequests="32"
maxSessions="10"
maxTransactions="5"
useStatementNamespaces="false"
/>
<transactionManager type="JDBC" commitrequired="false">
<dataSource type="SIMPLE">
<property name="JDBC.Driver" value="${driver}"/>
<property name="JDBC.ConnectionURL" value="${url}"/>
<property name="JDBC.Username" value="${username}"/>
<property name="JDBC.Password" value="${password}"/>
</dataSource>
</transactionManager>
<sqlMap resource="com/ibatis/utils/User.xml"/>
</sqlMapConfig>
问题解原文地址:http://www.jb51.cc/article/p-olcbzizo-bgy.html