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

2025-05-29 0 85

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

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

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

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 MyBatis使用自定义TypeHandler转换类型的实现方法 https://www.kuaiidc.com/111027.html

相关文章

发表评论
暂无评论