Skip to content

Instantly share code, notes, and snippets.

@making
Last active August 29, 2015 14:11
Show Gist options
  • Save making/021cdc80293da5ffbb6b to your computer and use it in GitHub Desktop.
Save making/021cdc80293da5ffbb6b to your computer and use it in GitHub Desktop.
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import java.beans.PropertyDescriptor;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
public class Jsr310SupportedBeanPropertyRowMapper<T> extends BeanPropertyRowMapper<T> {
@Override
protected Object getColumnValue(ResultSet rs, int index, PropertyDescriptor pd) throws SQLException {
Class<?> requiredType = pd.getPropertyType();
Object value = null;
if (LocalDateTime.class.equals(requiredType)) {
value = rs.getTimestamp(index).toLocalDateTime();
} else if (LocalDate.class.equals(requiredType)) {
value = rs.getDate(index).toLocalDate();
} else if (LocalTime.class.equals(requiredType)) {
value = rs.getTime(index).toLocalTime();
}
if (value == null) {
return super.getColumnValue(rs, index, pd);
} else {
return (rs.wasNull() ? null : value);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment