From eb65939341e38260434ed96d62d1761053f3e199 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Wed, 16 Aug 2023 17:02:41 +0200 Subject: [PATCH] Add examples for new bind/map methods on DatabaseClient See gh-27282 See gh-26021 --- .../modules/ROOT/pages/data-access/r2dbc.adoc | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/framework-docs/modules/ROOT/pages/data-access/r2dbc.adoc b/framework-docs/modules/ROOT/pages/data-access/r2dbc.adoc index 453e9c14ce1..a4f7814a61f 100644 --- a/framework-docs/modules/ROOT/pages/data-access/r2dbc.adoc +++ b/framework-docs/modules/ROOT/pages/data-access/r2dbc.adoc @@ -254,6 +254,25 @@ Kotlin:: ---- ====== +Alternatively, there is a shortcut for mapping to a single value: + +[source,java] +---- + Flux names = client.sql("SELECT name FROM person") + .mapValue(String.class) + .all(); +---- + +Or you may map to a result object with bean properties or record components: + +[source,java] +---- + // assuming a name property on Person + Flux persons = client.sql("SELECT name FROM person") + .mapProperties(Person.class) + .all(); +---- + [[r2dbc-DatabaseClient-mapping-null]] .What about `null`? **** @@ -324,6 +343,27 @@ The following example shows parameter binding for a query: .bind("age", 34); ---- +Alternatively, you may pass in a map of names and values: + +[source,java] +---- + Map params = new LinkedHashMap<>(); + params.put("id", "joe"); + params.put("name", "Joe"); + params.put("age", 34); + db.sql("INSERT INTO person (id, name, age) VALUES(:id, :name, :age)") + .bindValues(params); +---- + +Or you may pass in a parameter object with bean properties or record components: + +[source,java] +---- + // assuming id, name, age properties on Person + db.sql("INSERT INTO person (id, name, age) VALUES(:id, :name, :age)") + .bindProperties(new Person("joe", "Joe", 34); +---- + .R2DBC Native Bind Markers **** R2DBC uses database-native bind markers that depend on the actual database vendor.