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();
    }
}
相关推荐
IvorySQL43 分钟前
PostgreSQL 性能:云端与本地的延迟分析
数据库·postgresql
wangbing112544 分钟前
分组取前几位
数据库
给我来一根1 小时前
用户认证与授权:使用JWT保护你的API
jvm·数据库·python
林鸿群1 小时前
ubuntu 26.04 安装mysql-server
linux·mysql·ubuntu
_F_y1 小时前
MySQL表的操作
android·数据库·mysql
SmartBrain2 小时前
Agent 知识总结
服务器·数据库·笔记
千寻技术帮2 小时前
10336_基于SSM的少数民族文化商城
mysql·毕业设计·ssm·非遗商城
fenglllle2 小时前
MySQL explain format的差异
数据库·mysql
哈哈不让取名字3 小时前
用Pygame开发你的第一个小游戏
jvm·数据库·python
程序员敲代码吗3 小时前
Python异步编程入门:Asyncio库的使用
jvm·数据库·python