Last active
August 29, 2015 14:11
-
-
Save making/021cdc80293da5ffbb6b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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