java - jOOQ 查询中的重复次数较少

标签 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 不允许我按原样发布它,而不添加更多详细信息,我认为这对于我的问题来说是不必要的,但尽管如此,他们希望我发布更多内容文本,我现在正在通过输入此消息来执行此操作,我希望您不必读到最后。)

最佳答案

由于您要将所有列传递给插入语句,因此您可以这样写:

// 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();

关于java - jOOQ 查询中的重复次数较少,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52355849/

相关文章:

java - 在 Java 中将数据附加到 Map

java - 对 Bean 的 Arraylist 的 Arraylist 进行排序

SQL Server 动态排序依据

java - jOOQ 在更新时添加到当前值

java - jOOQ 子查询给出编译错误“SelectWhereStep 类型中的方法 where(Condition...)...不适用于参数

java - 将war部署到tomcat配置问题

java - 通过实例调用静态函数时是否会忽略运行时实例?

mysql - mysql中的这个触发器有什么问题?

mysql - Query1 与 Query2 连接,但 Query2 使用 Query1 的结果,如何仅使用 1 个查询来做到这一点?

java - Mysql流式结果集和jOOQ fetchLazy