0

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

我已经从 Spring Boot 中看到 DatabaseClient ,这还不可能。我尝试使用 R2DBC SPIStatementand方法来做到这一点,如下所示:

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执行批量更新还是只运行两个请求?

谢谢。

4

1 回答 1

0

有点晚了,但总比我猜的好。我会说您的代码发出两个单独的请求。使用 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

运行上述程序时,将记录以下内容:
Query:["delete from table1","delete from table2","delete from table3"] Bindings:[]

于 2020-03-26T07:37:26.553 回答