The first step is to implement Interface typehandler<t>
@MappedJdbcTypes (Jdbctype.timestamp)//here if you do not use this annotation, you need to specify jdbctype= "TIMESTAMP" when registering the Typehandler in Myabtis-config.xml.
@MappedTypes (Datetime.class)//here if you do not use this annotation, you need to specify javatype= when registering the Typehandler in Myabtis-config.xml. Com.sinosoft.sysframework.common.datatype.DateTime "Public
class Datetimehandler implements typehandler< datetime> {
Second Step registration Typehandler
There are two ways of registering
One is a single registration
<typeHandlers>
<typehandler handler= "com.***. Dateatimetypehandler "Javatype=" [Ljava.lang.String; "jdbctype=" VARCHAR "/>
</typeHandlers>
One is batch Registration
<mappers>
<mapper resource= "Com/tiantian/mybatis/mapper/usermapper.xml"/>
<package name= " Com.tiantian.mybatis.mapperinterface "/>
</mappers>
If you use the second registration method, the declarations of Jdbctype and javatype can only be defined in terms of annotations
Step Three: Use:
1. Query result set field definition
<resultmap id= "Userresult" type= "User" >
<id column= "id" property= "id"/> <result "column=
" Interests "property=" interests "javatype=" [Ljava.lang.String; "jdbctype=" VARCHAR]/>
</resultMap>
Or
<resultmap id= "Userresult" type= "User" >
<id column= "id" property= "id"/> <result "column=
" Interests "property=" interests "typehandler=" Com.tiantian.mybatis.handler.StringArrayTypeHandler "/>
< /resultmap>
That is, if you directly indicate the Typehandler class can not write Javatype and jdbctype, even if you write mybatis do not need
2, transfer parameters query or modify
<if test= "txlogstatus!= null" >
txlogstatus = #{txlogstatus, typehandler= Com.rrcp.util.typehandlers.EventStatusTypeHandler},
</if>
Similarly, Typehandler can also be replaced with Jdbctype and Javatype.
Summarize:
When the registration and use when try to keep consistent or write complete, if not unified, will not find the corresponding Typehandler so that the error, our aim is to correctly use Typehandler processing mechanism, and do not want to delve into the mybatis in the registration and find the use of algorithms, So as long as the success can be. Have time idle egg pain of the brother can study, less registration of a property, what will happen, but if you study through, maybe Myabtis's next version of the change.
The above is a small set to introduce the MyBatis to realize the custom Typehandler three steps, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!