Skip to content

Commit b55a263

Browse files
committed
NamedParameterJdbcTemplate provides accessor for classic JdbcTemplate
Issue: SPR-16241
1 parent 6f73b8b commit b55a263

2 files changed

Lines changed: 83 additions & 100 deletions

File tree

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,14 +105,26 @@ public NamedParameterJdbcTemplate(JdbcOperations classicJdbcTemplate) {
105105

106106

107107
/**
108-
* Expose the classic Spring JdbcTemplate to allow invocation of
109-
* less commonly used methods.
108+
* Expose the classic Spring JdbcTemplate operations to allow invocation
109+
* of less commonly used methods.
110110
*/
111111
@Override
112112
public JdbcOperations getJdbcOperations() {
113113
return this.classicJdbcTemplate;
114114
}
115115

116+
/**
117+
* Expose the classic Spring {@link JdbcTemplate} itself, if available,
118+
* in particular for passing it on to other {@code JdbcTemplate} consumers.
119+
* <p>If sufficient for the purposes at hand, {@link #getJdbcOperations()}
120+
* is recommended over this variant.
121+
* @since 5.0.3
122+
*/
123+
public JdbcTemplate getJdbcTemplate() {
124+
Assert.state(this.classicJdbcTemplate instanceof JdbcTemplate, "No JdbcTemplate available");
125+
return (JdbcTemplate) this.classicJdbcTemplate;
126+
}
127+
116128
/**
117129
* Specify the maximum number of entries for this template's SQL cache.
118130
* Default is 256.

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

Lines changed: 69 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2017 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,14 +34,10 @@
3434
import org.junit.Test;
3535
import org.junit.rules.ExpectedException;
3636

37-
import org.springframework.dao.DataAccessException;
3837
import org.springframework.jdbc.Customer;
3938
import org.springframework.jdbc.core.JdbcOperations;
4039
import org.springframework.jdbc.core.JdbcTemplate;
4140
import org.springframework.jdbc.core.PreparedStatementCallback;
42-
import org.springframework.jdbc.core.ResultSetExtractor;
43-
import org.springframework.jdbc.core.RowCallbackHandler;
44-
import org.springframework.jdbc.core.RowMapper;
4541
import org.springframework.jdbc.core.SqlParameterValue;
4642

4743
import static org.junit.Assert.*;
@@ -79,8 +75,9 @@ public class NamedParameterJdbcTemplateTests {
7975
private Map<String, Object> params = new HashMap<>();
8076
private NamedParameterJdbcTemplate namedParameterTemplate;
8177

78+
8279
@Before
83-
public void setUp() throws Exception {
80+
public void setup() throws Exception {
8481
connection = mock(Connection.class);
8582
dataSource = mock(DataSource.class);
8683
preparedStatement = mock(PreparedStatement.class);
@@ -95,33 +92,35 @@ public void setUp() throws Exception {
9592
given(databaseMetaData.supportsBatchUpdates()).willReturn(true);
9693
}
9794

95+
9896
@Test
99-
public void testNullDataSourceProvidedToCtor() throws Exception {
97+
public void testNullDataSourceProvidedToCtor() {
10098
thrown.expect(IllegalArgumentException.class);
10199
new NamedParameterJdbcTemplate((DataSource) null);
102100
}
103101

104102
@Test
105-
public void testNullJdbcTemplateProvidedToCtor() throws Exception {
103+
public void testNullJdbcTemplateProvidedToCtor() {
106104
thrown.expect(IllegalArgumentException.class);
107105
new NamedParameterJdbcTemplate((JdbcOperations) null);
108106
}
109107

108+
@Test
109+
public void testTemplateConfiguration() {
110+
assertSame(dataSource, namedParameterTemplate.getJdbcTemplate().getDataSource());
111+
}
112+
110113
@Test
111114
public void testExecute() throws SQLException {
112115
given(preparedStatement.executeUpdate()).willReturn(1);
113116

114117
params.put("perfId", 1);
115118
params.put("priceId", 1);
116119
Object result = namedParameterTemplate.execute(UPDATE_NAMED_PARAMETERS, params,
117-
new PreparedStatementCallback<Object>() {
118-
@Override
119-
public Object doInPreparedStatement(PreparedStatement ps)
120-
throws SQLException {
121-
assertEquals(preparedStatement, ps);
122-
ps.executeUpdate();
123-
return "result";
124-
}
120+
(PreparedStatementCallback<Object>) ps -> {
121+
assertEquals(preparedStatement, ps);
122+
ps.executeUpdate();
123+
return "result";
125124
});
126125

127126
assertEquals("result", result);
@@ -139,14 +138,10 @@ public void testExecuteWithTypedParameters() throws SQLException {
139138
params.put("perfId", new SqlParameterValue(Types.DECIMAL, 1));
140139
params.put("priceId", new SqlParameterValue(Types.INTEGER, 1));
141140
Object result = namedParameterTemplate.execute(UPDATE_NAMED_PARAMETERS, params,
142-
new PreparedStatementCallback<Object>() {
143-
@Override
144-
public Object doInPreparedStatement(PreparedStatement ps)
145-
throws SQLException {
146-
assertEquals(preparedStatement, ps);
147-
ps.executeUpdate();
148-
return "result";
149-
}
141+
(PreparedStatementCallback<Object>) ps -> {
142+
assertEquals(preparedStatement, ps);
143+
ps.executeUpdate();
144+
return "result";
150145
});
151146

152147
assertEquals("result", result);
@@ -162,14 +157,10 @@ public void testExecuteNoParameters() throws SQLException {
162157
given(preparedStatement.executeUpdate()).willReturn(1);
163158

164159
Object result = namedParameterTemplate.execute(SELECT_NO_PARAMETERS,
165-
new PreparedStatementCallback<Object>() {
166-
@Override
167-
public Object doInPreparedStatement(PreparedStatement ps)
168-
throws SQLException {
169-
assertEquals(preparedStatement, ps);
170-
ps.executeQuery();
171-
return "result";
172-
}
160+
(PreparedStatementCallback<Object>) ps -> {
161+
assertEquals(preparedStatement, ps);
162+
ps.executeQuery();
163+
return "result";
173164
});
174165

175166
assertEquals("result", result);
@@ -187,16 +178,12 @@ public void testQueryWithResultSetExtractor() throws SQLException {
187178
params.put("id", new SqlParameterValue(Types.DECIMAL, 1));
188179
params.put("country", "UK");
189180
Customer cust = namedParameterTemplate.query(SELECT_NAMED_PARAMETERS, params,
190-
new ResultSetExtractor<Customer>() {
191-
@Override
192-
public Customer extractData(ResultSet rs) throws SQLException,
193-
DataAccessException {
194-
rs.next();
195-
Customer cust = new Customer();
196-
cust.setId(rs.getInt(COLUMN_NAMES[0]));
197-
cust.setForename(rs.getString(COLUMN_NAMES[1]));
198-
return cust;
199-
}
181+
rs -> {
182+
rs.next();
183+
Customer cust1 = new Customer();
184+
cust1.setId(rs.getInt(COLUMN_NAMES[0]));
185+
cust1.setForename(rs.getString(COLUMN_NAMES[1]));
186+
return cust1;
200187
});
201188

202189
assertTrue("Customer id was assigned correctly", cust.getId() == 1);
@@ -215,16 +202,12 @@ public void testQueryWithResultSetExtractorNoParameters() throws SQLException {
215202
given(resultSet.getString("forename")).willReturn("rod");
216203

217204
Customer cust = namedParameterTemplate.query(SELECT_NO_PARAMETERS,
218-
new ResultSetExtractor<Customer>() {
219-
@Override
220-
public Customer extractData(ResultSet rs) throws SQLException,
221-
DataAccessException {
222-
rs.next();
223-
Customer cust = new Customer();
224-
cust.setId(rs.getInt(COLUMN_NAMES[0]));
225-
cust.setForename(rs.getString(COLUMN_NAMES[1]));
226-
return cust;
227-
}
205+
rs -> {
206+
rs.next();
207+
Customer cust1 = new Customer();
208+
cust1.setId(rs.getInt(COLUMN_NAMES[0]));
209+
cust1.setForename(rs.getString(COLUMN_NAMES[1]));
210+
return cust1;
228211
});
229212

230213
assertTrue("Customer id was assigned correctly", cust.getId() == 1);
@@ -243,14 +226,11 @@ public void testQueryWithRowCallbackHandler() throws SQLException {
243226
params.put("id", new SqlParameterValue(Types.DECIMAL, 1));
244227
params.put("country", "UK");
245228
final List<Customer> customers = new LinkedList<>();
246-
namedParameterTemplate.query(SELECT_NAMED_PARAMETERS, params, new RowCallbackHandler() {
247-
@Override
248-
public void processRow(ResultSet rs) throws SQLException {
249-
Customer cust = new Customer();
250-
cust.setId(rs.getInt(COLUMN_NAMES[0]));
251-
cust.setForename(rs.getString(COLUMN_NAMES[1]));
252-
customers.add(cust);
253-
}
229+
namedParameterTemplate.query(SELECT_NAMED_PARAMETERS, params, rs -> {
230+
Customer cust = new Customer();
231+
cust.setId(rs.getInt(COLUMN_NAMES[0]));
232+
cust.setForename(rs.getString(COLUMN_NAMES[1]));
233+
customers.add(cust);
254234
});
255235

256236
assertEquals(1, customers.size());
@@ -270,14 +250,11 @@ public void testQueryWithRowCallbackHandlerNoParameters() throws SQLException {
270250
given(resultSet.getString("forename")).willReturn("rod");
271251

272252
final List<Customer> customers = new LinkedList<>();
273-
namedParameterTemplate.query(SELECT_NO_PARAMETERS, new RowCallbackHandler() {
274-
@Override
275-
public void processRow(ResultSet rs) throws SQLException {
276-
Customer cust = new Customer();
277-
cust.setId(rs.getInt(COLUMN_NAMES[0]));
278-
cust.setForename(rs.getString(COLUMN_NAMES[1]));
279-
customers.add(cust);
280-
}
253+
namedParameterTemplate.query(SELECT_NO_PARAMETERS, rs -> {
254+
Customer cust = new Customer();
255+
cust.setId(rs.getInt(COLUMN_NAMES[0]));
256+
cust.setForename(rs.getString(COLUMN_NAMES[1]));
257+
customers.add(cust);
281258
});
282259

283260
assertEquals(1, customers.size());
@@ -297,14 +274,11 @@ public void testQueryWithRowMapper() throws SQLException {
297274
params.put("id", new SqlParameterValue(Types.DECIMAL, 1));
298275
params.put("country", "UK");
299276
List<Customer> customers = namedParameterTemplate.query(SELECT_NAMED_PARAMETERS, params,
300-
new RowMapper<Customer>() {
301-
@Override
302-
public Customer mapRow(ResultSet rs, int rownum) throws SQLException {
303-
Customer cust = new Customer();
304-
cust.setId(rs.getInt(COLUMN_NAMES[0]));
305-
cust.setForename(rs.getString(COLUMN_NAMES[1]));
306-
return cust;
307-
}
277+
(rs, rownum) -> {
278+
Customer cust = new Customer();
279+
cust.setId(rs.getInt(COLUMN_NAMES[0]));
280+
cust.setForename(rs.getString(COLUMN_NAMES[1]));
281+
return cust;
308282
});
309283
assertEquals(1, customers.size());
310284
assertTrue("Customer id was assigned correctly", customers.get(0).getId() == 1);
@@ -323,14 +297,11 @@ public void testQueryWithRowMapperNoParameters() throws SQLException {
323297
given(resultSet.getString("forename")).willReturn("rod");
324298

325299
List<Customer> customers = namedParameterTemplate.query(SELECT_NO_PARAMETERS,
326-
new RowMapper<Customer>() {
327-
@Override
328-
public Customer mapRow(ResultSet rs, int rownum) throws SQLException {
329-
Customer cust = new Customer();
330-
cust.setId(rs.getInt(COLUMN_NAMES[0]));
331-
cust.setForename(rs.getString(COLUMN_NAMES[1]));
332-
return cust;
333-
}
300+
(rs, rownum) -> {
301+
Customer cust = new Customer();
302+
cust.setId(rs.getInt(COLUMN_NAMES[0]));
303+
cust.setForename(rs.getString(COLUMN_NAMES[1]));
304+
return cust;
334305
});
335306
assertEquals(1, customers.size());
336307
assertTrue("Customer id was assigned correctly", customers.get(0).getId() == 1);
@@ -349,14 +320,11 @@ public void testQueryForObjectWithRowMapper() throws SQLException {
349320
params.put("id", new SqlParameterValue(Types.DECIMAL, 1));
350321
params.put("country", "UK");
351322
Customer cust = namedParameterTemplate.queryForObject(SELECT_NAMED_PARAMETERS, params,
352-
new RowMapper<Customer>() {
353-
@Override
354-
public Customer mapRow(ResultSet rs, int rownum) throws SQLException {
355-
Customer cust = new Customer();
356-
cust.setId(rs.getInt(COLUMN_NAMES[0]));
357-
cust.setForename(rs.getString(COLUMN_NAMES[1]));
358-
return cust;
359-
}
323+
(rs, rownum) -> {
324+
Customer cust1 = new Customer();
325+
cust1.setId(rs.getInt(COLUMN_NAMES[0]));
326+
cust1.setForename(rs.getString(COLUMN_NAMES[1]));
327+
return cust1;
360328
});
361329
assertTrue("Customer id was assigned correctly", cust.getId() == 1);
362330
assertTrue("Customer forename was assigned correctly", cust.getForename().equals("rod"));
@@ -405,15 +373,16 @@ public void testBatchUpdateWithPlainMap() throws Exception {
405373
final Map<String, Integer>[] ids = new Map[2];
406374
ids[0] = Collections.singletonMap("id", 100);
407375
ids[1] = Collections.singletonMap("id", 200);
408-
final int[] rowsAffected = new int[] { 1, 2 };
376+
final int[] rowsAffected = new int[] {1, 2};
409377

410378
given(preparedStatement.executeBatch()).willReturn(rowsAffected);
411379
given(connection.getMetaData()).willReturn(databaseMetaData);
412380

413381
JdbcTemplate template = new JdbcTemplate(dataSource, false);
414382
namedParameterTemplate = new NamedParameterJdbcTemplate(template);
415-
int[] actualRowsAffected = namedParameterTemplate.batchUpdate("UPDATE NOSUCHTABLE SET DATE_DISPATCHED = SYSDATE WHERE ID = :id", ids);
383+
assertSame(template, namedParameterTemplate.getJdbcTemplate());
416384

385+
int[] actualRowsAffected = namedParameterTemplate.batchUpdate("UPDATE NOSUCHTABLE SET DATE_DISPATCHED = SYSDATE WHERE ID = :id", ids);
417386
assertTrue("executed 2 updates", actualRowsAffected.length == 2);
418387
assertEquals(rowsAffected[0], actualRowsAffected[0]);
419388
assertEquals(rowsAffected[1], actualRowsAffected[1]);
@@ -430,15 +399,16 @@ public void testBatchUpdateWithSqlParameterSource() throws Exception {
430399
SqlParameterSource[] ids = new SqlParameterSource[2];
431400
ids[0] = new MapSqlParameterSource("id", 100);
432401
ids[1] = new MapSqlParameterSource("id", 200);
433-
final int[] rowsAffected = new int[] { 1, 2 };
402+
final int[] rowsAffected = new int[] {1, 2};
434403

435404
given(preparedStatement.executeBatch()).willReturn(rowsAffected);
436405
given(connection.getMetaData()).willReturn(databaseMetaData);
437406

438407
JdbcTemplate template = new JdbcTemplate(dataSource, false);
439408
namedParameterTemplate = new NamedParameterJdbcTemplate(template);
440-
int[] actualRowsAffected = namedParameterTemplate.batchUpdate("UPDATE NOSUCHTABLE SET DATE_DISPATCHED = SYSDATE WHERE ID = :id", ids);
409+
assertSame(template, namedParameterTemplate.getJdbcTemplate());
441410

411+
int[] actualRowsAffected = namedParameterTemplate.batchUpdate("UPDATE NOSUCHTABLE SET DATE_DISPATCHED = SYSDATE WHERE ID = :id", ids);
442412
assertTrue("executed 2 updates", actualRowsAffected.length == 2);
443413
assertEquals(rowsAffected[0], actualRowsAffected[0]);
444414
assertEquals(rowsAffected[1], actualRowsAffected[1]);
@@ -455,15 +425,16 @@ public void testBatchUpdateWithSqlParameterSourcePlusTypeInfo() throws Exception
455425
SqlParameterSource[] ids = new SqlParameterSource[2];
456426
ids[0] = new MapSqlParameterSource().addValue("id", 100, Types.NUMERIC);
457427
ids[1] = new MapSqlParameterSource().addValue("id", 200, Types.NUMERIC);
458-
final int[] rowsAffected = new int[] { 1, 2 };
428+
final int[] rowsAffected = new int[] {1, 2};
459429

460430
given(preparedStatement.executeBatch()).willReturn(rowsAffected);
461431
given(connection.getMetaData()).willReturn(databaseMetaData);
462432

463433
JdbcTemplate template = new JdbcTemplate(dataSource, false);
464434
namedParameterTemplate = new NamedParameterJdbcTemplate(template);
465-
int[] actualRowsAffected = namedParameterTemplate.batchUpdate("UPDATE NOSUCHTABLE SET DATE_DISPATCHED = SYSDATE WHERE ID = :id", ids);
435+
assertSame(template, namedParameterTemplate.getJdbcTemplate());
466436

437+
int[] actualRowsAffected = namedParameterTemplate.batchUpdate("UPDATE NOSUCHTABLE SET DATE_DISPATCHED = SYSDATE WHERE ID = :id", ids);
467438
assertTrue("executed 2 updates", actualRowsAffected.length == 2);
468439
assertEquals(rowsAffected[0], actualRowsAffected[0]);
469440
assertEquals(rowsAffected[1], actualRowsAffected[1]);

0 commit comments

Comments
 (0)