The first step is to implement the interface TypeHandler<T>
@MappedJdbcTypes(JdbcType.TIMESTAMP)//If this annotation is not used here, you need to write jdbctype="TIMESTAMP"@MappedTypes(DateTime.class)//If this annotation is not used here, you need to write javatype="com.sinosoft.sysframework.common.datatype.DateTime"public class DateTimeHandler implements TypeHandler<DateTime> {Step 2 Register Typehandler
There are two ways to register
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 the second registration method is used, the declarations of jdbctype and javatype can only be defined by annotation
Step 3: 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 the typehandler class is directly specified, you don’t need to write javatype and jdbctype, even if you write mybatis, you don’t need to.
2. Pass the parameters and query or modify them
<if test="txLogStatus != null">txLogStatus = #{txLogStatus, typeHandler=com.rrcp.util.typehandlers.EventStatusTypeHandler},</if>Similarly, the typehandler here can also be replaced with jdbcType and javaType
Summarize:
Try to keep it consistent or write it intact when registering and using it. If it is not unified, the corresponding typehandler will not be found and the error will be reported. Our purpose is to correctly use the typehandler processing mechanism. We do not want to delve into the algorithm used by mybatis in registering and searching, so as long as it is successful. If you have time and have time, you can study what consequences will be caused by registering less attributes. However, if you study them thoroughly, maybe the next version of myabtis will be changed.
The above is the three-step process of implementing a custom typehandler for Mybatis that I introduced to you. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support to Wulin.com website!