TypeHandlerYou can refer to the official Chinese documentation for the basic content:
Http://mybatis.github.io/mybatis-3/zh/configuration.html#typeHandlers
TypeHandlerIt's easy to write, but there's an important point to note.
Have you ever encountered an error similar to the following:
nullfor‘xxx‘. for the javaType / jdbcType combination specified.
If you encounter this problem, then this blog is just right for you. If you haven't met, you can avoid this problem.
Let's say the reason for the error, MyBatis, when looking for a type TypeHandler , call the following method:
Private<T> typehandler<t>Gettypehandler(Type type, Jdbctype jdbctype) {map<jdbctype, typehandler<?>> jdbchandlermap = Type_handler_map.get (TYPE); typehandler<?> handler =NULL;if(Jdbchandlermap! =NULL) {handler = Jdbchandlermap.get (Jdbctype);if(Handler = =NULL) {handler = Jdbchandlermap.get (NULL); } }if(Handler = =NULL&& Type! =NULL&& typeinstanceofClass && Enum.class.isAssignableFrom ((class<?>) type)) {handler =NewEnumtypehandler ((class<?>) type); }//Type drives generics here return(typehandler<t>) handler;}
In general, if we use the resulttypein Mapper.xml, or if we use the resultmap but the resultmap configuration does not specify a special type of field Jdbctype, the above error will occur.
It is easy to see from the source above that when Jdbctype is null:
handler = jdbcHandlerMap.get(jdbcType);ifnull) { handler = jdbcHandlerMap.get(null);}
jdbcHandlerMap.get(jdbcType)And the jdbcHandlerMap.get(null) effect is the same, the result is null , therefore, will report the above mentioned error.
How is this error solved?
If you configure only one Jdbctype from the above error, you will not be able to solve the problem when using resulttype .
From the source handler = jdbcHandlerMap.get(null); can be seen here, by default, if there is jdbctype, but handler=null will also try to remove a key for null the handler.
That is to say, in general, we TypeHandler need to have a key value for our own configuration key=null .
How do I set a null key? If you think it's an enumeration type, JdbcType.NULL it's wrong.
TypeHandlerThere are two ways to set it up null :
1.
@MappedJdbcTypesAnnotations
@Retention(RetentionPolicy.RUNTIME)@Target(ElementType.TYPE)public @interface MappedJdbcTypes { publicvalue(); booleandefaultfalse;}
The note contains a property includeNullJdbcType that is set to the property true .
2. XML configuration
The first point is that if you use the annotation configuration above, the configuration in the XML will jdbcType not work, and the configuration in the annotations will be fully used.
The XML configuration method is simple, that is, do not configure jdbcType properties, for example:
<!-- mybatis-config.xml --><typeHandlers> <typeHandler handler="org.mybatis.example.ExampleTypeHandler"/></typeHandlers>
In this case, a only Jdbctype is null configured .
In fact, most people may be configured so that they have not encountered this exception.
Mybatis Example of Typehandler