前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >source路径等问题 idea中配置使用mybatis出现问题BuilderException (XML文件路径问题)

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

作者头像
bear_fish
发布2018-09-19 15:40:25
2.5K0
发布2018-09-19 15:40:25
举报

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

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

[plain] view plain copy

  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中添加如下代码

[html] view plain copy

  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>

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016年08月30日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • mybatis读取配置文件报错:Could not find resource configuration.xml
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档