Polishing

This commit is contained in:
Juergen Hoeller 2023-07-26 14:07:08 +02:00
parent 2573ba4a50
commit 333249e7b0
3 changed files with 23 additions and 39 deletions

View File

@ -501,8 +501,8 @@ extend from it, your sub-class inherits a `setDataSource(..)` method from the
Regardless of which of the above template initialization styles you choose to use (or Regardless of which of the above template initialization styles you choose to use (or
not), it is seldom necessary to create a new instance of a `JdbcTemplate` class each not), it is seldom necessary to create a new instance of a `JdbcTemplate` class each
time you want to run SQL. Once configured, a `JdbcTemplate` instance is thread-safe. time you want to run SQL. Once configured, a `JdbcTemplate` instance is thread-safe.
If your application accesses multiple If your application accesses multiple databases, you may want multiple `JdbcTemplate`
databases, you may want multiple `JdbcTemplate` instances, which requires multiple `DataSources` and, subsequently, multiple differently instances, which requires multiple `DataSources` and, subsequently, multiple differently
configured `JdbcTemplate` instances. configured `JdbcTemplate` instances.
@ -531,11 +531,8 @@ Java::
} }
public int countOfActorsByFirstName(String firstName) { public int countOfActorsByFirstName(String firstName) {
String sql = "select count(*) from t_actor where first_name = :first_name";
String sql = "select count(*) from T_ACTOR where first_name = :first_name";
SqlParameterSource namedParameters = new MapSqlParameterSource("first_name", firstName); SqlParameterSource namedParameters = new MapSqlParameterSource("first_name", firstName);
return this.namedParameterJdbcTemplate.queryForObject(sql, namedParameters, Integer.class); return this.namedParameterJdbcTemplate.queryForObject(sql, namedParameters, Integer.class);
} }
---- ----
@ -547,7 +544,7 @@ Kotlin::
private val namedParameterJdbcTemplate = NamedParameterJdbcTemplate(dataSource) private val namedParameterJdbcTemplate = NamedParameterJdbcTemplate(dataSource)
fun countOfActorsByFirstName(firstName: String): Int { fun countOfActorsByFirstName(firstName: String): Int {
val sql = "select count(*) from T_ACTOR where first_name = :first_name" val sql = "select count(*) from t_actor where first_name = :first_name"
val namedParameters = MapSqlParameterSource("first_name", firstName) val namedParameters = MapSqlParameterSource("first_name", firstName)
return namedParameterJdbcTemplate.queryForObject(sql, namedParameters, Int::class.java)!! return namedParameterJdbcTemplate.queryForObject(sql, namedParameters, Int::class.java)!!
} }
@ -579,12 +576,9 @@ Java::
} }
public int countOfActorsByFirstName(String firstName) { public int countOfActorsByFirstName(String firstName) {
String sql = "select count(*) from t_actor where first_name = :first_name";
String sql = "select count(*) from T_ACTOR where first_name = :first_name";
Map<String, String> namedParameters = Collections.singletonMap("first_name", firstName); Map<String, String> namedParameters = Collections.singletonMap("first_name", firstName);
return this.namedParameterJdbcTemplate.queryForObject(sql, namedParameters, Integer.class);
return this.namedParameterJdbcTemplate.queryForObject(sql, namedParameters, Integer.class);
} }
---- ----
@ -596,7 +590,7 @@ Kotlin::
private val namedParameterJdbcTemplate = NamedParameterJdbcTemplate(dataSource) private val namedParameterJdbcTemplate = NamedParameterJdbcTemplate(dataSource)
fun countOfActorsByFirstName(firstName: String): Int { fun countOfActorsByFirstName(firstName: String): Int {
val sql = "select count(*) from T_ACTOR where first_name = :first_name" val sql = "select count(*) from t_actor where first_name = :first_name"
val namedParameters = mapOf("first_name" to firstName) val namedParameters = mapOf("first_name" to firstName)
return namedParameterJdbcTemplate.queryForObject(sql, namedParameters, Int::class.java)!! return namedParameterJdbcTemplate.queryForObject(sql, namedParameters, Int::class.java)!!
} }
@ -644,7 +638,6 @@ Java::
} }
// setters omitted... // setters omitted...
} }
---- ----
@ -673,12 +666,9 @@ Java::
} }
public int countOfActors(Actor exampleActor) { public int countOfActors(Actor exampleActor) {
// notice how the named parameters match the properties of the above 'Actor' class // notice how the named parameters match the properties of the above 'Actor' class
String sql = "select count(*) from T_ACTOR where first_name = :firstName and last_name = :lastName"; String sql = "select count(*) from t_actor where first_name = :firstName and last_name = :lastName";
SqlParameterSource namedParameters = new BeanPropertySqlParameterSource(exampleActor); SqlParameterSource namedParameters = new BeanPropertySqlParameterSource(exampleActor);
return this.namedParameterJdbcTemplate.queryForObject(sql, namedParameters, Integer.class); return this.namedParameterJdbcTemplate.queryForObject(sql, namedParameters, Integer.class);
} }
---- ----
@ -694,7 +684,7 @@ Kotlin::
fun countOfActors(exampleActor: Actor): Int { fun countOfActors(exampleActor: Actor): Int {
// notice how the named parameters match the properties of the above 'Actor' class // notice how the named parameters match the properties of the above 'Actor' class
val sql = "select count(*) from T_ACTOR where first_name = :firstName and last_name = :lastName" val sql = "select count(*) from t_actor where first_name = :firstName and last_name = :lastName"
val namedParameters = BeanPropertySqlParameterSource(exampleActor) val namedParameters = BeanPropertySqlParameterSource(exampleActor)
return namedParameterJdbcTemplate.queryForObject(sql, namedParameters, Int::class.java)!! return namedParameterJdbcTemplate.queryForObject(sql, namedParameters, Int::class.java)!!
} }
@ -707,8 +697,8 @@ functionality that is present only in the `JdbcTemplate` class, you can use the
`getJdbcOperations()` method to access the wrapped `JdbcTemplate` through the `getJdbcOperations()` method to access the wrapped `JdbcTemplate` through the
`JdbcOperations` interface. `JdbcOperations` interface.
See also xref:data-access/jdbc/core.adoc#jdbc-JdbcTemplate-idioms[`JdbcTemplate` Best Practices] for guidelines on using the See also xref:data-access/jdbc/core.adoc#jdbc-JdbcTemplate-idioms[`JdbcTemplate` Best Practices]
`NamedParameterJdbcTemplate` class in the context of an application. for guidelines on using the `NamedParameterJdbcTemplate` class in the context of an application.
[[jdbc-SQLExceptionTranslator]] [[jdbc-SQLExceptionTranslator]]

