Jooq目前不支持JSR 310类型,支持将在v3.8之后才会到来。
通常使用简单的转换器是可行的,但对于某些类型,例如postgres的TIMESTAMP WITH TIME ZONE
,需要自定义绑定。因此,我尝试编写了一个,但生成的XxxRecord
类仍然在我的DB中的TIMESTAMP WITH TIME ZONE
字段中使用Timestamp
数据类型。
我需要更改下面的代码以在jooq生成的类中将postgres的TIMESTAMP WITH TIME ZONE
视为Instant
吗?
转换器
public class TimestampConverter implements Converter<Timestamp, Instant> {
@Override public Instant from(Timestamp ts) {
return ts == null ? null : ts.toInstant();
}
@Override public Timestamp to(Instant instant) {
return instant == null ? null : Timestamp.from(instant);
}
@Override public Class<Timestamp> fromType() { return Timestamp.class; }
@Override public Class<Instant> toType() { return Instant.class; }
}
自定义绑定
public class TimestampBinding implements Binding<Timestamp, Instant> {
private static final Converter<Timestamp, Instant> converter = new TimestampConverter();
private final DefaultBinding<Timestamp, Instant> delegate =
new DefaultBinding<> (converter());
@Override public Converter<Timestamp, Instant> converter() { return converter; }
@Override public void sql(BindingSQLContext<Instant> ctx) throws SQLException {
delegate.sql(ctx);
}
//etc. same for all other overriden methods.
}
pom.xml(提取)
<customType>
<name>java.time.Instant</name>
<type>java.time.Instant</type>
<binding>xxx.TimestampBinding</binding>
</customType>
...
<forcedType>
<name>java.time.Instant</name>
<types>timestamp with time zone</types>
</forcedType>