我有以下问题:例如:
jooq是否提供了类似mybatis类型的转换机制,而不是每次我需要手动转换时。例如,int转换为Byte。将Long []转换为UInteger,等等。我不知道如何处理类型转换,可以给我一个详细的解决方案。
代码生成工具如下:
public static void main(String[] args) throws Exception {
Configuration configuration = new Configuration()
.withJdbc(new Jdbc()
.withDriver("com.mysql.jdbc.Driver")
.withUrl("jdbc:mysql://localhost:3306/51unboxdb")
.withUser("root")
.withPassword("root")
)
.withGenerator(
new Generator()
.withName("org.jooq.util.JavaGenerator")
.withGenerate(new Generate()
.withPojos(true)
.withImmutablePojos(true)
.withInterfaces(true)
.withDaos(true)
.withSpringAnnotations(true)
.withJavaTimeTypes(true)
)
.withDatabase(new Database()
.withName("org.jooq.util.mysql.MySQLDatabase")
.withIncludes(".*")
.withExcludes("")
.withDateAsTimestamp(true)
.withInputSchema("51unboxdb")
)
.withTarget(new Target()
.withPackageName("com.chunfytseng.unbox.jooq")
.withDirectory("src/main/java")
)
);
GenerationTool.generate(configuration);
}
数据库中有许多表。当您修改一个表的属性,然后从新生成的代码中覆盖现有代码时,这会使我非常麻烦。我是否可以指定更新表或排除某些表?并不是每次构建它时都会覆盖现有代码。
发布于 2017-06-27 14:02:08
至少有两种方法可以解决这个问题。
1.使用代码生成器的类型重写功能
代码生成器支持一个名为type rewriting的特性,在该特性中,您可以指定一个与所有相关列名和/或数据类型匹配的正则表达式,在您的情况下,其生成的类型应该重写为BIGINT
(因为您希望使用long
而不是UInteger
。只需添加:
...
.withDatabase(new Database()
...
.withForcedTypes(new ForcedType()
.withName("BIGINT")
.withExpression("(?i:.*\.USER\.ID)")
)
...
)
...
2.使用内置的自动转换接口
jOOQ的自动转换实用程序称为org.jooq.tools.Convert
。它已经涵盖了大量的自动类型转换,例如在不同的众所周知的数字类型之间,正如你所展示的那样。还有很多方便的API,例如,你可以将你想要的类型的Class引用传递给fetch()方法,以获得这种类型的结果。
https://stackoverflow.com/questions/44751928
复制相似问题