JOOQ 动态插入多个值。

4
我有一组记录,需要使用upsert的方式在一个查询中进行插入。
INSERT INTO table1 (name, lastname) 
VALUES ('name1', 'lastname1'), ('name2', 'lastname2') 
    ON CONFLICT DO NOTHING;

不是批量或合并操作,并且需要使用“on conflict”逻辑。
2个回答

4
使用新的 jOOQ 3.15 InsertValuesStep2.valuesOfRows() 方法:
insertInto(table1, table1.name, table1.lastname)
    .valuesOfRows(usersForInsert.map { DSL.row(it.name, it.lastname) })
    .onConflictDoNothing()
    .execute()

-1

使用 Kotlin 的这种方法使代码变得非常简洁:

insertInto(table1, table1.name, table1.lastname)
            .apply {
                usersForInsert.forEach { user ->
                    values(user.name, user.lastname)
                }
            }
            .onConflictDoNothing()
            .execute()

谢谢


如果不在apply之外至少调用一个值调用,这将无法工作,因为execute()只能在InsertValuesStepN<>泛型上调用。 - Christoph Grimmer

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接