mysql 数据库datetime 类型,转换为DO里面的long类型后,只剩下年了,没有了月和日

解决方法也简单:

自定义个一个 Date2LongTypeHandler

复制代码
    <resultMap id="BeanResult" type="XXXX.XXXXDO">
        <result column="gmt_create" property="gmtCreate" jdbcType="DATE" javaType="java.lang.Long"
                typeHandler="XXXX.Date2LongTypeHandler"/>
        <result column="gmt_modified" property="gmtModified" jdbcType="DATE" javaType="java.lang.Long"
                typeHandler="XXXXX.Date2LongTypeHandler"/>
    </resultMap>

Date2LongTypeHandler类实现如下:

java 复制代码
public class Date2LongTypeHandler extends BaseTypeHandler<Long> {
    @Override
    public void setNonNullParameter(PreparedStatement ps, int i, Long date, JdbcType jdbcType) throws SQLException {
        ps.setDate(i, new java.sql.Date(date));
    }

    @Override
    public Long getNullableResult(ResultSet resultSet, String columnName) throws SQLException {
        return toLong(resultSet.getDate(columnName));
    }

    @Override
    public Long getNullableResult(ResultSet resultSet, int i) throws SQLException {
        return toLong(resultSet.getDate(i));
    }

    @Override
    public Long getNullableResult(CallableStatement cs, int i) throws SQLException {
        return toLong(cs.getDate(i));
    }

    private Long toLong(Date time) {
        if (time == null) {
            return null;
        }
        return time.getTime();
    }
}
相关推荐
笨笨饿1 小时前
#102_Codex无在VSCold无法打开
java·c语言·数据库·笔记
向夏威夷 梦断明暄1 小时前
从架构特点到功能缺陷,重新认识分析型分布式数据库
数据库·分布式·架构
Mico181 小时前
MySQL 8.0.35 主从延迟模拟与解决
mysql
不知疲倦的仄仄2 小时前
MySQL/Read View快照/MVCC/串行化
java·数据库·mysql
向夏威夷 梦断明暄2 小时前
C# 弃元模式:从语法糖到性能利器的深度解析
服务器·数据库·c#
糖果店的幽灵2 小时前
大模型测评DeepEval快速入门-RAG指标详解
数据库·人工智能·langgraph·大模型测评·deepeval
whn19773 小时前
oracle的节点2无法启动asm实例 提示PMON terminating the instance due to error 481
数据库·oracle·wpf
Mico183 小时前
MySQL 5.7.35 升级到 8.0.35 — 原地升级(主从架构方式)
mysql
AAA@峥4 小时前
Ceph 集群配置管理完整指南
运维·数据库·分布式·ceph