package scanning skipping mapper files in jar file

461 views
Skip to first unread message

Ted S.

unread,
Dec 18, 2015, 7:31:18 PM12/18/15
to mybatis-user

Hello all,


Package scanning seems to be skipping mapper files packaged in a jar file. I'm not using Spring-Boot; it's just a straight-up export-as-jar from Eclipse.


For example:

<mappers>
  <!-- Doesn't work when I package my app as a single JAR. -->
  <package name="testmy.sqlmaps" />
  <!-- Works even when mapper files are in a JAR file. -->
  <!--mapper class="testmy.sqlmaps.TestMyMapper" /-->
</mappers>


Things work properly when I run my project via Eclipse or via command line as an "exploded jar", but if I run my app via command line packaged in a jar file then I get the following stack trace:

org.apache.ibatis.binding.BindingException: Type interface testmy.sqlmaps.TestMyMapper is not known to the MapperRegistry.
    at org.apache.ibatis.binding.MapperRegistry.getMapper(MapperRegistry.java:47)
    at org.apache.ibatis.session.Configuration.getMapper(Configuration.java:675)
    at org.apache.ibatis.session.defaults.DefaultSqlSession.getMapper(DefaultSqlSession.java:250)
    at testmy.dao.TestMyDAO.testMyBatisQuery(TestMyDAO.java:9)
    at testmy.batch.TestMybatis.main(TestMybatis.java:17)


It's as if ResolverUtil is either skipping my application jar or doesn't know to look inside for goodies.


I'm running Mybatis v3.3.0. Also, attached is a single file containing relevant source file contents, which is as distilled as I can muster.


Any ideas?

packageScanFileContents.txt

Iwao AVE!

unread,
Jan 15, 2016, 10:16:29 AM1/15/16
to mybatis-user
Hi Ted,

The problem is that Java does not provide any standard way to load nested jars.
So the issue is not specific to Spring-Boot.

Regards,
Iwao

Ted S.

unread,
Jan 18, 2016, 5:24:29 PM1/18/16
to mybatis-user
Hi Iwao,

Thanks for your thoughts. However, my deployment isn't structured as nested jars. It's along these lines on the server file system:

C:/test/testMyBatisBatch/
- lib/
- various third-party jars are here
- MybatisCommandLine.jar
- RunTest.bat
My mapper XML files are inside MybatisCommandLine.jar, as is the main class invoked by RunTest.bat.

Perhaps I'm confused by what you mean by "nested jars".

Cheers,

Ted

Iwao AVE!

unread,
Jan 21, 2016, 3:44:28 AM1/21/16
to mybatis-user
Hi Ted,

It might be another case that ResolverUtil cannot handle correctly.
Could you upload your project to your GitHub repo or somewhere so that
I can reproduce the problem quickly and reliably?

Regards,
Iwao
> --
> You received this message because you are subscribed to the Google Groups
> "mybatis-user" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to mybatis-user...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Iwao AVE!

unread,
Jan 28, 2016, 12:02:15 PM1/28/16
to mybatis-user
Hi again,

I've created the test project for you.
https://github.com/harawata/mapper-package-demo
And the mapper is picked up correctly.
Any difference from your project?

// Iwao

Ted S.

unread,
Feb 3, 2016, 5:53:10 PM2/3/16
to mybatis-user
Hi Iwao,

Thanks for putting together a sample app! I really appreciate you taking the time to do so.

I ran your app and it works fine for me. The main difference I see between your app and mine is you use an @Select annotation in the mapper interface, whereas I'm using an XML file:

--
-- TestMyMapper.xml
--

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="testmy.sqlmaps.TestMyMapper">
    <select id="testMyBatisQuery" resultType="string">
        SELECT 'Testing'
          FROM SYSIBM.SYSDUMMY1
    </select>
</mapper>

It's as if the package scanning won't pick up the XML.


>> Could you upload your project to your GitHub repo or somewhere so that I can reproduce the problem quickly and reliably?

I'll see what I can do; I've not used GitHub before but it's about time I've learned.

Cheers!

Iwao AVE!

unread,
Feb 3, 2016, 7:12:05 PM2/3/16
to mybatis-user
Hi Ted,

You are welcome and I thank you for taking the time to report it!
I have just pushed the XML mapper version and it still works as expected.
Maybe you should use JD-GUI [1] to inspect the generated JAR?

GitHub is a great tool to collaborate.
If you found a way to break the demo project, try sending a pull
request as a practice ;D

Regards,
Iwao

[1] http://jd.benow.ca/

Ted S.

unread,
Feb 4, 2016, 12:01:14 PM2/4/16
to mybatis-user
Hi Iwao,

Thanks for posting a revised jar file. Good news: I figured out the problem!

It turns out the issue was caused by how I was building the jar file.

Specifically, this issue arises when the "Add directory entries" checkbox is unchecked on the "JAR File Specification" step while exporting the project as a jar file in Eclipse. Ensuring the checkbox is checked solves the problem.

I'm spreading the word via this SO: http://stackoverflow.com/questions/31997159/package-scanning-skipping-mapper-files-in-jar-file.

Thanks so much for your help! It'll be so nice to start using package scanning in my jar deployments.

Ted

Iwao AVE!

unread,
Feb 4, 2016, 10:20:20 PM2/4/16
to mybatis-user
Good to know. Thanks for the explanation!
Enjoy package scanning! ;D

Cheers,
Iwao

2016-02-05 2:01 GMT+09:00 Ted S. <gudf...@gmail.com>:
> Hi Iwao,
>
Reply all
Reply to author
Forward
0 new messages