From b55a2631ba07beb143d30563e9b72c364a6e473a Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Wed, 29 Nov 2017 15:38:21 +0100 Subject: [PATCH] NamedParameterJdbcTemplate provides accessor for classic JdbcTemplate Issue: SPR-16241 --- .../NamedParameterJdbcTemplate.java | 16 +- .../NamedParameterJdbcTemplateTests.java | 167 ++++++++---------- 2 files changed, 83 insertions(+), 100 deletions(-) diff --git a/spring-jdbc/src/main/java/org/springframework/jdbc/core/namedparam/NamedParameterJdbcTemplate.java b/spring-jdbc/src/main/java/org/springframework/jdbc/core/namedparam/NamedParameterJdbcTemplate.java index 439eb08574..ca8a402f15 100644 --- a/spring-jdbc/src/main/java/org/springframework/jdbc/core/namedparam/NamedParameterJdbcTemplate.java +++ b/spring-jdbc/src/main/java/org/springframework/jdbc/core/namedparam/NamedParameterJdbcTemplate.java @@ -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. + *

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. diff --git a/spring-jdbc/src/test/java/org/springframework/jdbc/core/namedparam/NamedParameterJdbcTemplateTests.java b/spring-jdbc/src/test/java/org/springframework/jdbc/core/namedparam/NamedParameterJdbcTemplateTests.java index a64f85e7ac..33af4c29f8 100644 --- a/spring-jdbc/src/test/java/org/springframework/jdbc/core/namedparam/NamedParameterJdbcTemplateTests.java +++ b/spring-jdbc/src/test/java/org/springframework/jdbc/core/namedparam/NamedParameterJdbcTemplateTests.java @@ -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 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() { - @Override - public Object doInPreparedStatement(PreparedStatement ps) - throws SQLException { - assertEquals(preparedStatement, ps); - ps.executeUpdate(); - return "result"; - } + (PreparedStatementCallback) 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() { - @Override - public Object doInPreparedStatement(PreparedStatement ps) - throws SQLException { - assertEquals(preparedStatement, ps); - ps.executeUpdate(); - return "result"; - } + (PreparedStatementCallback) 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() { - @Override - public Object doInPreparedStatement(PreparedStatement ps) - throws SQLException { - assertEquals(preparedStatement, ps); - ps.executeQuery(); - return "result"; - } + (PreparedStatementCallback) 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() { - @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() { - @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 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 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 customers = namedParameterTemplate.query(SELECT_NAMED_PARAMETERS, params, - new RowMapper() { - @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 customers = namedParameterTemplate.query(SELECT_NO_PARAMETERS, - new RowMapper() { - @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() { - @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[] 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]);