MyBatis 3.0.5 和映射器加载问题
我正在使用 MyBatis 3.0.5,并且在加载映射器作为资源时遇到问题。我使用的是 Windows 7 64,我使用 Eclipse Indigo 64 位和 jdk7 64。MyBatis 在 Grizzly Web 容器(其中使用 jersey 框架实现休息服务)独立实例中初始化。
<mappers>
<mapper
url="file:///C:/Users/andrea/workspace/soap2rest/src/main/java/com/izs/mybatis/FormMapper.xml" />
<mapper resource="src/main/java/com/izs/mybatis/FormMapper.xml" />
</mappers>
我有相同的映射器仅用于测试,第一个已加载,第二个不起作用。错误:
org.apache.ibatis.exceptions.PersistenceException:
### Error building SqlSession.
### The error may exist in src/main/java/com/izs/mybatis/FormMapper.xml
### Cause: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: java.io.IOException: Could not find resource src/main/java/com/izs/mybatis/FormMapper.xml
at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:8)
at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:32)
at com.izs.Main.initMyBatis(Main.java:114)
at com.izs.Main.main(Main.java:80)
Caused by: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: java.io.IOException: Could not find resource src/main/java/com/izs/mybatis/FormMapper.xml
at org.apache.ibatis.builder.xml.XMLConfigBuilder.parseConfiguration(XMLConfigBuilder.java:85)
at org.apache.ibatis.builder.xml.XMLConfigBuilder.parse(XMLConfigBuilder.java:69)
at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:30)
... 2 more
Caused by: java.io.IOException: Could not find resource src/main/java/com/izs/mybatis/FormMapper.xml
at org.apache.ibatis.io.Resources.getResourceAsStream(Resources.java:89)
at org.apache.ibatis.io.Resources.getResourceAsStream(Resources.java:76)
at org.apache.ibatis.builder.xml.XMLConfigBuilder.mapperElement(XMLConfigBuilder.java:253)
at org.apache.ibatis.builder.xml.XMLConfigBuilder.parseConfiguration(XMLConfigBuilder.java:83)
... 4 more
Exception in thread "main" java.lang.NullPointerException
at com.izs.Main.initMyBatis(Main.java:122)
at com.izs.Main.main(Main.java:80)
抱歉我的英语。
解决方案: Maven 项目希望将资源放入 src/main/resources 和 src/test/resources 中进行测试。所以解决方案是将 xml 映射器放入这些文件夹中。
I'm using MyBatis 3.0.5 and I have problems about the loading of mappers as resources. I'm on Windows 7 64, I use Eclipse Indigo 64bit and jdk7 64. MyBatis is initialized in a Grizzly Web Container (where are implemented rest services with jersey framework) standalone instance.
<mappers>
<mapper
url="file:///C:/Users/andrea/workspace/soap2rest/src/main/java/com/izs/mybatis/FormMapper.xml" />
<mapper resource="src/main/java/com/izs/mybatis/FormMapper.xml" />
</mappers>
I have the same mappers only for testing, the first is loaded, the second doesn't work. Errors:
org.apache.ibatis.exceptions.PersistenceException:
### Error building SqlSession.
### The error may exist in src/main/java/com/izs/mybatis/FormMapper.xml
### Cause: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: java.io.IOException: Could not find resource src/main/java/com/izs/mybatis/FormMapper.xml
at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:8)
at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:32)
at com.izs.Main.initMyBatis(Main.java:114)
at com.izs.Main.main(Main.java:80)
Caused by: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: java.io.IOException: Could not find resource src/main/java/com/izs/mybatis/FormMapper.xml
at org.apache.ibatis.builder.xml.XMLConfigBuilder.parseConfiguration(XMLConfigBuilder.java:85)
at org.apache.ibatis.builder.xml.XMLConfigBuilder.parse(XMLConfigBuilder.java:69)
at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:30)
... 2 more
Caused by: java.io.IOException: Could not find resource src/main/java/com/izs/mybatis/FormMapper.xml
at org.apache.ibatis.io.Resources.getResourceAsStream(Resources.java:89)
at org.apache.ibatis.io.Resources.getResourceAsStream(Resources.java:76)
at org.apache.ibatis.builder.xml.XMLConfigBuilder.mapperElement(XMLConfigBuilder.java:253)
at org.apache.ibatis.builder.xml.XMLConfigBuilder.parseConfiguration(XMLConfigBuilder.java:83)
... 4 more
Exception in thread "main" java.lang.NullPointerException
at com.izs.Main.initMyBatis(Main.java:122)
at com.izs.Main.main(Main.java:80)
Sorry for my english.
SOLUTION:
Maven projects want resources into src/main/resources and src/test/resources for tests. So the solution is to put the xml mappers into these folders.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不要使用绝对路径。它使您的代码无法在其他环境中移植和使用。只是相对可以接受。对于您的示例,我想您可以使用以下相对路径:
Do not use absolute paths. It makes your code unportable and unused on another environment. Just relative acceptable. For your example, I guess you can use the following relative path: