You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
#59 - Consider custom conversion in MappingR2dbcConverter.
MappingR2dbcConverter now considers custom conversions for inbound and outbound conversion of top-level types (Row to Entity, Entity to OutboundRow) and on property level (e.g. convert an object to String and vice versa).
Copy file name to clipboardExpand all lines: src/main/asciidoc/reference/mapping.adoc
+85-1
Original file line number
Diff line number
Diff line change
@@ -28,6 +28,47 @@ Public `JavaBean` properties are not used.
28
28
Otherwise, the zero-argument constructor is used.
29
29
If there is more than one non-zero-argument constructor, an exception will be thrown.
30
30
31
+
[[mapping-configuration]]
32
+
== Mapping Configuration
33
+
34
+
Unless explicitly configured, an instance of `MappingR2dbcConverter` is created by default when you create a `DatabaseClient`.
35
+
You can create your own instance of the `MappingR2dbcConverter`.
36
+
By creating your own instance, you can register Spring converters to map specific classes to and from the database.
37
+
38
+
You can configure the `MappingR2dbcConverter` as well as `DatabaseClient` and `ConnectionFactory` by using Java-based metadata. The following example uses Spring's Java-based configuration:
39
+
40
+
.@Configuration class to configure R2DBC mapping support
41
+
====
42
+
[source,java]
43
+
----
44
+
@Configuration
45
+
public class MyAppConfig extends AbstractR2dbcConfiguration {
46
+
47
+
public ConnectionFactory connectionFactory() {
48
+
return ConnectionFactories.get("r2dbc:…");
49
+
}
50
+
51
+
// the following are optional
52
+
53
+
@Bean
54
+
@Override
55
+
public R2dbcCustomConversions r2dbcCustomConversions() {
56
+
57
+
List<Converter<?, ?>> converterList = new ArrayList<Converter<?, ?>>();
return new R2dbcCustomConversions(getStoreConversions(), converterList);
61
+
}
62
+
}
63
+
----
64
+
====
65
+
66
+
`AbstractR2dbcConfiguration` requires you to implement a method that defines a `ConnectionFactory`.
67
+
68
+
You can add additional converters to the converter by overriding the `r2dbcCustomConversions` method.
69
+
70
+
NOTE: `AbstractR2dbcConfiguration` creates a `DatabaseClient` instance and registers it with the container under the name `databaseClient`.
71
+
31
72
[[mapping-usage]]
32
73
== Metadata-based Mapping
33
74
@@ -52,7 +93,6 @@ public class Person {
52
93
53
94
private String firstName;
54
95
55
-
@Indexed
56
96
private String lastName;
57
97
}
58
98
----
@@ -103,3 +143,47 @@ class OrderItem {
103
143
104
144
----
105
145
146
+
[[mapping-explicit-converters]]
147
+
=== Overriding Mapping with Explicit Converters
148
+
149
+
When storing and querying your objects, it is convenient to have a `R2dbcConverter` instance handle the mapping of all Java types to `OutboundRow` instances.
150
+
However, sometimes you may want the `R2dbcConverter` instances do most of the work but let you selectively handle the conversion for a particular type -- perhaps to optimize performance.
151
+
152
+
To selectively handle the conversion yourself, register one or more one or more `org.springframework.core.convert.converter.Converter` instances with the `R2dbcConverter`.
153
+
154
+
You can use the `r2dbcCustomConversions` method in `AbstractR2dbcConfiguration` to configure converters. The examples <<mapping-configuration, at the beginning of this chapter>> show how to perform the configuration using Java.
155
+
156
+
NOTE: Custom top-level entity conversion requires asymmetric types for conversion. Inbound data is extracted from R2DBC's `Row`.
157
+
Outbound data (to be used with `INSERT`/`UPDATE` statements) is represented as `OutboundRow` and later assembled to a statement.
158
+
159
+
The following example of a Spring Converter implementation converts from a `Row` to a `Person` POJO:
160
+
161
+
[source,java]
162
+
----
163
+
@ReadingConverter
164
+
public class PersonReadConverter implements Converter<Row, Person> {
165
+
166
+
public Person convert(Row source) {
167
+
Person p = new Person(source.get("id", String.class),source.get("name", String.class));
168
+
p.setAge(source.get("age", Integer.class));
169
+
return p;
170
+
}
171
+
}
172
+
----
173
+
174
+
The following example converts from a `Person` to a `OutboundRow`:
175
+
176
+
[source,java]
177
+
----
178
+
@WritingConverter
179
+
public class PersonWriteConverter implements Converter<Person, OutboundRow> {
0 commit comments