MyBatis realizes the custom Typehandler three steps _java

Source: Internet
Author: User

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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.