在Lambda函数中获取返回值

时间:2018-08-13 10:53:19

标签: java postgresql jooq

我有一个使用lambda实现的函数调用,该函数使用jooq库在postgres数据库中插入了一行。

下面是代码:

const webpack = require('webpack')
const path = require('path')
const UglifyJSPlugin = require('uglifyjs-webpack-plugin');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const OptimizeCssAssetsPlugin = require('optimize-css-assets-webpack-plugin');

module.exports = {
  mode: "production",

  entry: {
    owners: "./src/app1.tsx",
    properties: "./src/app2.tsx"
  },

  output: {
    filename: '[name].[chunkhash].min.js',
    path: path.resolve(__dirname, 'dist'),
  },

  optimization: {
    splitChunks: {
      cacheGroups: {
        vendor: {
          chunks: 'initial',
          name: 'vendor',
          test: 'vendor',
          enforce: true,
        },
      }
    },
    runtimeChunk: true
  },

  resolve: {
    extensions: [".ts", ".tsx", ".js", ".json", "*.png", "*.jpg", "*.svg"]
  },

  module: {
    rules: [{
        test: /\.tsx?$/,
        include: /src/,
        loader: "ts-loader"
      },
      {
        test: /\.js$/,
        exclude: /node_modules/,
        loader: 'babel-loader',

        options: {
          presets: ["es5"]
        }
      },
      {
        test: /\.(jpe?g|png|gif|svg)$/i,
        use: [
          'url-loader?limit=10000',
          'img-loader'
        ]
      },
      {
        test: /\.css$/,
        include: /src/,
        exclude: /node_modules/,
        loader: ExtractTextPlugin.extract('typings-for-css-modules-loader?modules&namedExport&camelCase&importLoaders=1&localIdentName=[name]__[local]___[hash:base64:5]'),
      },
      {
        test: /\.svg$/,
        loader: 'svg-loader?pngScale=2'
      },
      {
        test: /\.css$/,
        include: /node_modules/,
        loader: ExtractTextPlugin.extract({fallback: 'style-loader', use: 'css-loader?sourceMap'})
      },
    ]
  },

  externals: {
    "react": "React",
    "react-dom": "ReactDOM"
  },

  plugins: [
    new UglifyJSPlugin(),
    new ExtractTextPlugin('[name].[chunkhash].min.css'),
    new OptimizeCssAssetsPlugin({
      assetNameRegExp: /\.css$/g,
      cssProcessor: require('cssnano'),
      cssProcessorOptions: { discardComments: { removeAll: true } },
      canPrint: true
    })
  ]
}

其中org.jooq.Configuration类型的c。

代码正常工作,并在表中插入一条记录,并返回插入的记录。我该如何访问 从lambda函数返回主键。

这是insertData的功能:

  dslContext.transaction(
    c -> {
        this.postgresService.insertData(c, table, map);
    });

2 个答案:

答案 0 :(得分:2)

只需使用transactionResult

String primaryKey = dslContext.transactionResult(
  (Configuration c) -> {
    return this.postgresService.insertData(c, table, map);
  });

答案 1 :(得分:1)

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

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