简体   繁体   English

如何在不生成代码的情况下使用 Jooq insert ...在 MySQL 中返回?

[英]How do I use Jooq insert ... returning in MySQL without code generation?

I've been trying to use insert...returning in MySQL with the DSL-based table definition (I'm not using the code generation) and my returned record is always null.我一直在尝试在 MySQL 中使用基于 DSL 的表定义的 insert...returning(我没有使用代码生成),并且我返回的记录始终为空。 Based on reading, I need to specify the identify column in the table definition, but I have no idea how!根据阅读,我需要在表定义中指定识别列,但我不知道如何!

Record recordKey = create.insertInto(table("modulerecords"), 
                                    field("id"), 
                                    field("module_id"),
                                    field("created_date"), 
                                    field("created_by"), 
                                    field("state"), 
                                    field("tag_id"),
                                    field("start_time",Timestamp.class), 
                                    field("kill_time", Timestamp.class), 
                                    field("feed_guid")
                                    )
                            .values(null, moduleId, currentTimestamp(), 
                                    userId, state, tagId, 
                                    new Timestamp(startTime), 
                                    new Timestamp(killTime), feedGuid)
                            .returning(field("id"))
                            .fetchOne();

The field "id" is auto_increment primary key in the database, but recordKey is always null.字段“id”是数据库中的auto_increment主键,但recordKey始终为空。

It is highly recommended you use the code generator to provide all the meta information to the DSL API.强烈建议您使用代码生成器向 DSL API 提供所有元信息。 You can, of course, not use the code generator and still use the internal APIs that the code generator would otherwise use.当然,您可以不使用代码生成器,而仍然使用代码生成器原本会使用的内部 API。 Instead of creaating your table and field references using the plain SQL API , you'd have to create a TableImpl subclass and override / implement all the relevant methods.您必须创建一个TableImpl子类并覆盖/实现所有相关方法,而不是使用普通的 SQL API创建表和字段引用。

Or, you just use the code generator.或者,您只需使用代码生成器。

As of jOOQ 3.14, this is possible by specifying the field's datatype as being an identity, which can be done using SQLDataType.INTEGER.identity(true) .从 jOOQ 3.14 开始,这可以通过将字段的数据类型指定为标识来实现,这可以使用SQLDataType.INTEGER.identity(true)来完成。

So for example, if you had a table with an auto-generating integer id and a string name, you would call:例如,如果您有一个带有自动生成整数 id 和字符串名称的表,您可以调用:

int id = DSL.using(connection, MYSQL_5_7)
            .insertInto(
                table("myTable"),
                field("name", String.class))
            .values("John Smith")
            .returning(field("id", SQLDataType.INTEGER.identity(true)))
            .fetchAny(field("id", Integer.class))

So for your example, you would do所以对于你的例子,你会做

Record recordKey = create.insertInto(table("modulerecords"), 
                                    field("id"), 
                                    field("module_id"),
                                    field("created_date"), 
                                    field("created_by"), 
                                    field("state"), 
                                    field("tag_id"),
                                    field("start_time",Timestamp.class), 
                                    field("kill_time", Timestamp.class), 
                                    field("feed_guid")
                                    )
                            .values(null, moduleId, currentTimestamp(), 
                                    userId, state, tagId, 
                                    new Timestamp(startTime), 
                                    new Timestamp(killTime), feedGuid)
                            .returning(field("id", SQLDataType.INTEGER.identity(true)))
                            .fetchOne();

Seethis Github comment for more background.有关更多背景信息,请参阅此 Github 评论

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM