java - 将jOOQ的批处理加载器与onDuplicateKeyUpdate一起使用时,如何仅更新某些字段?
问题描述
是否可以仅在找到重复项时使用 jOOQ 的batchAll()
加载器更新某些字段?类似于如何onDublicateKeyUpdate()
用于单个记录:https ://www.jooq.org/doc/latest/manual/sql-building/sql-statements/insert-statement/insert-on-duplicate-key/ 。
我要更改的代码,仅在存在重复时更新一个字段:
dsl.loadInto(TABLE)
.batchAll()
.onDuplicateKeyUpdate()
.loadRecords(records)
.fields(TABLE.fields())
.execute();
解决方案
Loader
不,这对于API是不可能的,但是您可以使用批处理 API以编程方式使用单个语句的普通批处理,或者甚至使用批处理连接,它收集所有 JDBC 语句并延迟执行,直到可以执行批处理:
dsl.batched(c -> {
for (Record record : records) {
c.dsl().insertInto(TABLE)
.set(record)
.onDuplicateKeyUpdate()
.set(TABLE.FIELD_OF_INTEREST, record.get(TABLE.FIELD_OF_INTEREST))
.execute(); // Actual execution is delayed until the end of the lambda
}
}); // Now, all collected statements are batched together
推荐阅读
- vba - 您如何在 VBA 中指定货币文字
- react-native - 如何使用 react-native 应用程序中的语言下拉菜单翻译移动应用程序文本?
- heroku - Strapi + Heroku 部署失败:npm ERR!代码 ERESOLVE
- sql - 获取第二个最大日期json对象(postgres)
- python - 如何解决错误“发生错误:Django/Python3 应用程序没有此类文件或目录”
- c++ - 为什么我需要使用队列
q 并且不能使用队列 问? - javascript - 将文件上传到 Firebase 存储时遇到问题
- php - 我有一个未序列化的数据。现在我如何剪切数组的第一部分并将其转换为 json 格式?
- python - 如何按列表中的出现频率对 Django 查询集进行排序并确保其余对象跟随?
- java - Netbeans 和 SQL 连接无法加载身份验证插件“caching_sha2_password”