Created
June 13, 2017 13:23
-
-
Save twogood/cf4517a42cd62a97207ee571372f0776 to your computer and use it in GitHub Desktop.
ArgumentFactory to support LocalDate with @BindBean 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
import java.sql.Date; | |
import java.sql.PreparedStatement; | |
import java.sql.SQLException; | |
import java.sql.Types; | |
import java.time.LocalDate; | |
import org.skife.jdbi.v2.StatementContext; | |
import org.skife.jdbi.v2.tweak.Argument; | |
import org.skife.jdbi.v2.tweak.ArgumentFactory; | |
public class LocalDateArgumentFactory implements ArgumentFactory<LocalDate> { | |
@Override | |
public boolean accepts(Class<?> expectedType, Object value, StatementContext ctx) { | |
return value instanceof LocalDate; | |
} | |
@Override | |
public Argument build(Class<?> expectedType, LocalDate value, StatementContext ctx) { | |
return new Argument() { | |
@Override | |
public void apply(int position, PreparedStatement statement, StatementContext ctx) throws SQLException { | |
if (value == null) { | |
statement.setNull(position, Types.DATE); | |
} else { | |
statement.setDate(position, Date.valueOf(value)); | |
} | |
} | |
}; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
jdbi.registerArgumentFactory(new LocalDateArgumentFactory());