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();
    }
}
相关推荐
꯭自꯭闭꯭15 分钟前
达梦事物特性及MVCC
linux·运维·数据库
小马同学-41 分钟前
MySQL主从复制和读写分离
数据库·mysql
谢亮_vipxieliang42 分钟前
Spring 事务失效的常见场景
java·开发语言·数据库·spring boot
海绵宝宝转agent1 小时前
MySql高频面试八股开源笔记总结
mysql·面试·开源
geovindu2 小时前
sql: JSON and XML Data Handling in SQL using sql server 2025
大数据·数据库·sqlserver·数据库开发·数据库架构
IT大白鼠2 小时前
图数据库系列 · 第 02 篇——架构拆解:原生图存储到因果集群
数据库·架构·nosql
余槐i2 小时前
数据没回滚也不报错:@Transactional 自调用失效的 3 种复现与修复
spring boot·mysql·多线程·spring 事务
EatFan3 小时前
从“框架混战“到“运行时收敛“:2026 年 AI Agent 开发框架的三条路线之争
java·数据库·人工智能·多智能体·ai agent·mcp·agent 框架
小蒜学长3 小时前
基于SpringBoot的佳新超市管理系统设计与实现系统(代码+数据库+LW)
java·数据库·spring boot·后端·佳新超市管理系统
jinyishu_3 小时前
向量数据库入门:记录结构、索引、检索与选型
数据库