|
@@ -9,49 +9,57 @@ import org.springframework.stereotype.Component;
|
|
|
import java.sql.*;
|
|
|
import java.time.LocalDateTime;
|
|
|
import java.time.ZoneId;
|
|
|
+import java.time.format.DateTimeFormatter;
|
|
|
|
|
|
@MappedTypes(LocalDateTime.class)
|
|
|
-@MappedJdbcTypes(JdbcType.TIMESTAMP)
|
|
|
+@MappedJdbcTypes(JdbcType.VARCHAR) // 关键:对应数据库 VARCHAR
|
|
|
@Component
|
|
|
public class LocalDateTimeHandler extends BaseTypeHandler<LocalDateTime> {
|
|
|
|
|
|
+ private static final DateTimeFormatter FMT = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
|
|
|
+
|
|
|
+ /* ====== 写入:LocalDateTime -> VARCHAR ====== */
|
|
|
@Override
|
|
|
- public void setNonNullParameter(PreparedStatement ps, int i, LocalDateTime parameter, JdbcType jdbcType) throws SQLException {
|
|
|
- ps.setTimestamp(i, java.sql.Timestamp.valueOf(parameter));
|
|
|
+ public void setNonNullParameter(PreparedStatement ps, int i,
|
|
|
+ LocalDateTime parameter, JdbcType jdbcType) throws SQLException {
|
|
|
+ ps.setString(i, parameter.format(FMT));
|
|
|
}
|
|
|
|
|
|
+ /* 允许 null */
|
|
|
@Override
|
|
|
- public void setParameter(PreparedStatement ps, int i, LocalDateTime parameter, JdbcType jdbcType) throws SQLException {
|
|
|
+ public void setParameter(PreparedStatement ps, int i,
|
|
|
+ LocalDateTime parameter, JdbcType jdbcType) throws SQLException {
|
|
|
if (parameter == null) {
|
|
|
- // 允许数据库使用默认值
|
|
|
- ps.setNull(i, Types.TIMESTAMP);
|
|
|
+ ps.setNull(i, Types.VARCHAR);
|
|
|
} else {
|
|
|
super.setParameter(ps, i, parameter, jdbcType);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /* ====== 读取:VARCHAR -> LocalDateTime ====== */
|
|
|
@Override
|
|
|
public LocalDateTime getNullableResult(ResultSet rs, String columnName) throws SQLException {
|
|
|
- java.sql.Timestamp timestamp = rs.getTimestamp(columnName);
|
|
|
- return convertToLocalDateTime(timestamp);
|
|
|
+ return strToLocalDateTime(rs.getString(columnName));
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public LocalDateTime getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
|
|
|
- java.sql.Timestamp timestamp = rs.getTimestamp(columnIndex);
|
|
|
- return convertToLocalDateTime(timestamp);
|
|
|
+ return strToLocalDateTime(rs.getString(columnIndex));
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public LocalDateTime getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
|
|
|
- java.sql.Timestamp timestamp = cs.getTimestamp(columnIndex);
|
|
|
- return convertToLocalDateTime(timestamp);
|
|
|
+ return strToLocalDateTime(cs.getString(columnIndex));
|
|
|
}
|
|
|
|
|
|
- private LocalDateTime convertToLocalDateTime(java.sql.Timestamp timestamp) {
|
|
|
- if (timestamp == null) return null;
|
|
|
- return timestamp.toInstant().atZone(ZoneId.of("UTC"))
|
|
|
- .withZoneSameInstant(ZoneId.of(TimezoneContextHolder.getTimeZone()))
|
|
|
- .toLocalDateTime();
|
|
|
+ private LocalDateTime strToLocalDateTime(String val) {
|
|
|
+ if (val == null || val.trim().isEmpty()) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ // 按自己的格式解析
|
|
|
+ return LocalDateTime.parse(val.trim(), FMT)
|
|
|
+ .atZone(ZoneId.of("UTC"))
|
|
|
+ .withZoneSameInstant(ZoneId.of(TimezoneContextHolder.getTimeZone()))
|
|
|
+ .toLocalDateTime();
|
|
|
}
|
|
|
}
|