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();
    }
}
相关推荐
程序员大辉7 分钟前
免费的mysql链接工具HeidiSQL比Navicat好用
数据库·mysql
叫我詹躲躲10 分钟前
为什么永远不要让前端直接连接数据库
javascript·mysql
syty202011 分钟前
mysql--Waiting for table metadata lock
mysql
德育处主任19 分钟前
在亚马逊云上解决RDS、MariaDB 与 Aurora MySQL复制延迟实战指南
后端·mysql
cui_win26 分钟前
Redis 生产环境命令管控规范
数据库·redis·缓存
woshilys29 分钟前
oracle 和sql server 查询数据时锁的区别
数据库·oracle
if时光重来29 分钟前
kingbase数据库指定数据表自增id重置
数据库·python·sql
jingyucsdn33 分钟前
将postgresql结构和数据备份成sql语句
数据库·sql·postgresql
我科绝伦(Huanhuan Zhou)35 分钟前
Oracle等待事件:性能诊断与优化的核心指南
数据库·oracle
梦里不知身是客1137 分钟前
explain分析SQL语句分析sql语句的优劣
java·数据库·sql