MyBatis 3.0.5和映射器加载问题

时间:2011-09-26 15:00:50

标签: java ibatis mybatis

我正在使用MyBatis 3.0.5,并且我遇到了将映射器加载为资源的问题。我使用的是Windows 7 64,我使用的是Eclipse Indigo 64bit和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映射器放入这些文件夹中。

1 个答案:

答案 0 :(得分:1)

不要使用绝对路径。它使您的代码在另一个环境中不可移植和未使用。只是相对可以接受对于您的示例,我猜您可以使用以下相对路径:

<mapper resource="com/izs/mybatis/FormMapper.xml" />