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();
    }
}
相关推荐
2301_7815714212 小时前
JavaScript中Object-getOwnPropertySymbols获取方法
jvm·数据库·python
jump_jump13 小时前
Drizzle 凭什么贴着 Go 跑——从设计哲学到热路径源码
数据库·性能优化·orm
jay神13 小时前
基于SpringBoot的宠物生命周期信息管理系统
java·数据库·spring boot·后端·web开发·宠物·管理系统
秋913 小时前
MySQL 8.0.46 与 MySQL 9.7.0在sql语句方面的区别并举例说明
数据库·sql·mysql
一只数据集13 小时前
NVIDIA Nemotron AIQ Agentic Safety Dataset:面向企业级智能体系统的安全与防护评估数据集全面解析
网络·数据库·安全
Amazinqc13 小时前
Mysql数据库数据软隔离的并发死锁情况
数据库·mysql·死锁
DianSan_ERP13 小时前
京东订单接口集成中如何处理消费者敏感信息的安全与合规问题?
前端·数据库·后端·团队开发·运维开发
原来是猿13 小时前
TCP Echo Server 深度解析:从单进程到线程池的演进之路(中)
linux·服务器·数据库
treesforest13 小时前
IP地址段查询完全指南:从单IP查到IPv4段批量归属地查询
网络·数据库·网络协议·tcp/ip·网络安全·运维开发
fTiN CAPA14 小时前
Linux系统离线部署MySQL详细教程(带每步骤图文教程)
linux·mysql·adb