Java Jooq对“的约束力”;带时区的时间戳“;输入博士后
和 使用简单的转换器通常有效,但某些类型除外,例如postgres的时区时间戳需要自定义绑定。因此,我试图编写一个,但生成的Java Jooq对“的约束力”;带时区的时间戳“;输入博士后,java,postgresql,java-8,jooq,java-time,Java,Postgresql,Java 8,Jooq,Java Time,和 使用简单的转换器通常有效,但某些类型除外,例如postgres的时区时间戳需要自定义绑定。因此,我试图编写一个,但生成的XxxRecord类仍然使用时间戳数据类型作为数据库中带时区的时间戳字段 我需要在下面的代码中更改什么才能在jooq生成的类中将postgres的带有时区的时间戳视为即时 转换器 public类TimestampConverter实现转换器{ @覆盖来自的公共即时消息(时间戳ts){ 返回ts==null?null:ts.toInstant(); } @将公共时间戳覆盖为(
XxxRecord
类仍然使用时间戳
数据类型作为数据库中带时区的时间戳字段
我需要在下面的代码中更改什么才能在jooq生成的类中将postgres的带有时区的时间戳
视为即时
转换器
public类TimestampConverter实现转换器{
@覆盖来自的公共即时消息(时间戳ts){
返回ts==null?null:ts.toInstant();
}
@将公共时间戳覆盖为(即时){
return instant==null?null:Timestamp.from(instant);
}
@重写公共类fromType(){return Timestamp.Class;}
@重写公共类toType(){return Instant.Class;}
}
定制装订
public类TimestampBinding实现绑定{
专用静态最终转换器转换器=新的时间戳转换器();
私有最终DefaultBinding委托=
新的DefaultBinding(converter());
@重写公共转换器(){return Converter;}
@重写公共无效sql(BindingSQLContext ctx)引发SQLException{
delegate.sql(ctx);
}
//等。所有其他重写的方法也是如此。
}
pom.xml(摘录)
java.time.Instant
java.time.Instant
xxx.x绑定
...
java.time.Instant
带时区的时间戳
一种方法是用反斜杠将
中的空格转义,如下所示:
<types>timestamp\ with\ time\ zone</types>
当启用javaTimeTypes
时,Jooq 3.11似乎将带有时区的时间戳变成了OffsetDateTime
,并且它还抱怨customTypes
被弃用,所以我无法得到其他适合我的答案
下面是我如何使用gradle jooq插件实现这一点的:
// inside the jooq...generator.database of build.gradle:
forcedTypes {
forcedType {
userType = 'java.time.Instant'
converter = '''
org.jooq.Converter.ofNullable(
java.time.OffsetDateTime.class,
java.time.Instant.class,
o -> o.toInstant(),
i -> i.atOffset(java.time.ZoneOffset.UTC))
'''
types = 'timestamp\\ with\\ time\\ zone'
}
}
将其转换为XML以供Maven或手动调用代码生成器应该非常容易,因为gradle插件的参数与XML的结构完全匹配。请注意,Groovy语法需要在类型
模式中加倍反斜杠,因此在转换为XML时需要对其进行调整
这使用一个内联转换器将Jooq当前使用的OffsetDateTime
转换为即时
。不需要外部转换器类。事实上,现在我想起来,它很不直观:手册只提到“Java正则表达式”。无论如何,谢谢你的挖掘@亚西莉亚,不客气。配置的这一方面肯定是不直观的,并且没有充分的文档记录。我通过一点尝试和错误得到了答案,然后在事后找到了原因的解释。这确实是非常不幸的,应该没有必要。途中修正(希望如此):@LukasEder在我的挖掘过程中,我遇到了GitHub问题,原因是引入了注释
标志。我在你刚刚出版的新一期中提到了这一点。
<plugin>
<groupId>org.jooq</groupId>
<artifactId>jooq-codegen-maven</artifactId>
<version>3.7.0</version>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<configuration>
<jdbc>
<driver>org.postgresql.Driver</driver>
<url>jdbc:postgresql:postgres</url>
<user>postgres</user>
<password>mypass</password>
</jdbc>
<generator>
<database>
<customTypes>
<customType>
<name>Instant</name>
<type>java.time.Instant</type>
<converter>xxx.TimestampConverter</converter>
</customType>
</customTypes>
<forcedTypes>
<forcedType>
<name>Instant</name>
<types>timestamp\ with\ time\ zone</types>
</forcedType>
</forcedTypes>
<name>org.jooq.util.postgres.PostgresDatabase</name>
<includes>author</includes>
<excludes/>
<inputSchema>public</inputSchema>
</database>
<target>
<packageName>xxx.table</packageName>
<directory>target/generated-sources/jooq</directory>
</target>
</generator>
</configuration>
</plugin>
<types>timestamp\ with\ time\ zone</types>
<plugin>
<groupId>org.jooq</groupId>
<artifactId>jooq-codegen-maven</artifactId>
<version>3.7.0</version>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<configuration>
<jdbc>
<driver>org.postgresql.Driver</driver>
<url>jdbc:postgresql:postgres</url>
<user>postgres</user>
<password>mypass</password>
</jdbc>
<generator>
<database>
<customTypes>
<customType>
<name>Instant</name>
<type>java.time.Instant</type>
<converter>xxx.TimestampConverter</converter>
</customType>
</customTypes>
<forcedTypes>
<forcedType>
<name>Instant</name>
<types>timestamp\ with\ time\ zone</types>
</forcedType>
</forcedTypes>
<name>org.jooq.util.postgres.PostgresDatabase</name>
<includes>author</includes>
<excludes/>
<inputSchema>public</inputSchema>
</database>
<target>
<packageName>xxx.table</packageName>
<directory>target/generated-sources/jooq</directory>
</target>
</generator>
</configuration>
</plugin>
// inside the jooq...generator.database of build.gradle:
forcedTypes {
forcedType {
userType = 'java.time.Instant'
converter = '''
org.jooq.Converter.ofNullable(
java.time.OffsetDateTime.class,
java.time.Instant.class,
o -> o.toInstant(),
i -> i.atOffset(java.time.ZoneOffset.UTC))
'''
types = 'timestamp\\ with\\ time\\ zone'
}
}