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
Copy file name to clipboardExpand all lines: src/main/asciidoc/reference/r2dbc-repositories.adoc
+41
Original file line number
Diff line number
Diff line change
@@ -279,6 +279,47 @@ Extensions are retrieved from the application context at the time of SpEL evalua
279
279
280
280
TIP: When using SpEL expressions in combination with plain parameters, use named parameter notation instead of native bind markers to ensure a proper binding order.
281
281
282
+
[[r2dbc.repositories.queries.query-by-example]]
283
+
=== Query By Example
284
+
285
+
Spring Data R2DBC also lets you use Query By Example to fashion queries.
286
+
This technique allows you to use a "probe" object.
287
+
Essentially, any field that isn't empty or `null` will be used to match.
<1> Create a custom `ExampleMatcher` that matches on ALL fields (use `matchingAny()` to match on *ANY* fields)
311
+
<2> For the `name` field, use a wildcard that matches against the end of the field
312
+
<3> Match columns against `null` (don't forget that `NULL` doesn't equal `NULL` in relational databases).
313
+
<4> Ignore the `role` field when forming the query.
314
+
<5> Plug the custom `ExampleMatcher` into the probe.
315
+
====
316
+
317
+
It's also possible to apply a `withTransform()` against any property, allowing you to transform a property before forming the query.
318
+
For example, you can apply a `toUpperCase()` to a `String` -based property before the query is created.
319
+
320
+
Query By Example really shines when you you don't know all the fields needed in a query in advance.
321
+
If you were building a filter on a web page where the user can pick the fields, Query By Example is a great way to flexibly capture that into an efficient query.
0 commit comments