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();
    }
}
相关推荐
Ai拆代码的曹操32 分钟前
MySQL GROUP BY 性能优化:执行计划分析 + 索引重构实战
mysql·性能优化·重构
枫叶v.1 小时前
Prompt Injection 防不住怎么办?从 Source-Sink 模型设计 Agent 安全边界
数据库·安全·prompt
Lucky_Turtle1 小时前
【Milvus】向量数据库
数据库
吴声子夜歌2 小时前
MongoDB 8.0——安全性
数据库·mongodb
xcLeigh2 小时前
KingbaseES 的卢智能运维体架构深度拆解
运维·数据库·人工智能·ai·架构·ffmpeg·智能体
weixin_397574093 小时前
按行业定制分析视角
大数据·数据库·人工智能·企业数据治理·数据驱动决策·本体语义·企业经营分析
鹿角片ljp3 小时前
Redis 深度复习:从入门到面试通关
数据库·redis·面试
Ming_studying3 小时前
Python + SQLite FTS5 构建本地文档全文搜索器:增量索引、中文检索与高亮
jvm·数据库·python·sqlite
云和恩墨4 小时前
从静态备份到极速恢复:zData S重构多元数据库时代的“第二存储”底座
数据库·重构
吴声子夜歌4 小时前
MongoDB 8.0——存储
数据库·mongodb