Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce JdbcClient with unified parameter capabilities (as a JDBC variant of DatabaseClient) #30931

Closed
making opened this issue Jul 24, 2023 · 5 comments
Assignees
Labels
in: data Issues in data modules (jdbc, orm, oxm, tx) type: enhancement A general enhancement
Milestone

Comments

@making
Copy link
Member

making commented Jul 24, 2023

Spring 6.1 introduces RestClient, a blocking variant of WebClient.
I would like a modern JDBC client like R2DBC's DatabaseClient as well.

Like RestTemplate, JdbcTemplate also has many overloaded method, making it difficult to add new functionality. (For example, Optional support #30927) Like RestClient for RestTemplate, JdbcClient for JdbcTemplate with fluent api would make it easier to add modern features.

@jhoeller
Copy link
Contributor

As per my comment on ##30927 (comment), this is to be investigated for 6.1 now. If we are happy with an initial cut, I can see this being included in 6.1 M4 already.

@jhoeller jhoeller self-assigned this Jul 24, 2023
@jhoeller jhoeller added in: data Issues in data modules (jdbc, orm, oxm, tx) type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Jul 24, 2023
@jhoeller jhoeller added this to the 6.1.x milestone Jul 24, 2023
@making making changed the title Support JDBC variant of DatabaseClient (something like JdbcClient) Support JDBC variant of DatabaseClient (something like JdbcClient) Jul 25, 2023
@jhoeller jhoeller modified the milestones: 6.1.x, 6.1.0-M4 Jul 25, 2023
@jhoeller
Copy link
Contributor

This is coming into 6.1 M4 as org.springframework.jdbc.core.simple.JdbcClient now, as a unified facade for JdbcTemplate and NamedParameterJdbcTemplate with flexible parameter options as well as flexible result retrieval options. For example:

Optional<Integer> value = client.sql("SELECT AGE FROM CUSTMR WHERE ID = :id")
        .param("id", 3)
        .query((rs, rowNum) -> rs.getInt(1))
        .optional();

@jhoeller
Copy link
Contributor

For a first impression, here is the entire JdbcClient API: https://github.com/spring-projects/spring-framework/blob/main/spring-jdbc/src/main/java/org/springframework/jdbc/core/simple/JdbcClient.java

@making
Copy link
Member Author

making commented Jul 26, 2023

I migrated 2 projects to JdbcClient. Both are in production :)

categolj/blog-api@b2303de
categolj/note-api@5d5ecc3

The migration was very smooth and all tests passed without any changes.
I love this client. Thank you for making this happen!

@jhoeller jhoeller changed the title Support JDBC variant of DatabaseClient (something like JdbcClient) Introduce JdbcClient with unified parameter capabilities (as a JDBC variant of DatabaseClient) Aug 7, 2023
@jhoeller
Copy link
Contributor

jhoeller commented Aug 15, 2023

A heads-up that the query variants have slightly changed along with #26594: There is a new query(Class) method with value and property mapping support now, as an alternative to specifying a custom RowMapper instance. Since this also covers typed column mappings, the singleColumn/singleValue methods are declared without a Class parameter now and just return whatever ResultSet.getObject provided (analogous to the existing singleRow method).

Note that singleColumn/singleValue typing only works for the actual result type now, there is no coercion/conversion anymore (like on all other methods on the query() result spec). So e.g. to get an Integer, do .query(Integer.class).single() instead of the former .query().singleValue(Integer.class).

jhoeller added a commit that referenced this issue Aug 15, 2023
…field mapping

Includes query(Class) method with value and property mapping support on JdbcClient.
JdbcClient's singleColumn/singleValue are declared without a Class parameter now.

Closes gh-26594
See gh-30931
jhoeller added a commit that referenced this issue Aug 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: data Issues in data modules (jdbc, orm, oxm, tx) type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

3 participants