JOOQ动态设置

4

我希望只更新那些被更新的字段(它们不会为空)。 我想创建一个动态集合,例如(这不是有效的代码):

dslContext.update(table)
            if(field1 != null) {
              .set(field1, val1)
            }
            if(field2 != null) {
              .set(field2, val2)
            }
            .where(condition1)
            .and(condition2)
            .execute();

有没有一种方法可以做到这一点?
1个回答

1

虽然您可以通过滥用DSL API来实现这一点,但实际上最好的方法是使用以下方法之一:

一个使用Record的例子:
Record record = dslContext.newRecord(table);

if (field1 != null)
    record.set(field1, val1)

if (field2 != null) {
    record.set(field2, val2)

dslContext.update(table)
          .set(record)
          .where(condition1)
          .and(condition2)
          .execute();

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