NamedParameterJdbcTemplate provides accessor for classic JdbcTemplate
Issue: SPR-16241
This commit is contained in:
@@ -105,14 +105,26 @@ public class NamedParameterJdbcTemplate implements NamedParameterJdbcOperations
|
||||
|
||||
|
||||
/**
|
||||
* Expose the classic Spring JdbcTemplate to allow invocation of
|
||||
* less commonly used methods.
|
||||
* Expose the classic Spring JdbcTemplate operations to allow invocation
|
||||
* of less commonly used methods.
|
||||
*/
|
||||
@Override
|
||||
public JdbcOperations getJdbcOperations() {
|
||||
return this.classicJdbcTemplate;
|
||||
}
|
||||
|
||||
/**
|
||||
* Expose the classic Spring {@link JdbcTemplate} itself, if available,
|
||||
* in particular for passing it on to other {@code JdbcTemplate} consumers.
|
||||
* <p>If sufficient for the purposes at hand, {@link #getJdbcOperations()}
|
||||
* is recommended over this variant.
|
||||
* @since 5.0.3
|
||||
*/
|
||||
public JdbcTemplate getJdbcTemplate() {
|
||||
Assert.state(this.classicJdbcTemplate instanceof JdbcTemplate, "No JdbcTemplate available");
|
||||
return (JdbcTemplate) this.classicJdbcTemplate;
|
||||
}
|
||||
|
||||
/**
|
||||
* Specify the maximum number of entries for this template's SQL cache.
|
||||
* Default is 256.
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2016 the original author or authors.
|
||||
* Copyright 2002-2017 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -34,14 +34,10 @@ import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
|
||||
import org.springframework.dao.DataAccessException;
|
||||
import org.springframework.jdbc.Customer;
|
||||
import org.springframework.jdbc.core.JdbcOperations;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.jdbc.core.PreparedStatementCallback;
|
||||
import org.springframework.jdbc.core.ResultSetExtractor;
|
||||
import org.springframework.jdbc.core.RowCallbackHandler;
|
||||
import org.springframework.jdbc.core.RowMapper;
|
||||
import org.springframework.jdbc.core.SqlParameterValue;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
@@ -79,8 +75,9 @@ public class NamedParameterJdbcTemplateTests {
|
||||
private Map<String, Object> params = new HashMap<>();
|
||||
private NamedParameterJdbcTemplate namedParameterTemplate;
|
||||
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
public void setup() throws Exception {
|
||||
connection = mock(Connection.class);
|
||||
dataSource = mock(DataSource.class);
|
||||
preparedStatement = mock(PreparedStatement.class);
|
||||
@@ -95,18 +92,24 @@ public class NamedParameterJdbcTemplateTests {
|
||||
given(databaseMetaData.supportsBatchUpdates()).willReturn(true);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testNullDataSourceProvidedToCtor() throws Exception {
|
||||
public void testNullDataSourceProvidedToCtor() {
|
||||
thrown.expect(IllegalArgumentException.class);
|
||||
new NamedParameterJdbcTemplate((DataSource) null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNullJdbcTemplateProvidedToCtor() throws Exception {
|
||||
public void testNullJdbcTemplateProvidedToCtor() {
|
||||
thrown.expect(IllegalArgumentException.class);
|
||||
new NamedParameterJdbcTemplate((JdbcOperations) null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTemplateConfiguration() {
|
||||
assertSame(dataSource, namedParameterTemplate.getJdbcTemplate().getDataSource());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testExecute() throws SQLException {
|
||||
given(preparedStatement.executeUpdate()).willReturn(1);
|
||||
@@ -114,14 +117,10 @@ public class NamedParameterJdbcTemplateTests {
|
||||
params.put("perfId", 1);
|
||||
params.put("priceId", 1);
|
||||
Object result = namedParameterTemplate.execute(UPDATE_NAMED_PARAMETERS, params,
|
||||
new PreparedStatementCallback<Object>() {
|
||||
@Override
|
||||
public Object doInPreparedStatement(PreparedStatement ps)
|
||||
throws SQLException {
|
||||
assertEquals(preparedStatement, ps);
|
||||
ps.executeUpdate();
|
||||
return "result";
|
||||
}
|
||||
(PreparedStatementCallback<Object>) ps -> {
|
||||
assertEquals(preparedStatement, ps);
|
||||
ps.executeUpdate();
|
||||
return "result";
|
||||
});
|
||||
|
||||
assertEquals("result", result);
|
||||
@@ -139,14 +138,10 @@ public class NamedParameterJdbcTemplateTests {
|
||||
params.put("perfId", new SqlParameterValue(Types.DECIMAL, 1));
|
||||
params.put("priceId", new SqlParameterValue(Types.INTEGER, 1));
|
||||
Object result = namedParameterTemplate.execute(UPDATE_NAMED_PARAMETERS, params,
|
||||
new PreparedStatementCallback<Object>() {
|
||||
@Override
|
||||
public Object doInPreparedStatement(PreparedStatement ps)
|
||||
throws SQLException {
|
||||
assertEquals(preparedStatement, ps);
|
||||
ps.executeUpdate();
|
||||
return "result";
|
||||
}
|
||||
(PreparedStatementCallback<Object>) ps -> {
|
||||
assertEquals(preparedStatement, ps);
|
||||
ps.executeUpdate();
|
||||
return "result";
|
||||
});
|
||||
|
||||
assertEquals("result", result);
|
||||
@@ -162,14 +157,10 @@ public class NamedParameterJdbcTemplateTests {
|
||||
given(preparedStatement.executeUpdate()).willReturn(1);
|
||||
|
||||
Object result = namedParameterTemplate.execute(SELECT_NO_PARAMETERS,
|
||||
new PreparedStatementCallback<Object>() {
|
||||
@Override
|
||||
public Object doInPreparedStatement(PreparedStatement ps)
|
||||
throws SQLException {
|
||||
assertEquals(preparedStatement, ps);
|
||||
ps.executeQuery();
|
||||
return "result";
|
||||
}
|
||||
(PreparedStatementCallback<Object>) ps -> {
|
||||
assertEquals(preparedStatement, ps);
|
||||
ps.executeQuery();
|
||||
return "result";
|
||||
});
|
||||
|
||||
assertEquals("result", result);
|
||||
@@ -187,16 +178,12 @@ public class NamedParameterJdbcTemplateTests {
|
||||
params.put("id", new SqlParameterValue(Types.DECIMAL, 1));
|
||||
params.put("country", "UK");
|
||||
Customer cust = namedParameterTemplate.query(SELECT_NAMED_PARAMETERS, params,
|
||||
new ResultSetExtractor<Customer>() {
|
||||
@Override
|
||||
public Customer extractData(ResultSet rs) throws SQLException,
|
||||
DataAccessException {
|
||||
rs.next();
|
||||
Customer cust = new Customer();
|
||||
cust.setId(rs.getInt(COLUMN_NAMES[0]));
|
||||
cust.setForename(rs.getString(COLUMN_NAMES[1]));
|
||||
return cust;
|
||||
}
|
||||
rs -> {
|
||||
rs.next();
|
||||
Customer cust1 = new Customer();
|
||||
cust1.setId(rs.getInt(COLUMN_NAMES[0]));
|
||||
cust1.setForename(rs.getString(COLUMN_NAMES[1]));
|
||||
return cust1;
|
||||
});
|
||||
|
||||
assertTrue("Customer id was assigned correctly", cust.getId() == 1);
|
||||
@@ -215,16 +202,12 @@ public class NamedParameterJdbcTemplateTests {
|
||||
given(resultSet.getString("forename")).willReturn("rod");
|
||||
|
||||
Customer cust = namedParameterTemplate.query(SELECT_NO_PARAMETERS,
|
||||
new ResultSetExtractor<Customer>() {
|
||||
@Override
|
||||
public Customer extractData(ResultSet rs) throws SQLException,
|
||||
DataAccessException {
|
||||
rs.next();
|
||||
Customer cust = new Customer();
|
||||
cust.setId(rs.getInt(COLUMN_NAMES[0]));
|
||||
cust.setForename(rs.getString(COLUMN_NAMES[1]));
|
||||
return cust;
|
||||
}
|
||||
rs -> {
|
||||
rs.next();
|
||||
Customer cust1 = new Customer();
|
||||
cust1.setId(rs.getInt(COLUMN_NAMES[0]));
|
||||
cust1.setForename(rs.getString(COLUMN_NAMES[1]));
|
||||
return cust1;
|
||||
});
|
||||
|
||||
assertTrue("Customer id was assigned correctly", cust.getId() == 1);
|
||||
@@ -243,14 +226,11 @@ public class NamedParameterJdbcTemplateTests {
|
||||
params.put("id", new SqlParameterValue(Types.DECIMAL, 1));
|
||||
params.put("country", "UK");
|
||||
final List<Customer> customers = new LinkedList<>();
|
||||
namedParameterTemplate.query(SELECT_NAMED_PARAMETERS, params, new RowCallbackHandler() {
|
||||
@Override
|
||||
public void processRow(ResultSet rs) throws SQLException {
|
||||
Customer cust = new Customer();
|
||||
cust.setId(rs.getInt(COLUMN_NAMES[0]));
|
||||
cust.setForename(rs.getString(COLUMN_NAMES[1]));
|
||||
customers.add(cust);
|
||||
}
|
||||
namedParameterTemplate.query(SELECT_NAMED_PARAMETERS, params, rs -> {
|
||||
Customer cust = new Customer();
|
||||
cust.setId(rs.getInt(COLUMN_NAMES[0]));
|
||||
cust.setForename(rs.getString(COLUMN_NAMES[1]));
|
||||
customers.add(cust);
|
||||
});
|
||||
|
||||
assertEquals(1, customers.size());
|
||||
@@ -270,14 +250,11 @@ public class NamedParameterJdbcTemplateTests {
|
||||
given(resultSet.getString("forename")).willReturn("rod");
|
||||
|
||||
final List<Customer> customers = new LinkedList<>();
|
||||
namedParameterTemplate.query(SELECT_NO_PARAMETERS, new RowCallbackHandler() {
|
||||
@Override
|
||||
public void processRow(ResultSet rs) throws SQLException {
|
||||
Customer cust = new Customer();
|
||||
cust.setId(rs.getInt(COLUMN_NAMES[0]));
|
||||
cust.setForename(rs.getString(COLUMN_NAMES[1]));
|
||||
customers.add(cust);
|
||||
}
|
||||
namedParameterTemplate.query(SELECT_NO_PARAMETERS, rs -> {
|
||||
Customer cust = new Customer();
|
||||
cust.setId(rs.getInt(COLUMN_NAMES[0]));
|
||||
cust.setForename(rs.getString(COLUMN_NAMES[1]));
|
||||
customers.add(cust);
|
||||
});
|
||||
|
||||
assertEquals(1, customers.size());
|
||||
@@ -297,14 +274,11 @@ public class NamedParameterJdbcTemplateTests {
|
||||
params.put("id", new SqlParameterValue(Types.DECIMAL, 1));
|
||||
params.put("country", "UK");
|
||||
List<Customer> customers = namedParameterTemplate.query(SELECT_NAMED_PARAMETERS, params,
|
||||
new RowMapper<Customer>() {
|
||||
@Override
|
||||
public Customer mapRow(ResultSet rs, int rownum) throws SQLException {
|
||||
Customer cust = new Customer();
|
||||
cust.setId(rs.getInt(COLUMN_NAMES[0]));
|
||||
cust.setForename(rs.getString(COLUMN_NAMES[1]));
|
||||
return cust;
|
||||
}
|
||||
(rs, rownum) -> {
|
||||
Customer cust = new Customer();
|
||||
cust.setId(rs.getInt(COLUMN_NAMES[0]));
|
||||
cust.setForename(rs.getString(COLUMN_NAMES[1]));
|
||||
return cust;
|
||||
});
|
||||
assertEquals(1, customers.size());
|
||||
assertTrue("Customer id was assigned correctly", customers.get(0).getId() == 1);
|
||||
@@ -323,14 +297,11 @@ public class NamedParameterJdbcTemplateTests {
|
||||
given(resultSet.getString("forename")).willReturn("rod");
|
||||
|
||||
List<Customer> customers = namedParameterTemplate.query(SELECT_NO_PARAMETERS,
|
||||
new RowMapper<Customer>() {
|
||||
@Override
|
||||
public Customer mapRow(ResultSet rs, int rownum) throws SQLException {
|
||||
Customer cust = new Customer();
|
||||
cust.setId(rs.getInt(COLUMN_NAMES[0]));
|
||||
cust.setForename(rs.getString(COLUMN_NAMES[1]));
|
||||
return cust;
|
||||
}
|
||||
(rs, rownum) -> {
|
||||
Customer cust = new Customer();
|
||||
cust.setId(rs.getInt(COLUMN_NAMES[0]));
|
||||
cust.setForename(rs.getString(COLUMN_NAMES[1]));
|
||||
return cust;
|
||||
});
|
||||
assertEquals(1, customers.size());
|
||||
assertTrue("Customer id was assigned correctly", customers.get(0).getId() == 1);
|
||||
@@ -349,14 +320,11 @@ public class NamedParameterJdbcTemplateTests {
|
||||
params.put("id", new SqlParameterValue(Types.DECIMAL, 1));
|
||||
params.put("country", "UK");
|
||||
Customer cust = namedParameterTemplate.queryForObject(SELECT_NAMED_PARAMETERS, params,
|
||||
new RowMapper<Customer>() {
|
||||
@Override
|
||||
public Customer mapRow(ResultSet rs, int rownum) throws SQLException {
|
||||
Customer cust = new Customer();
|
||||
cust.setId(rs.getInt(COLUMN_NAMES[0]));
|
||||
cust.setForename(rs.getString(COLUMN_NAMES[1]));
|
||||
return cust;
|
||||
}
|
||||
(rs, rownum) -> {
|
||||
Customer cust1 = new Customer();
|
||||
cust1.setId(rs.getInt(COLUMN_NAMES[0]));
|
||||
cust1.setForename(rs.getString(COLUMN_NAMES[1]));
|
||||
return cust1;
|
||||
});
|
||||
assertTrue("Customer id was assigned correctly", cust.getId() == 1);
|
||||
assertTrue("Customer forename was assigned correctly", cust.getForename().equals("rod"));
|
||||
@@ -405,15 +373,16 @@ public class NamedParameterJdbcTemplateTests {
|
||||
final Map<String, Integer>[] ids = new Map[2];
|
||||
ids[0] = Collections.singletonMap("id", 100);
|
||||
ids[1] = Collections.singletonMap("id", 200);
|
||||
final int[] rowsAffected = new int[] { 1, 2 };
|
||||
final int[] rowsAffected = new int[] {1, 2};
|
||||
|
||||
given(preparedStatement.executeBatch()).willReturn(rowsAffected);
|
||||
given(connection.getMetaData()).willReturn(databaseMetaData);
|
||||
|
||||
JdbcTemplate template = new JdbcTemplate(dataSource, false);
|
||||
namedParameterTemplate = new NamedParameterJdbcTemplate(template);
|
||||
int[] actualRowsAffected = namedParameterTemplate.batchUpdate("UPDATE NOSUCHTABLE SET DATE_DISPATCHED = SYSDATE WHERE ID = :id", ids);
|
||||
assertSame(template, namedParameterTemplate.getJdbcTemplate());
|
||||
|
||||
int[] actualRowsAffected = namedParameterTemplate.batchUpdate("UPDATE NOSUCHTABLE SET DATE_DISPATCHED = SYSDATE WHERE ID = :id", ids);
|
||||
assertTrue("executed 2 updates", actualRowsAffected.length == 2);
|
||||
assertEquals(rowsAffected[0], actualRowsAffected[0]);
|
||||
assertEquals(rowsAffected[1], actualRowsAffected[1]);
|
||||
@@ -430,15 +399,16 @@ public class NamedParameterJdbcTemplateTests {
|
||||
SqlParameterSource[] ids = new SqlParameterSource[2];
|
||||
ids[0] = new MapSqlParameterSource("id", 100);
|
||||
ids[1] = new MapSqlParameterSource("id", 200);
|
||||
final int[] rowsAffected = new int[] { 1, 2 };
|
||||
final int[] rowsAffected = new int[] {1, 2};
|
||||
|
||||
given(preparedStatement.executeBatch()).willReturn(rowsAffected);
|
||||
given(connection.getMetaData()).willReturn(databaseMetaData);
|
||||
|
||||
JdbcTemplate template = new JdbcTemplate(dataSource, false);
|
||||
namedParameterTemplate = new NamedParameterJdbcTemplate(template);
|
||||
int[] actualRowsAffected = namedParameterTemplate.batchUpdate("UPDATE NOSUCHTABLE SET DATE_DISPATCHED = SYSDATE WHERE ID = :id", ids);
|
||||
assertSame(template, namedParameterTemplate.getJdbcTemplate());
|
||||
|
||||
int[] actualRowsAffected = namedParameterTemplate.batchUpdate("UPDATE NOSUCHTABLE SET DATE_DISPATCHED = SYSDATE WHERE ID = :id", ids);
|
||||
assertTrue("executed 2 updates", actualRowsAffected.length == 2);
|
||||
assertEquals(rowsAffected[0], actualRowsAffected[0]);
|
||||
assertEquals(rowsAffected[1], actualRowsAffected[1]);
|
||||
@@ -455,15 +425,16 @@ public class NamedParameterJdbcTemplateTests {
|
||||
SqlParameterSource[] ids = new SqlParameterSource[2];
|
||||
ids[0] = new MapSqlParameterSource().addValue("id", 100, Types.NUMERIC);
|
||||
ids[1] = new MapSqlParameterSource().addValue("id", 200, Types.NUMERIC);
|
||||
final int[] rowsAffected = new int[] { 1, 2 };
|
||||
final int[] rowsAffected = new int[] {1, 2};
|
||||
|
||||
given(preparedStatement.executeBatch()).willReturn(rowsAffected);
|
||||
given(connection.getMetaData()).willReturn(databaseMetaData);
|
||||
|
||||
JdbcTemplate template = new JdbcTemplate(dataSource, false);
|
||||
namedParameterTemplate = new NamedParameterJdbcTemplate(template);
|
||||
int[] actualRowsAffected = namedParameterTemplate.batchUpdate("UPDATE NOSUCHTABLE SET DATE_DISPATCHED = SYSDATE WHERE ID = :id", ids);
|
||||
assertSame(template, namedParameterTemplate.getJdbcTemplate());
|
||||
|
||||
int[] actualRowsAffected = namedParameterTemplate.batchUpdate("UPDATE NOSUCHTABLE SET DATE_DISPATCHED = SYSDATE WHERE ID = :id", ids);
|
||||
assertTrue("executed 2 updates", actualRowsAffected.length == 2);
|
||||
assertEquals(rowsAffected[0], actualRowsAffected[0]);
|
||||
assertEquals(rowsAffected[1], actualRowsAffected[1]);
|
||||
|
||||
Reference in New Issue
Block a user