This pull request introduces the queryForOptional
method, aimed at simplifying SQL query result retrieval with a RowMapper
. The goal is to eliminate boilerplate code often needed to handle empty results, offering a cleaner alternative to queryForObject
.
Example of usage:
Optional<Integer> age = jdbcTemplate.queryForOptional("SELECT AGE FROM CUSTMR WHERE ID = ?", (rs, rowNum) -> rs.getInt(1), 3);
Comment From: sbrannen
Hi @vuriaval,
Congratulations on submitting your first PR on GitHub! 👍
Unfortunately, we do not intend to introduce additional overloaded methods in JdbcOperations
/JdbcTemplate
to support Optional
in that fashion.
Instead, please use the fluent API available in the new JdbcClient
:
Optional<Integer> value = jdbcClient.sql("SELECT AGE FROM CUSTMR WHERE ID = ?")
.param(1, 3)
.query((rs, rowNum) -> rs.getInt(1))
.optional();
In light of that, I am closing this PR.
Comment From: snicoll
See also https://github.com/spring-projects/spring-framework/pull/724 and the issues that it links to where this has been discussed several times.