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();
    }
}
相关推荐
阿里小阿希9 分钟前
CentOS7 PostgreSQL 9.2 升级到 15 完整教程
数据库·postgresql
荒川之神14 分钟前
Oracle 数据仓库雪花模型设计(完整实战方案)
数据库·数据仓库·oracle
做个文艺程序员25 分钟前
MySQL安全加固十大硬核操作
数据库·mysql·安全
不吃香菜学java33 分钟前
Redis简单应用
数据库·spring boot·tomcat·maven
一个天蝎座 白勺 程序猿44 分钟前
Apache IoTDB(15):IoTDB查询写回(INTO子句)深度解析——从语法到实战的ETL全链路指南
数据库·apache·etl·iotdb
不知名的老吴1 小时前
Redis的延迟瓶颈:TCP栈开销无法避免
数据库·redis·缓存
YOU OU1 小时前
三大范式和E-R图
数据库
MaCa .BaKa1 小时前
47-心里健康咨询平台/心理咨询系统
java·spring boot·mysql·tomcat·maven·intellij-idea·个人开发
一江寒逸1 小时前
零基础从入门到精通MySQL(上篇):筑基篇——吃透核心概念与基础操作,打通SQL入门第一关
数据库·sql·mysql
@土豆1 小时前
Ubuntu 22.04 运行 Filebeat 7.11.2 崩溃问题分析及解决文档
linux·数据库·ubuntu