解析XPath'/ sqlMapConfig / sqlMap'时出现iBatis错误

时间:2017-05-09 03:29:52

标签: java xml xpath ibatis

我是ibatis的新手。我喜欢将SQLMapConfig文件和SQL映射资源拆分为2个文件。但是当我运行该项目时,它似乎显示错误。 以下是我的2文件SQL Map。

  1. 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>
    
  2. 这里是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
    
    谁能帮助我吗?感谢。

2 个答案:

答案 0 :(得分:0)

无法找到资源配置/ db / tbluser.xml,因此请尝试添加前缀classpath:

<mapper resource="classpath:configuration/db/tbluster.xml">

答案 1 :(得分:0)

尝试使用

<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>