Multiple SqlSessionFactoryBean config locations

1,576 views
Skip to first unread message

Pawel Zmarlicki

unread,
Jun 13, 2012, 11:49:52 AM6/13/12
to mybatis-user
I'm upgrading an application written for iBatis 2.3 to MyBatis 3.1.1
with Spring integration 1.1.1.

In the original application I initialize SqlMapClientFactoryBean using
configuration spread over multiple locations, using the method
setConfigLocations(Resource[] configLocations).

In MyBatis, the factory bean class SqlSessionFactoryBean only supports
initialization with the method setConfigLocation(Resource
configLocation), that is only one config location.

Are there any plans to extend SqlSessionFactoryBean to support
multiple configuration locations? Any recommended workarounds?

Eduardo Macarron

unread,
Jun 13, 2012, 12:00:21 PM6/13/12
to mybati...@googlegroups.com
I suppose you are looking for a way to specify where your mapper files
are, in that case the property is mapperLocations:

http://www.mybatis.org/spring/factorybean.html

2012/6/13 Pawel Zmarlicki <pzmar...@gmail.com>:

Guy Rouillier

unread,
Jun 13, 2012, 5:59:10 PM6/13/12
to mybati...@googlegroups.com
On 6/13/2012 11:49 AM, Pawel Zmarlicki wrote:

> Are there any plans to extend SqlSessionFactoryBean to support
> multiple configuration locations? Any recommended workarounds?

In MyBatis 3, you can specify multiple mapper files in the main
configuration file. Is that sufficient for your needs? The various
mapper files are specified as resources, so they can be located anywhere
that resources can be found.

--
Guy Rouillier

Pawel Zmarlicki

unread,
Jun 14, 2012, 7:12:09 AM6/14/12
to mybatis-user
Thank you. I think I'm a bit more clear about this now: in the old
application each of the configuration files contained a mixture of
actual configuration information, such as type handlers, etc., as well
as references to sql maps, in the format <sqlMap
resource="some_resource"/>.

Looks like I need to factor out all configuration info into one file,
then discard the references to mappers from configuration, and instead
use the setMapperLocations() method to point to them.


On Jun 13, 12:00 pm, Eduardo Macarron <eduardo.macar...@gmail.com>
wrote:
> I suppose you are looking for a way to specify where your mapper files
> are, in that case the property is mapperLocations:
>
> http://www.mybatis.org/spring/factorybean.html
>
> 2012/6/13 Pawel Zmarlicki <pzmarli...@gmail.com>:

Pawel Zmarlicki

unread,
Jun 14, 2012, 7:13:02 AM6/14/12
to mybatis-user
Yes, with some refactoring of the existing code, I think it will
work.

Mike Fotiou

unread,
Jun 13, 2012, 8:14:21 PM6/13/12
to mybati...@googlegroups.com
I had this problem - we had several web modules and several shared library projects (JARs).  We wanted to use one Mybatis XML configuration file yet have mappers in multiple locations.  The locations in one project might not be around in another, yet the Mybatis layer was shared by all web modules.

We ended up writing our own XMLClassPathConfigBuilder that loops through all files in the classpath, as given by the classloader, including within JAR files.  It will dynamically find and load all *mapper.xml files.

It works quite well.  We can even catch when xml files change and reload them (great for development).

Is this the type of solution you are looking for?

Pawel Zmarlicki

unread,
Jun 15, 2012, 8:48:31 AM6/15/12
to mybatis-user
Eduardo, I hit a problem with this approach: in the original
application, some sql maps have definitions of type aliases. In
3.1.1, I need to combine all these aliases in one config file.
However, depending on how the application is deployed, some of the
classes corresponding to these type aliases may not be included at run
time - in other words, type aliases need to be discovered dynamically.

The only workaround I can think of is to find them dynamically, then
parse each one individually, combine manually all of them into one xml
document, write it to a string, use the string to create an instance
of InputStreamResource with the combined contents, and use this
InputStreamResource to initialize configuration of the factory bean.
This seems like a lot of added complexity, which the original
application did not require. Is there a simpler way, that I'm not
seeing?

On Jun 13, 12:00 pm, Eduardo Macarron <eduardo.macar...@gmail.com>
wrote:
> I suppose you are looking for a way to specify where your mapper files
> are, in that case the property is mapperLocations:
>
> http://www.mybatis.org/spring/factorybean.html
>
> 2012/6/13PawelZmarlicki<pzmarli...@gmail.com>:

Pawel Zmarlicki

unread,
Jun 15, 2012, 9:00:37 AM6/15/12
to mybati...@googlegroups.com
Mike, since I'm using MyBatis with Spring integration, I can use this type of syntax to get all mappers: Resource[] mapperLocations = resolver.getResources("classpath*:package/**/mappers/*.xml").  My issue is with configuration: in 3.1.1, SqlSessionFactoryBean will let me specify only one configuration location, so I can't provide it with an array of Resource, as provided by this method.  This was possible in iBATIS 2.x.

Eduardo Macarron

unread,
Jun 15, 2012, 9:51:44 AM6/15/12
to mybati...@googlegroups.com
Hi Pawel, if there is a pattern you can autodiscover the aliases with
the typeAliasesPackage property.

2012/6/15 Pawel Zmarlicki <pzmar...@gmail.com>:
Reply all
Reply to author
Forward
0 new messages