MyBatis 3.0.5 and mappers loading problem

2,813 views
Skip to first unread message

raid3n

unread,
Sep 26, 2011, 11:36:02 AM9/26/11
to mybatis-user
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.

Poitras Christian

unread,
Sep 26, 2011, 1:12:33 PM9/26/11
to mybati...@googlegroups.com
Try <mapper resource="com/izs/mybatis/FormMapper.xml"/>
MyBaits loads resources from the classpath.

Christian

-----Message d'origine-----
De : mybati...@googlegroups.com [mailto:mybati...@googlegroups.com] De la part de raid3n
Envoyé : September-26-11 11:36 AM
À : mybatis-user
Objet : MyBatis 3.0.5 and mappers loading problem

raid3n

unread,
Sep 27, 2011, 2:07:28 AM9/27/11
to mybatis-user
If I try your pat I get:

org.apache.ibatis.exceptions.PersistenceException:
### Error building SqlSession.
### The error may exist in com/izs/mybatis/FormMapper.xml
### Cause: org.apache.ibatis.builder.BuilderException: Error parsing
SQL Mapper Configuration. Cause: java.io.IOException: Could not find
resource 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:115)
at com.izs.Main.main(Main.java:81)
Caused by: org.apache.ibatis.builder.BuilderException: Error parsing
SQL Mapper Configuration. Cause: java.io.IOException: Could not find
resource 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 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:123)
at com.izs.Main.main(Main.java:81)


On 26 Set, 19:12, Poitras Christian <Christian.Poit...@ircm.qc.ca>
wrote:

Poitras Christian

unread,
Sep 27, 2011, 8:17:31 AM9/27/11
to mybati...@googlegroups.com
It seems the resources cannot be seen on the classpath.
You can check by calling Class.getResource("/com/izs/mybatis/FormMapper.xml");

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

Christian

-----Message d'origine-----
De : mybati...@googlegroups.com [mailto:mybati...@googlegroups.com] De la part de raid3n

Envoyé : September-27-11 2:07 AM
À : mybatis-user
Objet : Re: MyBatis 3.0.5 and mappers loading problem

raid3n

unread,
Sep 28, 2011, 3:06:22 AM9/28/11
to mybatis-user
This works fine:
this.getClass().getResource("com/izs/mybatis/FormMapper.xml");

If I write <mapper resource="com/izs/mybatis/FormMapper.xml" /> in the
Configuration.xml the errors are showed after a while. Instead if I
put another path the errors are showed immediately.

On 27 Set, 14:17, Poitras Christian <Christian.Poit...@ircm.qc.ca>
wrote:
> It seems the resources cannot be seen on the classpath.
> You can check by calling Class.getResource("/com/izs/mybatis/FormMapper.xml");
>
> You can try <mapper resource="/com/izs/mybatis/FormMapper.xml"/>
>
> Christian
>
> -----Message d'origine-----
> De : mybati...@googlegroups.com [mailto:mybati...@googlegroups.com] De la part de raid3n
> Envoyé : September-27-11 2:07 AM
> À : mybatis-user
> Objet : Re: MyBatis 3.0.5 and mappers loading problem
>
> If I try your pat I get:
>
> org.apache.ibatis.exceptions.PersistenceException:
> ### Error building SqlSession.
> ### The error may exist in com/izs/mybatis/FormMapper.xml
> ### Cause: org.apache.ibatis.builder.BuilderException: Error parsing
> SQL Mapper Configuration. Cause: java.io.IOException: Could not find
> resource com/izs/mybatis/FormMapper.xml
>         at
> org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactor y.java:
> 8)
>         at
> org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryB uilder.java:
> 32)
>         at com.izs.Main.initMyBatis(Main.java:115)
>         at com.izs.Main.main(Main.java:81)
> Caused by: org.apache.ibatis.builder.BuilderException: Error parsing
> SQL Mapper Configuration. Cause: java.io.IOException: Could not find
> resource com/izs/mybatis/FormMapper.xml
>         at
> org.apache.ibatis.builder.xml.XMLConfigBuilder.parseConfiguration(XMLConfig Builder.java:
> 85)
>         at
> org.apache.ibatis.builder.xml.XMLConfigBuilder.parse(XMLConfigBuilder.java:
> 69)
>         at
> org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryB uilder.java:
> 30)
>         ... 2 more
> Caused by: java.io.IOException: Could not find resource 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

raid3n

unread,
Sep 28, 2011, 3:55:07 AM9/28/11
to mybatis-user
Sorry.

So I think I've problem with my classpath settings. It's a Maven
Project.
I'm trying:

in these cases url is null:
URL url = this.getClass().getResource("src/main/java/com/izs/mybatis/
FormMapper.xml");
URL url = this.getClass().getResource("/src/main/java/com/izs/mybatis/
FormMapper.xml");
URL url = this.getClass().getResource("com/izs/mybatis/
FormMapper.xml");
URL url = this.getClass().getResource("/com/izs/mybatis/
FormMapper.xml");

raid3n

unread,
Sep 28, 2011, 3:57:08 AM9/28/11
to mybatis-user
My .classpath file:

<classpath>
<classpathentry kind="src" path="src/main/java" including="**/
*.java"/>
...
</classpath>

raid3n

unread,
Sep 28, 2011, 4:15:28 AM9/28/11
to mybatis-user
Sorry for my flood:

URL ur = Thread.currentThread().getContextClassLoader()
.getResource("src/main/java/com/izs/mybatis/FormMapper.xml");

It's not null. Why?

Richard Yee

unread,
Sep 28, 2011, 8:33:55 AM9/28/11
to mybati...@googlegroups.com
If you are using maven, your mapper and config files should be put under the src/main/resources, not src/main/java. The package goal would then include them in the jar or war file you build. You would then reference them using the package path only(ie "java/com/izs/mybatis/FormMapper.xml")
If the files are for testing, then they would be put under src/test/resources

Richard


Sent from my iPhone

raid3n

unread,
Sep 29, 2011, 2:13:17 AM9/29/11
to mybatis-user
THX!!!!!!!!!!!!!!!!!!!!!!

It's the solution!!!

Best Regards,
Andrea

On 28 Set, 14:33, Richard Yee <richard.k....@gmail.com> wrote:
> If you are using maven, your mapper and config files should be put under the src/main/resources, not src/main/java. The package goal would then include them in the jar or war file you build. You would then reference them using the package path only(ie "java/com/izs/mybatis/FormMapper.xml")
> If the files are for testing, then they would be put under src/test/resources
>
> Richard
>
> Sent from my iPhone
>
Reply all
Reply to author
Forward
0 new messages