Skip to content

Commit 23eb0e3

Browse files
committed
Polishing
(cherry picked from commit bbde68c)
1 parent 840bd57 commit 23eb0e3

File tree

5 files changed

+35
-42
lines changed

5 files changed

+35
-42
lines changed

spring-jdbc/src/main/java/org/springframework/jdbc/core/JdbcOperations.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2021 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -35,12 +35,13 @@
3535
* <p>Alternatively, the standard JDBC infrastructure can be mocked.
3636
* However, mocking this interface constitutes significantly less work.
3737
* As an alternative to a mock objects approach to testing data access code,
38-
* consider the powerful integration testing support provided via the <em>Spring
39-
* TestContext Framework</em>, in the {@code spring-test} artifact.
38+
* consider the powerful integration testing support provided via the
39+
* <em>Spring TestContext Framework</em>, in the {@code spring-test} artifact.
4040
*
4141
* @author Rod Johnson
4242
* @author Juergen Hoeller
4343
* @see JdbcTemplate
44+
* @see org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations
4445
*/
4546
public interface JdbcOperations {
4647

spring-jdbc/src/main/java/org/springframework/jdbc/core/JdbcTemplate.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,7 @@
6565
* It executes core JDBC workflow, leaving application code to provide SQL
6666
* and extract results. This class executes SQL queries or updates, initiating
6767
* iteration over ResultSets and catching JDBC exceptions and translating
68-
* them to the generic, more informative exception hierarchy defined in the
69-
* {@code org.springframework.dao} package.
68+
* them to the common {@code org.springframework.dao} exception hierarchy.
7069
*
7170
* <p>Code using this class need only implement callback interfaces, giving
7271
* them a clearly defined contract. The {@link PreparedStatementCreator} callback
@@ -75,7 +74,8 @@
7574
* values from a ResultSet. See also {@link PreparedStatementSetter} and
7675
* {@link RowMapper} for two popular alternative callback interfaces.
7776
*
78-
* <p>Can be used within a service implementation via direct instantiation
77+
* <p>An instance of this template class is thread-safe once configured.
78+
* Can be used within a service implementation via direct instantiation
7979
* with a DataSource reference, or get prepared in an application context
8080
* and given to services as bean reference. Note: The DataSource should
8181
* always be configured as a bean in the application context, in the first case
@@ -88,12 +88,11 @@
8888
* <p>All SQL operations performed by this class are logged at debug level,
8989
* using "org.springframework.jdbc.core.JdbcTemplate" as log category.
9090
*
91-
* <p><b>NOTE: An instance of this class is thread-safe once configured.</b>
92-
*
9391
* @author Rod Johnson
9492
* @author Juergen Hoeller
9593
* @author Thomas Risberg
9694
* @since May 3, 2001
95+
* @see JdbcOperations
9796
* @see PreparedStatementCreator
9897
* @see PreparedStatementSetter
9998
* @see CallableStatementCreator
@@ -103,6 +102,7 @@
103102
* @see RowCallbackHandler
104103
* @see RowMapper
105104
* @see org.springframework.jdbc.support.SQLExceptionTranslator
105+
* @see org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate
106106
*/
107107
public class JdbcTemplate extends JdbcAccessor implements JdbcOperations {
108108

spring-jdbc/src/main/java/org/springframework/jdbc/core/namedparam/BeanPropertySqlParameterSource.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -32,9 +32,10 @@
3232
/**
3333
* {@link SqlParameterSource} implementation that obtains parameter values
3434
* from bean properties of a given JavaBean object. The names of the bean
35-
* properties have to match the parameter names.
35+
* properties have to match the parameter names. Supports components of
36+
* record classes as well, with accessor methods matching parameter names.
3637
*
37-
* <p>Uses a Spring BeanWrapper for bean property access underneath.
38+
* <p>Uses a Spring {@link BeanWrapper} for bean property access underneath.
3839
*
3940
* @author Thomas Risberg
4041
* @author Juergen Hoeller

spring-jdbc/src/main/java/org/springframework/jdbc/core/namedparam/NamedParameterJdbcTemplate.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -55,16 +55,19 @@
5555
* done at execution time. It also allows for expanding a {@link java.util.List}
5656
* of values to the appropriate number of placeholders.
5757
*
58-
* <p>The underlying {@link org.springframework.jdbc.core.JdbcTemplate} is
58+
* <p>An instance of this template class is thread-safe once configured.
59+
* The underlying {@link org.springframework.jdbc.core.JdbcTemplate} is
5960
* exposed to allow for convenient access to the traditional
6061
* {@link org.springframework.jdbc.core.JdbcTemplate} methods.
6162
*
62-
* <p><b>NOTE: An instance of this class is thread-safe once configured.</b>
63-
*
6463
* @author Thomas Risberg
6564
* @author Juergen Hoeller
6665
* @since 2.0
6766
* @see NamedParameterJdbcOperations
67+
* @see SqlParameterSource
68+
* @see ResultSetExtractor
69+
* @see RowCallbackHandler
70+
* @see RowMapper
6871
* @see org.springframework.jdbc.core.JdbcTemplate
6972
*/
7073
public class NamedParameterJdbcTemplate implements NamedParameterJdbcOperations {

spring-jdbc/src/test/java/org/springframework/jdbc/core/namedparam/NamedParameterQueryTests.java

Lines changed: 15 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -34,8 +34,6 @@
3434
import org.junit.jupiter.api.BeforeEach;
3535
import org.junit.jupiter.api.Test;
3636

37-
import org.springframework.jdbc.core.RowMapper;
38-
3937
import static org.assertj.core.api.Assertions.assertThat;
4038
import static org.mockito.ArgumentMatchers.anyString;
4139
import static org.mockito.BDDMockito.given;
@@ -135,8 +133,7 @@ public void testQueryForListWithParamMapAndSingleRowAndColumn() throws Exception
135133
}
136134

137135
@Test
138-
public void testQueryForListWithParamMapAndIntegerElementAndSingleRowAndColumn()
139-
throws Exception {
136+
public void testQueryForListWithParamMapAndIntegerElementAndSingleRowAndColumn() throws Exception {
140137
given(resultSet.getMetaData()).willReturn(resultSetMetaData);
141138
given(resultSet.next()).willReturn(true, false);
142139
given(resultSet.getInt(1)).willReturn(11);
@@ -174,11 +171,10 @@ public void testQueryForObjectWithParamMapAndRowMapper() throws Exception {
174171

175172
MapSqlParameterSource params = new MapSqlParameterSource();
176173
params.addValue("id", 3);
177-
Object o = template.queryForObject("SELECT AGE FROM CUSTMR WHERE ID = :id",
178-
params, (RowMapper<Object>) (rs, rowNum) -> rs.getInt(1));
174+
Integer value = template.queryForObject("SELECT AGE FROM CUSTMR WHERE ID = :id",
175+
params, (rs, rowNum) -> rs.getInt(1));
179176

180-
boolean condition = o instanceof Integer;
181-
assertThat(condition).as("Correct result type").isTrue();
177+
assertThat(value).isEqualTo(22);
182178
verify(connection).prepareStatement("SELECT AGE FROM CUSTMR WHERE ID = ?");
183179
verify(preparedStatement).setObject(1, 3);
184180
}
@@ -191,11 +187,10 @@ public void testQueryForObjectWithMapAndInteger() throws Exception {
191187

192188
Map<String, Object> params = new HashMap<>();
193189
params.put("id", 3);
194-
Object o = template.queryForObject("SELECT AGE FROM CUSTMR WHERE ID = :id",
190+
Integer value = template.queryForObject("SELECT AGE FROM CUSTMR WHERE ID = :id",
195191
params, Integer.class);
196192

197-
boolean condition = o instanceof Integer;
198-
assertThat(condition).as("Correct result type").isTrue();
193+
assertThat(value).isEqualTo(22);
199194
verify(connection).prepareStatement("SELECT AGE FROM CUSTMR WHERE ID = ?");
200195
verify(preparedStatement).setObject(1, 3);
201196
}
@@ -208,30 +203,26 @@ public void testQueryForObjectWithParamMapAndInteger() throws Exception {
208203

209204
MapSqlParameterSource params = new MapSqlParameterSource();
210205
params.addValue("id", 3);
211-
Object o = template.queryForObject("SELECT AGE FROM CUSTMR WHERE ID = :id",
206+
Integer value = template.queryForObject("SELECT AGE FROM CUSTMR WHERE ID = :id",
212207
params, Integer.class);
213208

214-
boolean condition = o instanceof Integer;
215-
assertThat(condition).as("Correct result type").isTrue();
209+
assertThat(value).isEqualTo(22);
216210
verify(connection).prepareStatement("SELECT AGE FROM CUSTMR WHERE ID = ?");
217211
verify(preparedStatement).setObject(1, 3);
218212
}
219213

220214
@Test
221215
public void testQueryForObjectWithParamMapAndList() throws Exception {
222-
String sql = "SELECT AGE FROM CUSTMR WHERE ID IN (:ids)";
223-
String sqlToUse = "SELECT AGE FROM CUSTMR WHERE ID IN (?, ?)";
224216
given(resultSet.getMetaData()).willReturn(resultSetMetaData);
225217
given(resultSet.next()).willReturn(true, false);
226218
given(resultSet.getInt(1)).willReturn(22);
227219

228220
MapSqlParameterSource params = new MapSqlParameterSource();
229221
params.addValue("ids", Arrays.asList(3, 4));
230-
Object o = template.queryForObject(sql, params, Integer.class);
222+
Integer value = template.queryForObject("SELECT AGE FROM CUSTMR WHERE ID IN (:ids)", params, Integer.class);
231223

232-
boolean condition = o instanceof Integer;
233-
assertThat(condition).as("Correct result type").isTrue();
234-
verify(connection).prepareStatement(sqlToUse);
224+
assertThat(value).isEqualTo(22);
225+
verify(connection).prepareStatement("SELECT AGE FROM CUSTMR WHERE ID IN (?, ?)");
235226
verify(preparedStatement).setObject(1, 3);
236227
}
237228

@@ -246,14 +237,11 @@ public void testQueryForObjectWithParamMapAndListOfExpressionLists() throws Exce
246237
l1.add(new Object[] {3, "Rod"});
247238
l1.add(new Object[] {4, "Juergen"});
248239
params.addValue("multiExpressionList", l1);
249-
Object o = template.queryForObject(
250-
"SELECT AGE FROM CUSTMR WHERE (ID, NAME) IN (:multiExpressionList)",
240+
Integer value = template.queryForObject("SELECT AGE FROM CUSTMR WHERE (ID, NAME) IN (:multiExpressionList)",
251241
params, Integer.class);
252242

253-
boolean condition = o instanceof Integer;
254-
assertThat(condition).as("Correct result type").isTrue();
255-
verify(connection).prepareStatement(
256-
"SELECT AGE FROM CUSTMR WHERE (ID, NAME) IN ((?, ?), (?, ?))");
243+
assertThat(value).isEqualTo(22);
244+
verify(connection).prepareStatement("SELECT AGE FROM CUSTMR WHERE (ID, NAME) IN ((?, ?), (?, ?))");
257245
verify(preparedStatement).setObject(1, 3);
258246
}
259247

0 commit comments

Comments
 (0)