批量插入支持

我尝试使用r2dbc执行批量插入。

我已经看到从Spring Boot开始使用Databaseclient,这还不可能。 我尝试使用R2DBC SPI Statementand方法来做到这一点,就像这样:

Mono.from(this.factory.create())
            .map(connection -> connection.createStatement(insertSQL))
            .map(statement -> {
                lines.forEach(line -> {
                    statement
                        .add()
                        .bind(0,line.getId())
                        ;
                });
                return statement;
            })
            .flatMap(statement -> Mono.from(statement.execute()));

我在日志上看到完成了两个插入请求。

2019-12-18 16:27:36.092 DEBUG [] 4133 --- [tor-tcp-epoll-1] io.r2dbc.postgresql.QUERY                : Executing query: insert into table(id) values ($1)
2019-12-18 16:27:36.116 DEBUG [] 4133 --- [tor-tcp-epoll-1] i.r.p.client.ReactorNettyClient          : Request:  Bind{}
2019-12-18 16:27:36.126 DEBUG [] 4133 --- [tor-tcp-epoll-1] io.r2dbc.postgresql.QUERY                : Executing query: insert into table(id) values ($1)
2019-12-18 16:27:36.130 DEBUG [] 4133 --- [tor-tcp-epoll-1] i.r.p.client.ReactorNettyClient          : Request:  Bind{}

add是执行批更新还是仅运行两个请求?

谢谢。

yin088 回答:批量插入支持

有点晚了,但总比没有好。 我会说您的代码发出两个单独的请求。 使用DatabaseClient,我猜自问问题以来,它已经成熟了:

    Mono.from(connectionFactory.create())
        .map(connection -> connection.createBatch())
        .map(batch -> {
            batch.add("delete from table1");
            batch.add("delete from table2");
            batch.add("delete from table3");
            return batch.execute();
        })
        // Etc

运行上述命令时,将记录以下内容:
查询:[“从表1中删除”,“从表2中删除”,“从表3中删除”]绑定:[]

本文链接:https://www.f2er.com/2897875.html

大家都在问