为了账号安全,请及时绑定邮箱和手机立即绑定

在 Lambda 函数中获取返回值

在 Lambda 函数中获取返回值

米脂 2021-07-09 14:03:09
我有一个使用 lambda 实现的函数调用,它使用 jooq 库在 postgres 数据库中插入一行。下面是代码:  dslContext.transaction(    c -> {        this.postgresService.insertData(c, table, map);    });其中 org.jooq.Configuration 类型的 c。代码正常工作并在表中插入一条记录并返回插入的记录。如何从 lambda 函数中访问返回的主键。这是 insertData 的函数:public Record insertData(        Configuration configuration, Table<? extends Record> table, Map<TableField<? extends Record, ?>, Object> map    )    {        return DSL.using(configuration)            .insertInto(table)            .set(map)            .returning()            .fetchOne();    }
查看完整描述

2 回答

?
肥皂起泡泡

TA贡献1829条经验 获得超6个赞

您可以创建一个包装类来存储检索到的值:


class PrimaryKeyWrapper{

    Record primaryKey;


    public void setPrimaryKey(Record primaryKey) {

      this.primaryKey = primaryKey;

    }


    public Record getPrimaryKey() {

     return primaryKey;

    }

  }

并使用该类的实例从 lambda 函数内部存储此值:


PrimaryKeyWrapper primaryKeyWrapper = new PrimaryKeyWrapper();


dslContext.transaction(

c -> {

    Record primaryKey = this.postgresService.insertData(c, table, map);

    primaryKeyWrapper.setPrimaryKey(primaryKey);


});

最后,您可以从外部获取值:


primaryKeyWrapper.getPrimaryKey();


查看完整回答
反对 回复 2021-07-14
?
潇潇雨雨

TA贡献1833条经验 获得超4个赞

只需使用transactionResult:


String primaryKey = dslContext.transactionResult(

  (Configuration c) -> {

    return this.postgresService.insertData(c, table, map);

  });


查看完整回答
反对 回复 2021-07-14
  • 2 回答
  • 0 关注
  • 278 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信