Question

I am using MyBatis with Spring Integration as described here.

I am further using the Joda Date API instead of the Java Date API, now the tricky bit is ensure that MyBatis recognizes the Joda Date API, and enables me to query the same.

Now the documented way to achieve this is to use a DateTypeHandler

I am assuming that if we use the correct annotations, Mybatis will pick up the custom handler and register it with the sqlsessionfactory.

So my class looks like

@MappedTypes(value = DateTime.class)
@MappedJdbcTypes(value = {JdbcType.DATE,JdbcType.TIME,JdbcType.TIMESTAMP})

public class MyBatisJodaDateTimeType extends DateTypeHandler {
......
.....
}

Some how this does not register my Custom Type handler ...., What I am having to do, is write code like this at application startup ...

 SqlSessionFactory mybatisSessionFactory = applicationContext.getBean("sqlSessionFactory", SqlSessionFactory.class);
        MyBatisJodaDateTimeType myBatisJodaDateTimeType = applicationContext.getBean("myBatisJodaDateTimeType", MyBatisJodaDateTimeType.class);

        mybatisSessionFactory.getConfiguration().getTypeHandlerRegistry().register(DateTime.class, myBatisJodaDateTimeType);
        mybatisSessionFactory.getConfiguration().getTypeHandlerRegistry().register(DateTime.class, JdbcType.DATE, myBatisJodaDateTimeType);
        mybatisSessionFactory.getConfiguration().getTypeHandlerRegistry().register(DateTime.class, JdbcType.TIME, myBatisJodaDateTimeType);
        mybatisSessionFactory.getConfiguration().getTypeHandlerRegistry().register(DateTime.class, JdbcType.TIMESTAMP, myBatisJodaDateTimeType);
        mybatisSessionFactory.getConfiguration().getTypeHandlerRegistry().register(DateTime.class, null, myBatisJodaDateTimeType);

I know it looks like crap and i want to avoid it, cant i get MyBatis to scan my application for these annotations and then register my custom type handler automatically ?

I am sure that my type has not been registered (Using just the annotations) because i did a inspect in the MYBatis code and could not find my handler there ....

Was it helpful?

Solution

I ran into the same problem, also for a Joda DateTime TypeHandler. Tracing through, I see that the handler is actually registered. However, TypeHandlerRegistry.getTypeHandler seems to use a null jdbcType to look up the handler.

Now, the last line in your code snippet above explicitly registers a handler for null JdbcType. Unfortunately, it seems to be impossible to include null in the @MappedJdbcTypes annotation. So that looks like a bug to me.

Oh, and you may be able to just remove the @MappedJdbcTypes annotation altogether, to get it working.

UPDATE:

Here's how I set up in my applicationContext.xml:

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="mapperLocations" value="classpath*:META-INF/persistence/*.xml" />
    <property name="typeAliasesPackage" value="com.mycompany.data" />
    <property name="typeHandlersPackage" value="com.mycompany.typehandler" />
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="basePackage" value="com.mycompany.persistence" />
</bean>

If you pick up type handlers by scanning a package like this, you can set the MyBatis logging level to debug to see which ones it attempts to add.

My TypeHandler starts like this:

@MappedTypes(DateTime.class)
public class DateTimeTypeHandler implements TypeHandler<DateTime> {...}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top