服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|JavaScript|易语言|

服务器之家 - 编程语言 - Java教程 - MyBatis使用自定义TypeHandler转换类型的实现方法

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

2021-06-04 14:09waylau Java教程

这篇文章主要介绍了MyBatis使用自定义TypeHandler转换类型的实现方法,本文介绍使用TypeHandler 实现日期类型的转换,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

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

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

问题背景

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

?
1
birthday = #{birthday, jdbctype=date},

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

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

解决方案:

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/**
 * 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 的配置:

?
1
2
3
4
5
6
7
8
9
10
11
<!-- 自定义 -->
<!--声明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类型:

?
1
birthday = #{birthday, jdbctype=date},

方式2 :指定 typehandler

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

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

源码

https://github.com/waylau/lite-book-mall

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://my.oschina.net/waylau/blog/2243801

延伸 · 阅读

精彩推荐