DBFlute で MySQL のDATE型をJoda-TimeのLocalDateにマッピングする
Java
TnAbstractValueType を継承したValueTypeを作り、java.sql.Dateとorg.joda.time.LocalDateの変換を実装する
DBFluteInitializerを継承し、prologue()をオーバーライドして、DBFluteConfig#registerBasicValueType() でLocalDateに対するValueTypeを登録する
dfprop
typeMappingMap.dfprop で、DATEをorg.joda.time.LocalDateにマッピングする
littleAdjustmentMap.dfpropのextendedDBFluteInitializerClassに、拡張したDBFluteInitializerを設定する
includeQueryMap.dfpropでFromToとDateFromToの生成を抑制する(FromToがjava.util.Dateを使ってしまいコンパイルエラーになるため)
public class JodaDateType extends TnAbstractValueType {
public JodaDateType () {
super (Types .DATE );
}
@ Override
public Object getValue (final ResultSet rs , final int index ) throws SQLException {
final Date date = rs .getDate (index );
return date == null ? null : LocalDate .fromDateFields (date );
}
@ Override
public Object getValue (final ResultSet rs , final String columnName ) throws SQLException {
final Date date = rs .getDate (columnName );
return date == null ? null : LocalDate .fromDateFields (date );
}
@ Override
public Object getValue (final CallableStatement cs , final int index ) throws SQLException {
final Date date = cs .getDate (index );
return date == null ? null : LocalDate .fromDateFields (date );
}
@ Override
public Object getValue (final CallableStatement cs , final String parameterName ) throws SQLException {
final Date date = cs .getDate (parameterName );
return date == null ? null : LocalDate .fromDateFields (date );
}
@ Override
public void bindValue (final Connection conn , final PreparedStatement ps , final int index , final Object value ) throws SQLException {
if (value == null ) {
this .setNull (ps , index );
} else {
ps .setDate (index , this .toSqlDate (value ));
}
}
@ Override
public void bindValue (final Connection conn , final CallableStatement cs , final String parameterName , final Object value ) throws SQLException {
if (value == null ) {
this .setNull (cs , parameterName );
} else {
cs .setDate (parameterName , this .toSqlDate (value ));
}
}
private java .sql .Date toSqlDate (final Object value ) {
assert value != null ;
if (value instanceof ReadablePartial ) {
return new java .sql .Date (((ReadablePartial ) value ).toDateTime (null ).withTimeAtStartOfDay ().getMillis ());
} else if (value instanceof ReadableInstant ) {
return new java .sql .Date (((ReadableInstant ) value ).getMillis ());
}
return DfTypeUtil .toSqlDate (value );
}
}
public class ExtendedDBFluteInitializer extends DBFluteInitializer {
private static final ValueType DATE_TYPE = new JodaDateType();
public ExtendedDBFluteInitializer(final DataSource dataSource) {
super(dataSource);
}
@Override
protected void prologue() {
super.prologue();
final DBFluteConfig config = DBFluteConfig.getInstance();
config.registerBasicValueType(LocalDate.class, DATE_TYPE);
}
}
map:{
; NUMERIC = $$AutoMapping$$ ; DECIMAL = $$AutoMapping$$
; DATE = org.joda.time.LocalDate
}
littleAdjustmentMap.dfprop
...
; extendedDBFluteInitializerClass = com.example.ExtendedDBFluteInitializer
...
...
; Date = map:{
; NotEqual = map:{}
; InScope = map:{}
; NotInScope = map:{}
; FromTo = map:{}
; DateFromTo = map:{}
}
...