我是 ibatis 的新手。我喜欢将 SQLMapConfig 文件和 SQL 映射资源拆分为 2 个文件。但当我运行该项目时,它似乎显示错误。 下面是我的 2 个文件 SQL 映射。
SQLMapConfig.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">
<sqlMapConfig>
<settings
cacheModelsEnabled="true"
enhancementEnabled="true"
lazyLoadingEnabled="true"
maxRequests="32"
maxSessions="10"
maxTransactions="5"
useStatementNamespaces="false"
/>
<transactionManager type="JDBC" >
<dataSource type="DBCP">
<property name="JDBC.Driver" value="com.mysql.jdbc.Driver"/>
<property name="JDBC.ConnectionURL" value="jdbc:mysql://localhost:3306/sms_converter"/>
<property name="JDBC.Username" value="root"/>
<property name="JDBC.Password" value=""/>
</dataSource>
</transactionManager>
<sqlMap resource="configuration/db/tbluser.xml" />
</sqlMapConfig>
这是 SQL Map 资源 tbluser.xml :
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE sqlMap PUBLIC "-//iBATIS.com//DTD SQL Map 2.0//EN" "http://www.ibatis.com/dtd/sql-map-2.dtd">
<sqlMap namespace="MUser">
<select id="getUserById" parameterClass = "com.me.bean.MUser" resultClass="com.me.bean.MUser">
SELECT user_id AS sUserId FROM users where user.id = #sUserId#
</select>
</sqlMap>
但是在我运行该项目后,我遇到了这个错误:
Exception in thread "main" java.lang.RuntimeException: Error occurred. Cause: com.ibatis.common.xml.NodeletException: Error parsing XML. Cause: java.lang.RuntimeException: Error parsing XPath '/sqlMapConfig/sqlMap'. Cause: java.io.IOException: Could not find resource configuration/db/tbluser.xml
at com.ibatis.sqlmap.engine.builder.xml.SqlMapConfigParser.parse(SqlMapConfigParser.java:49)
at com.ibatis.sqlmap.client.SqlMapClientBuilder.buildSqlMapClient(SqlMapClientBuilder.java:63)
at com.me.dao.DBDao.main(DBDao.java:77)
Caused by: com.ibatis.common.xml.NodeletException: Error parsing XML. Cause: java.lang.RuntimeException: Error parsing XPath '/sqlMapConfig/sqlMap'. Cause: java.io.IOException: Could not find resource configuration/db/tbluser.xml
at com.ibatis.common.xml.NodeletParser.parse(NodeletParser.java:53)
at com.ibatis.sqlmap.engine.builder.xml.SqlMapConfigParser.parse(SqlMapConfigParser.java:46)
... 2 more
Caused by: java.lang.RuntimeException: Error parsing XPath '/sqlMapConfig/sqlMap'. Cause: java.io.IOException: Could not find resource configuration/db/tbluser.xml
at com.ibatis.common.xml.NodeletParser.processNodelet(NodeletParser.java:123)
at com.ibatis.common.xml.NodeletParser.process(NodeletParser.java:84)
at com.ibatis.common.xml.NodeletParser.process(NodeletParser.java:102)
at com.ibatis.common.xml.NodeletParser.parse(NodeletParser.java:72)
at com.ibatis.common.xml.NodeletParser.parse(NodeletParser.java:51)
... 3 more
Caused by: java.io.IOException: Could not find resource configuration/db/tbluser.xml
at com.ibatis.common.resources.Resources.getResourceAsStream(Resources.java:110)
at com.ibatis.common.resources.Resources.getResourceAsStream(Resources.java:95)
at com.ibatis.common.resources.Resources.getResourceAsReader(Resources.java:161)
at com.ibatis.sqlmap.engine.builder.xml.SqlMapConfigParser$10.process(SqlMapConfigParser.java:262)
at com.ibatis.common.xml.NodeletParser.processNodelet(NodeletParser.java:121)
... 7 more
有人可以帮助我吗?谢谢。
找不到资源配置/db/tbluser.xml,所以尝试添加前缀类路径:
<mapper resource="classpath:configuration/db/tbluster.xml">
尝试使用
<typeAlias alias="classMUser" type="com.me.bean.MUser" />
<resultMap id="userMap" class="classMUser">
<result property="sUserId" column="sUserId" />
</resultMap>
<select id="getUserById" parameterClass = "com.me.bean.MUser" resultMap="userMap">
SELECT user_id AS sUserId FROM users where user.id = #sUserId#
</select>
就我而言,问题是:
<select id="someId" parameterClass="hashmap"
resultClass="someResultClass" resultMap="someResultMap">
我在 resultMap 中写了“hashmap”而不是“someResultMap”。
纠正此问题解决了编译问题。