如何使用R2DBC-DatabaseClient实现saveAll呢?

huangapple go评论80阅读模式
英文:

How to implement saveAll using R2DBC-DatabaseClient?

问题

@Repository
class AppStartRepo(val client: DatabaseClient) {

    suspend fun saveAll(starts: List<AppStart>) {
        val builder = StringBuilder()
        builder.append("INSERT INTO app_start (device_id, platform, last_update) VALUES ")
        for (index in starts.indices) {
            builder.append("(?")
            builder.append(index * 3)
            builder.append(",?")
            builder.append(index * 3 + 1)
            builder.append(",?")
            builder.append(index * 3 + 2)
            builder.append(")")
        }

        var querySpec = client.sql(builder.toString())
        for (index in starts.indices) {
            val start = starts[index]
            querySpec = querySpec.bind(index * 3, start.deviceId)
                    .bind(index * 3 + 1, start.platform.toString())
                    .bind(index * 3 + 2, start.lastUpdate)
        }

        querySpec.await()
    }

}
英文:

How can I implement a saveAll method like in ReactiveCrudRepository using DatabaseClient?

That's my implementation - Sadly it doesn't work.

@Repository
class AppStartRepo(val client: DatabaseClient) {

suspend fun saveAll(starts: List&lt;AppStart&gt;) {
    val builder = StringBuilder()
    builder.append(&quot;INSERT INTO app_start (device_id, platform, last_update) VALUES &quot;)
    for (index in starts.indices) {
        builder.append(&quot;(?&quot;)
        builder.append(index * 3)
        builder.append(&quot;,?&quot;)
        builder.append(index * 3 + 1)
        builder.append(&quot;,?&quot;)
        builder.append(index * 3 + 2)
        builder.append(&quot;)&quot;)
    }

    var querySpec = client.sql(builder.toString())
    for (index in starts.indices) {
        val start = starts[index]
        querySpec = querySpec.bind(index * 3, start.deviceId)
                .bind(index * 3 + 1, start.platform.toString())
                .bind(index * 3 + 2, start.lastUpdate)
    }

    querySpec.await()
}

}

答案1

得分: 1

@Autowired
private DatabaseClient databaseClient;

public Mono<Integer> saveAll(List<Book> books) {
    var query = new StringBuilder("INSERT INTO book(title, author) VALUES ");
    var bookIterator = books.iterator();
    while(bookIterator.hasNext()) {
        var book = bookIterator.next();
        query.append(String.format("('%s', '%s')", book.getTitle(), book.getAuthor()));
        if(bookIterator.hasNext()) {
            query.append(", ");
        }
    }
    return databaseClient.execute(query.toString()).fetch().rowsUpdated();
}
英文:
@Autowired
private DatabaseClient databaseClient;

public Mono&lt;Integer&gt; saveAll(List&lt;Book&gt; books) {
    var query = new StringBuilder(&quot;INSERT INTO book(title, author) VALUES &quot;);
    var bookIterator = books.iterator();
    while(bookIterator.hasNext()) {
        var book = bookIterator.next();
        query.append(String.format(&quot;(&#39;%s&#39;, &#39;%s&#39;)&quot;, book.getTitle(), book.getAuthor()));
        if(bookIterator.hasNext()) {
            query.append(&quot;, &quot;);
        }
    }
    return databaseClient.execute(query.toString()).fetch().rowsUpdated();
}

答案2

得分: 0

使用Statement.add方法绑定多个参数。

此帖子中查看我的示例。

英文:

Use Statement.add method to bind multiple parameters.

See my example in this post.

huangapple
  • 本文由 发表于 2020年10月8日 01:31:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/64249333.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定