MyBatis 3.0.5和映射器加载问题 [英] MyBatis 3.0.5 and mappers loading problem
问题描述
我正在使用MyBatis 3.0.5,我遇到了将映射器加载为资源的问题。我使用的是Windows 7 64,我使用Eclipse Indigo 64bit和jdk7 64. MyBatis在Grizzly Web容器中实现(在其中实现了使用jersey框架的休息服务)独立实例。
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)
对不起我的英语。
解决方案:
Maven项目需要资源到src / main / resources和src / test /资源用于测试。因此解决方案是将xml映射器放入这些文件夹中。
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.
推荐答案
不要使用绝对路径。它使您的代码在其他环境中不可移植和未使用。只是相对可接受。对于您的示例,我猜您可以使用以下相对路径:
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:
<mapper resource="com/izs/mybatis/FormMapper.xml" />
这篇关于MyBatis 3.0.5和映射器加载问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!