关闭

source路径等问题 idea中配置使用mybatis出现问题BuilderException (XML文件路径问题)

2896人阅读 评论(0) 收藏 举报
分类:

http://blog.csdn.net/witsmakemen/article/details/46913425

在Idea编辑其中配置使用Mybatis出现BuilderException问题,问题描述如下:

  1. 2015-07-16 15:43:32,751 DEBUG [org.apache.ibatis.logging.LogFactory] - Logging initialized using 'org.apache.ibatis.logging.commons.JakartaCommonsLoggingImpl' adapter.  
  2. 2015-07-16 15:43:32,825 DEBUG [org.apache.ibatis.datasource.pooled.PooledDataSource] - PooledDataSource forcefully closed/removed all connections.  
  3. 2015-07-16 15:43:32,825 DEBUG [org.apache.ibatis.datasource.pooled.PooledDataSource] - PooledDataSource forcefully closed/removed all connections.  
  4. 2015-07-16 15:43:32,825 DEBUG [org.apache.ibatis.datasource.pooled.PooledDataSource] - PooledDataSource forcefully closed/removed all connections.  
  5. 2015-07-16 15:43:32,825 DEBUG [org.apache.ibatis.datasource.pooled.PooledDataSource] - PooledDataSource forcefully closed/removed all connections.  
  6. Exception in thread "main" org.apache.ibatis.exceptions.PersistenceException:   
  7. ### Error building SqlSession.  
  8. ### The error may exist in com/qunar/fresh/mybatistest/mapping/userMapper.xml  
  9. ### Cause: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: java.io.IOException: Could not find resource com/qunar/fresh/mybatistest/mapping/userMapper.xml  
  10.     at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:23)  
  11.     ... 7 more  
  12. Caused by: java.io.IOException: Could not find resource com/qunar/fresh/mybatistest/mapping/userMapper.xml  
  13.     at org.apache.ibatis.io.Resources.getResourceAsStream(Resources.java:104)  
  14.     at org.apache.ibatis.io.Resources.getResourceAsStream(Resources.java:91)  
  15.     at org.apache.ibatis.builder.xml.XMLConfigBuilder.mapperElement(XMLConfigBuilder.java:309)  
  16.     at org.apache.ibatis.builder.xml.XMLConfigBuilder.parseConfiguration(XMLConfigBuilder.java:103)  
  17.     ... 9 more  

问题:在编译后的target文件夹下,发现只有mapper的class文件,而没有xml文件,将对应的xml文件放到这个文件夹下运行就不会出现下面的错误。说明出现这个错误的原因是maven编译时没有将xml文件放进去。

解决方法:在pom.xml中添加如下代码

  1. <build>  
  2. ...  
  3.         <resources>  
  4.             <resource>  
  5.                 <directory>src/main/java</directory>  
  6.                 <includes>  
  7.                     <include>**/*.xml</include>  
  8.                 </includes>  
  9.                 <filtering>true</filtering>  
  10.             </resource>  
  11.         </resources>  
  12. ...  
  13. </build> 

mybatis读取配置文件报错:Could not find resource configuration.xml

码如下:

Java代码  收藏代码
  1. Reader reader = null;     
  2.         try {     
  3.             reader = Resources.getResourceAsReader("configuration.xml");   
  4.         } catch (IOException e) {     
  5.             e.printStackTrace();     
  6.         }  

 

执行之后报错:

Java代码  收藏代码
  1. java.io.IOException: Could not find resource configuration.xml  
  2.     at org.apache.ibatis.io.Resources.getResourceAsStream(Resources.java:89)  
  3.     at org.apache.ibatis.io.Resources.getResourceAsStream(Resources.java:76)  
  4.     at org.apache.ibatis.io.Resources.getResourceAsReader(Resources.java:134)  

 

 

说明:在项目src/main/resources目录下存在configuration.xml这个文件。



http://stackoverflow.com/questions/19730026/mybatisibatis-xml-mapping-configuration

1
 

I'm using MyBatis 3.2.2 and I have problems loading XML mappers as resources.

<mappers>
    <mapper resource="src/main/resources/Conference.xml" />
</mappers>
1
 

I'm using MyBatis 3.2.2 and I have problems loading XML mappers as resources.

<mappers>
    <mapper resource="src/main/resources/Conference.xml" />
</mappers>

Exception

nested exception is org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: java.io.IOException: Could not find resource src/main/resources/Conference.xml

1 down vote accepted
 

You are using Maven, so you can leave off the src/main/resources path altogether, as Conference.xml will be found at the root of your classpath. This should work:

<mappers>
    <mapper resource="Conference.xml" />
</mappers>












0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:640461次
    • 积分:7960
    • 等级:
    • 排名:第2573名
    • 原创:24篇
    • 转载:955篇
    • 译文:0篇
    • 评论:64条