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.Sql开发者_如何学PythonSessionFactoryBuilder.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.
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" />
精彩评论