fix: 修复mysql驱动更新后,日期类型bug

This commit is contained in:
lhc
2025-12-03 10:22:15 +08:00
parent a49f1f3079
commit 106b80c2f4
2 changed files with 63 additions and 1 deletions

View File

@@ -9,7 +9,7 @@
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>hcframe-base</artifactId>
<version>1.2.5-SNAPSHOT</version>
<version>1.2.5.1-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>

View File

@@ -0,0 +1,62 @@
package com.taixingyiji.base.module.data.module;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;
import org.apache.ibatis.type.MappedTypes;
import org.apache.ibatis.type.TypeHandler;
import java.sql.*;
import java.time.LocalDateTime;
import java.util.Date;
/**
* @className DateHandler
* @author lhc
* @date 2025年12月03日 9:56
* @description 描述
* @version 1.0
*/
@MappedJdbcTypes(JdbcType.TIMESTAMP)
@MappedTypes({Date.class, LocalDateTime.class})
public class DateHandler implements TypeHandler<Date> {
@Override
public void setParameter(PreparedStatement ps, int i, Date parameter, JdbcType jdbcType) throws SQLException {
if (parameter == null) {
ps.setNull(i, Types.TIMESTAMP);
} else {
ps.setTimestamp(i, new Timestamp(parameter.getTime()));
}
}
@Override
public Date getResult(ResultSet rs, String columnName) throws SQLException {
return convert(rs.getObject(columnName));
}
@Override
public Date getResult(ResultSet rs, int columnIndex) throws SQLException {
return convert(rs.getObject(columnIndex));
}
@Override
public Date getResult(CallableStatement cs, int columnIndex) throws SQLException {
return convert(cs.getObject(columnIndex));
}
private Date convert(Object obj) throws SQLException {
if (obj == null) return null;
if (obj instanceof Timestamp) {
return new Date(((Timestamp) obj).getTime());
}
if (obj instanceof LocalDateTime) {
return Timestamp.valueOf((LocalDateTime) obj);
}
if (obj instanceof Date) {
return (Date) obj;
}
throw new SQLException("Unsupported datetime type: " + obj.getClass());
}
}