package com.ruoyi.common.handler; import org.apache.ibatis.type.BaseTypeHandler; import org.apache.ibatis.type.JdbcType; import org.apache.ibatis.type.MappedTypes; import java.sql.*; import java.time.LocalDateTime; @MappedTypes(LocalDateTime.class) public class LocalDateTimeTypeHandler extends BaseTypeHandler { @Override public void setNonNullParameter(PreparedStatement ps, int i, LocalDateTime parameter, JdbcType jdbcType) throws SQLException { ps.setObject(i, parameter); } @Override public LocalDateTime getNullableResult(ResultSet rs, String columnName) throws SQLException { Object value = rs.getObject(columnName); if (value == null) { return null; } if (value instanceof Timestamp) { return ((Timestamp) value).toLocalDateTime(); } else if (value instanceof LocalDateTime) { return (LocalDateTime) value; } // 特殊处理:如果数据库返回的是字符串 return LocalDateTime.parse(value.toString()); } @Override public LocalDateTime getNullableResult(ResultSet rs, int columnIndex) throws SQLException { Object value = rs.getObject(columnIndex); if (value == null) { return null; } if (value instanceof Timestamp) { return ((Timestamp) value).toLocalDateTime(); } else if (value instanceof LocalDateTime) { return (LocalDateTime) value; } return LocalDateTime.parse(value.toString()); } @Override public LocalDateTime getNullableResult(CallableStatement cs, int columnIndex) throws SQLException { Object value = cs.getObject(columnIndex); if (value == null) { return null; } if (value instanceof Timestamp) { return ((Timestamp) value).toLocalDateTime(); } else if (value instanceof LocalDateTime) { return (LocalDateTime) value; } return LocalDateTime.parse(value.toString()); } }