Order matters with MyBatis Configuration

If you are using MyBatis TypeHandlers in your Configuration, you need to register them BEFORE you add your Mapper instances. As it turns out, MyBatis proactively parses your XML and completely configures each mapper when it is registered. If you mapper XML uses results that need a TypeHandler, you must register those before hand.

Here’s some quick code:

configuration.getTypeHandlerRegistry().register(Locale.class, LocaleTypeHandler.class);
configuration.addMapper(SomeLocalMapper.class);

This works because the TypeHandler is registered before the mapper that requires it. This won’t work:

configuration.addMapper(SomeLocalMapper.class);
configuration.getTypeHandlerRegistry().register(Locale.class, LocaleTypeHandler.class); // FAIL!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s