java MyBatis 3.0.5 和映射器加载问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7557111/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 20:29:21  来源:igfitidea点击:

MyBatis 3.0.5 and mappers loading problem

javaibatismybatis

提问by raid3n

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.

我正在使用 MyBatis 3.0.5,但在加载映射器作为资源时遇到问题。我在 Windows 7 64 上,我使用 Eclipse Indigo 64 位和 jdk7 64。MyBatis 在 Grizzly Web Container(其中使用 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>

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.

解决方案:Maven 项目希望将资源放入 src/main/resources 和 src/test/resources 以进行测试。所以解决方案是将xml映射器放入这些文件夹中。

回答by BrownFurSeal

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" />