广告
返回顶部
首页 > 资讯 > 后端开发 > JAVA >MyBatis实现 Java 对象和数据库中日期类型之间的转换(超详细)
  • 583
分享到

MyBatis实现 Java 对象和数据库中日期类型之间的转换(超详细)

mybatisjava数据库springdatabase 2023-09-23 12:09:41 583人浏览 安东尼
摘要

背景 数据库存储的时间字段的类型是datetime Java实体类的时间字段类型是Date 需求:响应前端的时间字段格式为”yyyy-MM-dd HH:mm:ss“ 步骤 1、定义resultMap

背景

数据库存储的时间字段的类型是datetime
Java实体类的时间字段类型是Date
需求:响应前端的时间字段格式为”yyyy-MM-dd HH:mm:ss“

步骤

1、定义resultMap

定义 Java 对象和数据库表字段的对应关系,在 mapper.xml 文件中使用 #{属性名,jdbcType=数据库字段类型} 来进行参数传递和结果集映射,例如:

<resultMap id="userResultMap" type="User">    <id column="id" property="id" jdbcType="INTEGER"/>    <result column="name" property="name" jdbcType="VARCHAR"/>    <result column="age" property="age" jdbcType="INTEGER"/>    <result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>resultMap>

2、实体类时间字段上添加JSONFORMat注解

@jsonFormat日期格式化你的pattern格式,timezone设置时区

@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")private Date createTime;

:时区也可以在application.yml或application.propertites配置文件中添加,一劳永逸,@JsonFormat就不需要配置timezone了

spring:  jackson:    time-zone: Asia/Shanghai

特殊解决方式如下

步骤1和2没有解决的话,继续如下三个步骤

3、自定义类型处理器(mybatis支持)

public class MyDateTypeHandler implements TypeHandler<Date> {    private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");    @Override    public void setParameter(PreparedStatement ps, int i, Date date, JdbcType jdbcType) throws sqlException {        if (date == null) {            ps.setNull(i, jdbcType.TYPE_CODE);        } else {            ps.setString(i, sdf.format(date));        }    }    @Override    public Date getResult(ResultSet rs, String columnName) throws SQLException {        Timestamp ts = rs.getTimestamp(columnName);        return ts == null ? null : new Date(ts.getTime());    }    @Override    public Date getResult(ResultSet rs, int columnIndex) throws SQLException {        Timestamp ts = rs.getTimestamp(columnIndex);        return ts == null ? null : new Date(ts.getTime());    }    @Override    public Date getResult(CallableStatement cs, int columnIndex) throws SQLException {        Timestamp ts = cs.getTimestamp(columnIndex);        return ts == null ? null : new Date(ts.getTime());    }}

4、将类型处理器Bean注入容器(在MyBatis配置类中)

@Configuration@MapperScan({"com.example.mapper"})public class MyBatisConfig {    @Bean    public TypeHandler<Date> myDateTypeHandler() {        return new MyDateTypeHandler();    }}

5、mapper.xml文件中修改

<resultMap id="myResultMap" type="com.example.MyResultType">  <id property="id" column="id" />  <result property="createTime" column="create_time" typeHandler="com.example.MyDateTypeHandler"/>resultMap>

结果SUCCESS

在这里插入图片描述

结束语:当你在做你热爱的事情时,你永远不会感到疲惫。

来源地址:https://blog.csdn.net/Da_zhenzai/article/details/129532255

--结束END--

本文标题: MyBatis实现 Java 对象和数据库中日期类型之间的转换(超详细)

本文链接: https://www.lsjlt.com/news/416435.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作