Quantcast
Viewing all articles
Browse latest Browse all 10

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!

Viewing all articles
Browse latest Browse all 10

Trending Articles