当前位置: 移动技术网 > IT编程>开发语言>Java > MyBatis使用自定义TypeHandler转换类型的实现方法

MyBatis使用自定义TypeHandler转换类型的实现方法

2019年07月19日  | 移动技术网IT编程  | 我要评论

x58f,武汉晨报,香椿树

mybatis虽然有很好的sql执行性能,但毕竟不是完整的orm框架,不同的数据库之间sql执行还是有差异。 笔者最近在升级 oracle 驱动至 ojdbc 7 ,就发现了处理date类型存在问题。还好mybatis提供了使用自定义typehandler转换类型的功能。

本文介绍如下使用 typehandler 实现日期类型的转换。

问题背景

项目中有如下的字段,是采用的date类型:

birthday = #{birthday, jdbctype=date},

在更新 oracle 驱动之前,dateonlytypehandler会做出处理,将 jdbctype 是 date 的数据转为短日期格式(‘年月日')插入数据库。毕竟是生日嘛,只需要精确到年月日即可。

但是,升级 oracle 驱动至 ojdbc 7 ,就发现了处理date类型存在问题。插入的数据格式变成了长日期格式(‘年月日时分秒'),显然不符合需求了。

解决方案:

mybatis提供了使用自定义typehandler转换类型的功能。可以自己写个typehandler来对 date 类型做特殊处理:

/**
 * welcome to https://waylau.com
 */
package com.waylau.lite.mall.type;

import java.sql.callablestatement;
import java.sql.preparedstatement;
import java.sql.resultset;
import java.sql.sqlexception;
import java.text.dateformat;
import java.util.date;

import org.apache.ibatis.type.basetypehandler;
import org.apache.ibatis.type.jdbctype;
import org.apache.ibatis.type.mappedjdbctypes;
import org.apache.ibatis.type.mappedtypes;

/**
 * 自定义typehandler,用于将日期转为'yyyy-mm-dd'
 * 
 * @since 1.0.0 2018年10月10日
 * @author <a href="https://waylau.com" rel="external nofollow" >way lau</a>
 */
@mappedjdbctypes(jdbctype.date)
@mappedtypes(date.class)
public class dateshorttypehandler extends basetypehandler<date> {

 @override
 public void setnonnullparameter(preparedstatement ps, int i, date parameter, jdbctype jdbctype)
  throws sqlexception {
 dateformat df = dateformat.getdateinstance();
 string datestr = df.format(parameter);
 ps.setdate(i, java.sql.date.valueof(datestr));
 }

 @override
 public date getnullableresult(resultset rs, string columnname) throws sqlexception {
 java.sql.date sqldate = rs.getdate(columnname);
 if (sqldate != null) {
  return new date(sqldate.gettime());
 }
 return null;
 }

 @override
 public date getnullableresult(resultset rs, int columnindex) throws sqlexception {
 java.sql.date sqldate = rs.getdate(columnindex);
 if (sqldate != null) {
  return new date(sqldate.gettime());
 }
 return null;
 }

 @override
 public date getnullableresult(callablestatement cs, int columnindex) throws sqlexception {
 java.sql.date sqldate = cs.getdate(columnindex);
 if (sqldate != null) {
  return new date(sqldate.gettime());
 }
 return null;
 }

}

如果是 spring 项目,以下面方式进行 typehandler 的配置:

<!-- 自定义 -->
<!--声明typehandler bean-->
<bean id="dateshorttypehandler" class="com.waylau.lite.mall.type.dateshorttypehandler"/>

<!-- mybatis 工厂 -->
<bean id="sqlsessionfactory" class="org.mybatis.spring.sqlsessionfactorybean">
 <property name="datasource" ref="datasource" />
 
 <!--typehandler注入-->
 <property name="typehandlers" ref="dateshorttypehandler"/>
</bean>

如何使用 typehandler

方式1 :指定 jdbctype 为 date

比如,目前,项目中有如下的字段,是采用的date类型:

birthday = #{birthday, jdbctype=date},

方式2 :指定 typehandler

指定 typehandler 为我们自定义的 typehandler:

birthday = #{birthday, typehandler=com.waylau.lite.mall.type.dateshorttypehandler},

源码

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网