View File

@ -98,9 +98,9 @@ through its `key` attribute. You can use xref:core/expressions.adoc[SpEL] to pic
arguments of interest (or their nested properties), perform operations, or even arguments of interest (or their nested properties), perform operations, or even
invoke arbitrary methods without having to write any code or implement any interface. invoke arbitrary methods without having to write any code or implement any interface.
This is the recommended approach over the This is the recommended approach over the
xref:integration/cache/annotations.adoc#cache-annotations-cacheable-default-key[default generator], since methods tend to be xref:integration/cache/annotations.adoc#cache-annotations-cacheable-default-key[default generator],
quite different in signatures as the code base grows. While the default strategy might since methods tend to be quite different in signatures as the code base grows. While the
work for some methods, it rarely works for all methods. default strategy might work for some methods, it rarely works for all methods.
The following examples use various SpEL declarations (if you are not familiar with SpEL, The following examples use various SpEL declarations (if you are not familiar with SpEL,
do yourself a favor and read xref:core/expressions.adoc[Spring Expression Language]): do yourself a favor and read xref:core/expressions.adoc[Spring Expression Language]):
@ -137,9 +137,8 @@ that specifies both results in an exception.
[[cache-annotations-cacheable-default-cache-resolver]] [[cache-annotations-cacheable-default-cache-resolver]]
=== Default Cache Resolution === Default Cache Resolution
The caching abstraction uses a simple `CacheResolver` that The caching abstraction uses a simple `CacheResolver` that retrieves the caches
retrieves the caches defined at the operation level by using the configured defined at the operation level by using the configured `CacheManager`.
`CacheManager`.
To provide a different default cache resolver, you need to implement the To provide a different default cache resolver, you need to implement the
`org.springframework.cache.interceptor.CacheResolver` interface. `org.springframework.cache.interceptor.CacheResolver` interface.
@ -160,12 +159,11 @@ For applications that work with several cache managers, you can set the
---- ----
<1> Specifying `anotherCacheManager`. <1> Specifying `anotherCacheManager`.
You can also replace the `CacheResolver` entirely in a fashion similar to that of You can also replace the `CacheResolver` entirely in a fashion similar to that of
replacing xref:integration/cache/annotations.adoc#cache-annotations-cacheable-key[key generation]. The resolution is replacing xref:integration/cache/annotations.adoc#cache-annotations-cacheable-key[key generation].
requested for every cache operation, letting the implementation actually resolve The resolution is requested for every cache operation, letting the implementation
the caches to use based on runtime arguments. The following example shows how to actually resolve the caches to use based on runtime arguments. The following example
specify a `CacheResolver`: shows how to specify a `CacheResolver`:
[source,java,indent=0,subs="verbatim,quotes"] [source,java,indent=0,subs="verbatim,quotes"]
---- ----
@ -174,7 +172,6 @@ specify a `CacheResolver`:
---- ----
<1> Specifying the `CacheResolver`. <1> Specifying the `CacheResolver`.
[NOTE] [NOTE]
==== ====
Since Spring 4.1, the `value` attribute of the cache annotations are no longer Since Spring 4.1, the `value` attribute of the cache annotations are no longer
@ -229,7 +226,6 @@ argument `name` has a length shorter than 32:
---- ----
<1> Setting a condition on `@Cacheable`. <1> Setting a condition on `@Cacheable`.
In addition to the `condition` parameter, you can use the `unless` parameter to veto the In addition to the `condition` parameter, you can use the `unless` parameter to veto the
adding of a value to the cache. Unlike `condition`, `unless` expressions are evaluated adding of a value to the cache. Unlike `condition`, `unless` expressions are evaluated
after the method has been invoked. To expand on the previous example, perhaps we only after the method has been invoked. To expand on the previous example, perhaps we only
@ -242,7 +238,6 @@ want to cache paperback books, as the following example does:
---- ----
<1> Using the `unless` attribute to block hardbacks. <1> Using the `unless` attribute to block hardbacks.
The cache abstraction supports `java.util.Optional` return types. If an `Optional` value The cache abstraction supports `java.util.Optional` return types. If an `Optional` value
is _present_, it will be stored in the associated cache. If an `Optional` value is not is _present_, it will be stored in the associated cache. If an `Optional` value is not
present, `null` will be stored in the associated cache. `#result` always refers to the present, `null` will be stored in the associated cache. `#result` always refers to the
@ -344,7 +339,7 @@ confirm the exclusion.
[[cache-annotations-evict]] [[cache-annotations-evict]]
== The `@CacheEvict` annotation == The `@CacheEvict` Annotation
The cache abstraction allows not just population of a cache store but also eviction. The cache abstraction allows not just population of a cache store but also eviction.
This process is useful for removing stale or unused data from the cache. As opposed to This process is useful for removing stale or unused data from the cache. As opposed to
@ -402,7 +397,7 @@ The following example uses two `@CacheEvict` annotations:
[[cache-annotations-config]] [[cache-annotations-config]]
== The `@CacheConfig` annotation == The `@CacheConfig` Annotation
So far, we have seen that caching operations offer many customization options and that So far, we have seen that caching operations offer many customization options and that
you can set these options for each operation. However, some of the customization options you can set these options for each operation. However, some of the customization options

View File

@ -163,8 +163,7 @@ final class DefaultDatabaseClient implements DatabaseClient {
/** /**
* Release the {@link Connection}. * Release the {@link Connection}.
* @param connection to close. * @param connection to close.
* @return a {@link Publisher} that completes successfully when the connection is * @return a {@link Publisher} that completes successfully when the connection is closed
* closed
*/ */
private Publisher<Void> closeConnection(Connection connection) { private Publisher<Void> closeConnection(Connection connection) {
return ConnectionFactoryUtils.currentConnectionFactory( return ConnectionFactoryUtils.currentConnectionFactory(