Using custom types in MyBatis / Spring

I am using MyBatis with Spring Integration as described here .

I use the Joda Date API instead of the Java Date API, now the complex bit ensures that MyBatis recognizes the Joda Date API and allows me to request the same.

Now the documented way to achieve this is to use DateTypeHandler

I assume that if we use the correct annotations, Mybatis will take a custom handler and register it using sqlsessionfactory.

So my class looks like

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

Some, as my custom type handler does not register this .... I need to make such code as when I started the application ...

  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 this looks like shit, and I want to avoid this, can MyBatis scan my application for these annotations and then automatically register a custom type handler?

I am sure that my type was not registered (using only annotations) because I checked in MYBatis code and could not find my handler there.

+4
source share
1 answer

I ran into the same issue, also for Joda DateTime TypeHandler. By tracking, I see that the handler is actually registered. However, TypeHandlerRegistry.getTypeHandler seems to use null jdbcType to find the handler.

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

Oh, and you can simply remove the @MappedJdbcTypes annotation @MappedJdbcTypes to make it work.

UPDATE:

This is how I set up my applicationContext.xml file:

 <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 take type handlers by scanning such a package, you can configure the logging level of MyBatis for debugging to see which ones it is trying to add.

My TypeHandler starts as follows:

 @MappedTypes(DateTime.class) public class DateTimeTypeHandler implements TypeHandler<DateTime> {...} 
+4
source

Source: https://habr.com/ru/post/1390380/


All Articles