jOOQ查询中的重复次数更少

时间:2018-09-16 15:53:29

标签: java sql jooq

关于如何以更少的重复次数定义以下jOOQ查询的任何想法吗?

我正在使用jOOQ 3.11.4。

db.insertInto(ACCOUNT,
        ACCOUNT.ACCOUNT_ID,
        ACCOUNT.EMAIL,
        ACCOUNT.FIRST_NAME,
        ACCOUNT.LAST_NAME,
        ACCOUNT.IS_ADMIN,
        ACCOUNT.PASSWORD)
        .values(account.accountId,
            account.email,
            account.firstName,
            account.lastName,
            account.isAdmin,
            account.password)
        .onConflict(ACCOUNT.ACCOUNT_ID)
        .doUpdate()
        .set(ACCOUNT.EMAIL, account.email)
        .set(ACCOUNT.FIRST_NAME, account.firstName)
        .set(ACCOUNT.LAST_NAME, account.lastName)
        .set(ACCOUNT.IS_ADMIN, account.isAdmin)
        .set(ACCOUNT.PASSWORD, account.password)
        .returning(
            ACCOUNT.ACCOUNT_ID,
            ACCOUNT.EMAIL,
            ACCOUNT.FIRST_NAME,
            ACCOUNT.LAST_NAME,
            ACCOUNT.IS_ADMIN,
            ACCOUNT.PASSWORD
        )
        .fetchOne()

(我发现我的问题主要是代码,而StackOverflow不允许我按原样发布它,而无需添加更多细节,我认为这不是我的问题所必需的,但是他们希望我发布更多细节文本,我现在正在通过键入此消息来执行此操作,希望您不必读到最后。)

1 个答案:

答案 0 :(得分:2)

由于您要将所有列都传递给insert语句,因此您可以改写以下内容:

// Create an AccountRecord that contains your POJO data
Record rec = db.newRecord(ACCOUNT);
rec.from(account);

// Don't pass the columns to the insert statement explicitly
db.insertInto(ACCOUNT)

// But pass the record to the set method. It will use all the changed values
  .set(rec)

// Use the MySQL syntax, which can be emulated on PostgreSQL using ON CONFLICT
  .onDuplicateKeyUpdate()

// But pass the record to the set method again
  .set(rec)

// Don't specify any columns to the returning clause. It will take all the ACCOUNT columns
  .returning()
  .fetchOne();