Created
June 13, 2017 13:26
-
-
Save twogood/04048c7f2de390ab68249970f1fd1cf0 to your computer and use it in GitHub Desktop.
Using LocalDate fields with BeanMapper in JDBI
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
// | |
// USAGE: | |
// | |
// jdbi.registerColumnMapper(new LocalDateColumnMapper()); | |
// | |
import java.sql.Date; | |
import java.sql.ResultSet; | |
import java.sql.SQLException; | |
import java.time.LocalDate; | |
import org.skife.jdbi.v2.StatementContext; | |
import org.skife.jdbi.v2.tweak.ResultColumnMapper; | |
public class LocalDateColumnMapper implements ResultColumnMapper<LocalDate> { | |
@Override | |
public LocalDate mapColumn(ResultSet r, int columnNumber, StatementContext ctx) throws SQLException { | |
return toLocalDate(r.getDate(columnNumber)); | |
} | |
@Override | |
public LocalDate mapColumn(ResultSet r, String columnLabel, StatementContext ctx) throws SQLException { | |
return toLocalDate(r.getDate(columnLabel)); | |
} | |
private static LocalDate toLocalDate(Date date) { | |
return (date == null) ? null : date.toLocalDate(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment