Skip to content

Commit 2ab1c5b

Browse files
committed
JdbcClient documentation update
See gh-30931
1 parent a2f52db commit 2ab1c5b

File tree

1 file changed

+47
-10
lines changed
  • framework-docs/modules/ROOT/pages/data-access/jdbc

1 file changed

+47
-10
lines changed

framework-docs/modules/ROOT/pages/data-access/jdbc/core.adoc

Lines changed: 47 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -709,29 +709,29 @@ As of 6.1, the named parameter statements of `NamedParameterJdbcTemplate` and th
709709
parameter statements of a regular `JdbcTemplate` are available through a unified client API
710710
with a fluent interaction model.
711711

712-
E.g. with named parameters:
712+
E.g. with positional parameters:
713713

714714
[source,java,indent=0,subs="verbatim,quotes"]
715715
----
716716
private JdbcClient jdbcClient = JdbcClient.create(dataSource);
717717
718718
public int countOfActorsByFirstName(String firstName) {
719-
return this.jdbcClient.sql("select count(*) from t_actor where first_name = :first_name")
720-
.param("first_name", firstName);
721-
.query().singleValue(Integer.class);
719+
return this.jdbcClient.sql("select count(*) from t_actor where first_name = ?")
720+
.param(firstName);
721+
.query(Integer.class).single();
722722
}
723723
----
724724

725-
E.g. with positional parameters:
725+
E.g. with named parameters:
726726

727727
[source,java,indent=0,subs="verbatim,quotes"]
728728
----
729729
private JdbcClient jdbcClient = JdbcClient.create(dataSource);
730730
731731
public int countOfActorsByFirstName(String firstName) {
732-
return this.jdbcClient.sql("select count(*) from t_actor where first_name = ?")
733-
.param(firstName);
734-
.query().singleValue(Integer.class);
732+
return this.jdbcClient.sql("select count(*) from t_actor where first_name = :firstName")
733+
.param("firstName", firstName);
734+
.query(Integer.class).single();
735735
}
736736
----
737737

@@ -744,13 +744,24 @@ E.g. with positional parameters:
744744
.list();
745745
----
746746

747+
Instead of a custom `RowMapper`, you may also specify a class to map to.
748+
E.g. assuming that `Actor` has `firstName` and `lastName` properties
749+
as a record class, a custom constructor, bean properties, or plain fields:
750+
751+
[source,java,indent=0,subs="verbatim,quotes"]
752+
----
753+
List<Actor> actors = this.jdbcClient.sql("select first_name, last_name from t_actor")
754+
.query(Actor.class)
755+
.list();
756+
----
757+
747758
With a required single object result:
748759

749760
[source,java,indent=0,subs="verbatim,quotes"]
750761
----
751762
Actor actor = this.jdbcClient.sql("select first_name, last_name from t_actor where id = ?",
752763
.param(1212L);
753-
.query((rs, rowNum) -> new Actor(rs.getString("first_name"), rs.getString("last_name")))
764+
.query(Actor.class)
754765
.single();
755766
----
756767

@@ -760,7 +771,7 @@ With a `java.util.Optional` result:
760771
----
761772
Optional<Actor> actor = this.jdbcClient.sql("select first_name, last_name from t_actor where id = ?",
762773
.param(1212L);
763-
.query((rs, rowNum) -> new Actor(rs.getString("first_name"), rs.getString("last_name")))
774+
.query(Actor.class)
764775
.optional();
765776
----
766777

@@ -773,6 +784,32 @@ And for an update statement:
773784
.update();
774785
----
775786

787+
Or an update statement with named parameters:
788+
789+
[source,java,indent=0,subs="verbatim,quotes"]
790+
----
791+
this.jdbcClient.sql("insert into t_actor (first_name, last_name) values (:firstName, :lastName)")
792+
.param("firstName", "Leonor").param("lastName", "Watling");
793+
.update();
794+
----
795+
796+
Instead of individual named parameters, you may also specify a parameter source object,
797+
e.g. a record class or a class with bean properties or a plain field holder which
798+
provides `firstName` and `lastName` properties, such as the `Actor` class from above:
799+
800+
[source,java,indent=0,subs="verbatim,quotes"]
801+
----
802+
this.jdbcClient.sql("insert into t_actor (first_name, last_name) values (:firstName, :lastName)")
803+
.paramSource(new Actor("Leonor", "Watling");
804+
.update();
805+
----
806+
807+
The automatic `Actor` class mapping for parameters as well as the query results above is
808+
provided through implicit `SimplePropertySqlParameterSource` and `SimplePropertyRowMapper`
809+
strategies which are also available for direct use. They can serve as a common replacement
810+
for `BeanPropertySqlParameterSource` and `BeanPropertyRowMapper`/`DataClassRowMapper`,
811+
also with `JdbcTemplate` and `NamedParameterJdbcTemplate` themselves.
812+
776813
NOTE: `JdbcClient` is a flexible but simplified facade for JDBC query/update statements.
777814
Advanced capabilities such as batch inserts and stored procedure calls typically require
778815
extra customization: consider Spring's `SimpleJdbcInsert` and `SimpleJdbcCall` classes or

0 commit comments

Comments
 (0)