@@ -709,29 +709,29 @@ As of 6.1, the named parameter statements of `NamedParameterJdbcTemplate` and th
709
709
parameter statements of a regular `JdbcTemplate` are available through a unified client API
710
710
with a fluent interaction model.
711
711
712
- E.g. with named parameters:
712
+ E.g. with positional parameters:
713
713
714
714
[source,java,indent=0,subs="verbatim,quotes"]
715
715
----
716
716
private JdbcClient jdbcClient = JdbcClient.create(dataSource);
717
717
718
718
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( );
722
722
}
723
723
----
724
724
725
- E.g. with positional parameters:
725
+ E.g. with named parameters:
726
726
727
727
[source,java,indent=0,subs="verbatim,quotes"]
728
728
----
729
729
private JdbcClient jdbcClient = JdbcClient.create(dataSource);
730
730
731
731
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( );
735
735
}
736
736
----
737
737
@@ -744,13 +744,24 @@ E.g. with positional parameters:
744
744
.list();
745
745
----
746
746
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
+
747
758
With a required single object result:
748
759
749
760
[source,java,indent=0,subs="verbatim,quotes"]
750
761
----
751
762
Actor actor = this.jdbcClient.sql("select first_name, last_name from t_actor where id = ?",
752
763
.param(1212L);
753
- .query((rs, rowNum) -> new Actor(rs.getString("first_name"), rs.getString("last_name")) )
764
+ .query(Actor.class )
754
765
.single();
755
766
----
756
767
@@ -760,7 +771,7 @@ With a `java.util.Optional` result:
760
771
----
761
772
Optional<Actor> actor = this.jdbcClient.sql("select first_name, last_name from t_actor where id = ?",
762
773
.param(1212L);
763
- .query((rs, rowNum) -> new Actor(rs.getString("first_name"), rs.getString("last_name")) )
774
+ .query(Actor.class )
764
775
.optional();
765
776
----
766
777
@@ -773,6 +784,32 @@ And for an update statement:
773
784
.update();
774
785
----
775
786
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
+
776
813
NOTE: `JdbcClient` is a flexible but simplified facade for JDBC query/update statements.
777
814
Advanced capabilities such as batch inserts and stored procedure calls typically require
778
815
extra customization: consider Spring's `SimpleJdbcInsert` and `SimpleJdbcCall` classes or
0 commit comments