错误: org.springframework.dao.InvalidDataAccessApiUsageException: Error attempting to get column 'expirationTime' from result set. Cause: java.sql.SQLFeatureNotSupportedException: getObject with type ; getObject with type; nested exception is java.sql.SQLFeatureNotSupportedException: getObject with type
解决:
改写LocalDateTime处理程序
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;
import org.apache.ibatis.type.MappedTypes;
import org.springframework.stereotype.Component;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
@Component
@MappedTypes(LocalDateTime.class)
@MappedJdbcTypes(value = JdbcType.TIMESTAMP, includeNullJdbcType = true)
public class MyLocalDateTimeTypeHandler extends BaseTypeHandler<LocalDateTime> {
private final DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
@Override
public void setNonNullParameter(PreparedStatement preparedStatement, int i, LocalDateTime localDateTime, JdbcType jdbcType) throws SQLException {
if (localDateTime != null) {
preparedStatement.setString(i, dateTimeFormatter.format(localDateTime));
}
}
@Override
public LocalDateTime getNullableResult(ResultSet resultSet, String columnName) throws SQLException {
String target = resultSet.getString(columnName);
if (StringUtils.isEmpty(target)) {
return null;
}
return LocalDateTime.parse(target, dateTimeFormatter);
}
@Override
public LocalDateTime getNullableResult(ResultSet resultSet, int columnIndex) throws SQLException {
String target = resultSet.getString(columnIndex);
if (StringUtils.isEmpty(target)) {
return null;
}
return LocalDateTime.parse(target, dateTimeFormatter);
}
@Override
public LocalDateTime getNullableResult(CallableStatement callableStatement, int columnIndex) throws SQLException {
String target = callableStatement.getString(columnIndex);
if (StringUtils.isEmpty(target)) {
return null;
}
return LocalDateTime.parse(target, dateTimeFormatter);
}
}
改写LocalDate处理程序
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedTypes;
import org.springframework.stereotype.Component;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.LocalDate;
import java.util.Optional;
@Component
@MappedTypes(LocalDate.class)
public class LocalDateTypeHandler extends BaseTypeHandler<LocalDate> {
public LocalDateTypeHandler() {
}
@Override
public void setNonNullParameter(PreparedStatement preparedStatement, int i, LocalDate localDate, JdbcType jdbcType) throws SQLException {
preparedStatement.setObject(i, (int)localDate.atStartOfDay(ZoneOffset.of("+8")).toInstant().getEpochSecond());
}
@Override
public LocalDate getNullableResult(ResultSet resultSet, String s) throws SQLException {
return this.parseValue(resultSet.getObject(s));
}
@Override
public LocalDate getNullableResult(ResultSet resultSet, int i) throws SQLException {
return this.parseValue(resultSet.getObject(i));
}
@Override
public LocalDate getNullableResult(CallableStatement callableStatement, int i) throws SQLException {
return this.parseValue(callableStatement.getObject(i));
}
private LocalDate parseValue(Object value) {
return Optional.ofNullable(value).map(String::valueOf).filter((v) ->
v.length() > 0
).map(Long::parseLong).map(DateUtil::ofLocalDate).orElse( null);
}
}
版权声明:本文为hehe8881原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。