Browse Source

Stop calling deprecated JdbcOperations methods

Signed-off-by: Yanming Zhou <zhouyanming@gmail.com>
pull/34673/head
Yanming Zhou 9 months ago committed by Juergen Hoeller
parent
commit
5be83e9223
  1. 22
      spring-jdbc/src/main/java/org/springframework/jdbc/core/JdbcTemplate.java
  2. 6
      spring-jdbc/src/main/java/org/springframework/jdbc/core/PreparedStatementCallback.java
  3. 10
      spring-jdbc/src/main/kotlin/org/springframework/jdbc/core/JdbcOperationsExtensions.kt
  4. 12
      spring-jdbc/src/test/kotlin/org/springframework/jdbc/core/JdbcOperationsExtensionsTests.kt

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

@ -798,12 +798,12 @@ public class JdbcTemplate extends JdbcAccessor implements JdbcOperations {
@Deprecated @Deprecated
@Override @Override
public <T> List<T> query(String sql, @Nullable Object @Nullable [] args, RowMapper<T> rowMapper) throws DataAccessException { public <T> List<T> query(String sql, @Nullable Object @Nullable [] args, RowMapper<T> rowMapper) throws DataAccessException {
return result(query(sql, args, new RowMapperResultSetExtractor<>(rowMapper))); return result(query(sql, newArgPreparedStatementSetter(args), new RowMapperResultSetExtractor<>(rowMapper)));
} }
@Override @Override
public <T> List<T> query(String sql, RowMapper<T> rowMapper, @Nullable Object @Nullable ... args) throws DataAccessException { public <T> List<T> query(String sql, RowMapper<T> rowMapper, @Nullable Object @Nullable ... args) throws DataAccessException {
return result(query(sql, args, new RowMapperResultSetExtractor<>(rowMapper))); return result(query(sql, newArgPreparedStatementSetter(args), new RowMapperResultSetExtractor<>(rowMapper)));
} }
/** /**
@ -865,13 +865,13 @@ public class JdbcTemplate extends JdbcAccessor implements JdbcOperations {
@Deprecated @Deprecated
@Override @Override
public <T> @Nullable T queryForObject(String sql,@Nullable Object @Nullable [] args, RowMapper<T> rowMapper) throws DataAccessException { public <T> @Nullable T queryForObject(String sql,@Nullable Object @Nullable [] args, RowMapper<T> rowMapper) throws DataAccessException {
List<T> results = query(sql, args, new RowMapperResultSetExtractor<>(rowMapper, 1)); List<T> results = query(sql, newArgPreparedStatementSetter(args), new RowMapperResultSetExtractor<>(rowMapper, 1));
return DataAccessUtils.nullableSingleResult(results); return DataAccessUtils.nullableSingleResult(results);
} }
@Override @Override
public <T> @Nullable T queryForObject(String sql, RowMapper<T> rowMapper, @Nullable Object @Nullable ... args) throws DataAccessException { public <T> @Nullable T queryForObject(String sql, RowMapper<T> rowMapper, @Nullable Object @Nullable ... args) throws DataAccessException {
List<T> results = query(sql, args, new RowMapperResultSetExtractor<>(rowMapper, 1)); List<T> results = query(sql, newArgPreparedStatementSetter(args), new RowMapperResultSetExtractor<>(rowMapper, 1));
return DataAccessUtils.nullableSingleResult(results); return DataAccessUtils.nullableSingleResult(results);
} }
@ -885,12 +885,12 @@ public class JdbcTemplate extends JdbcAccessor implements JdbcOperations {
@Deprecated @Deprecated
@Override @Override
public <T> @Nullable T queryForObject(String sql, @Nullable Object @Nullable [] args, Class<T> requiredType) throws DataAccessException { public <T> @Nullable T queryForObject(String sql, @Nullable Object @Nullable [] args, Class<T> requiredType) throws DataAccessException {
return queryForObject(sql, args, getSingleColumnRowMapper(requiredType)); return queryForObject(sql, getSingleColumnRowMapper(requiredType), args);
} }
@Override @Override
public <T> @Nullable T queryForObject(String sql, Class<T> requiredType, @Nullable Object @Nullable ... args) throws DataAccessException { public <T> @Nullable T queryForObject(String sql, Class<T> requiredType, @Nullable Object @Nullable ... args) throws DataAccessException {
return queryForObject(sql, args, getSingleColumnRowMapper(requiredType)); return queryForObject(sql, getSingleColumnRowMapper(requiredType), args);
} }
@Override @Override
@ -900,7 +900,7 @@ public class JdbcTemplate extends JdbcAccessor implements JdbcOperations {
@Override @Override
public Map<String, Object> queryForMap(String sql, @Nullable Object @Nullable ... args) throws DataAccessException { public Map<String, Object> queryForMap(String sql, @Nullable Object @Nullable ... args) throws DataAccessException {
return result(queryForObject(sql, args, getColumnMapRowMapper())); return result(queryForObject(sql, getColumnMapRowMapper(), args));
} }
@Override @Override
@ -911,12 +911,12 @@ public class JdbcTemplate extends JdbcAccessor implements JdbcOperations {
@Deprecated @Deprecated
@Override @Override
public <T> List<T> queryForList(String sql, @Nullable Object @Nullable [] args, Class<T> elementType) throws DataAccessException { public <T> List<T> queryForList(String sql, @Nullable Object @Nullable [] args, Class<T> elementType) throws DataAccessException {
return query(sql, args, getSingleColumnRowMapper(elementType)); return query(sql, newArgPreparedStatementSetter(args), getSingleColumnRowMapper(elementType));
} }
@Override @Override
public <T> List<T> queryForList(String sql, Class<T> elementType, @Nullable Object @Nullable ... args) throws DataAccessException { public <T> List<T> queryForList(String sql, Class<T> elementType, @Nullable Object @Nullable ... args) throws DataAccessException {
return query(sql, args, getSingleColumnRowMapper(elementType)); return query(sql, newArgPreparedStatementSetter(args), getSingleColumnRowMapper(elementType));
} }
@Override @Override
@ -926,7 +926,7 @@ public class JdbcTemplate extends JdbcAccessor implements JdbcOperations {
@Override @Override
public List<Map<String, Object>> queryForList(String sql, @Nullable Object @Nullable ... args) throws DataAccessException { public List<Map<String, Object>> queryForList(String sql, @Nullable Object @Nullable ... args) throws DataAccessException {
return query(sql, args, getColumnMapRowMapper()); return query(sql, newArgPreparedStatementSetter(args), getColumnMapRowMapper());
} }
@Override @Override
@ -936,7 +936,7 @@ public class JdbcTemplate extends JdbcAccessor implements JdbcOperations {
@Override @Override
public SqlRowSet queryForRowSet(String sql, @Nullable Object @Nullable ... args) throws DataAccessException { public SqlRowSet queryForRowSet(String sql, @Nullable Object @Nullable ... args) throws DataAccessException {
return result(query(sql, args, new SqlRowSetResultSetExtractor())); return result(query(sql, newArgPreparedStatementSetter(args), new SqlRowSetResultSetExtractor()));
} }
protected int update(final PreparedStatementCreator psc, final @Nullable PreparedStatementSetter pss) protected int update(final PreparedStatementCreator psc, final @Nullable PreparedStatementSetter pss)

6
spring-jdbc/src/main/java/org/springframework/jdbc/core/PreparedStatementCallback.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2018 the original author or authors. * Copyright 2002-2025 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -72,8 +72,8 @@ public interface PreparedStatementCallback<T> {
* @throws SQLException if thrown by a JDBC method, to be auto-converted * @throws SQLException if thrown by a JDBC method, to be auto-converted
* to a DataAccessException by an SQLExceptionTranslator * to a DataAccessException by an SQLExceptionTranslator
* @throws DataAccessException in case of custom exceptions * @throws DataAccessException in case of custom exceptions
* @see JdbcTemplate#queryForObject(String, Object[], Class) * @see JdbcTemplate#queryForObject(String, Class, Object...)
* @see JdbcTemplate#queryForList(String, Object[]) * @see JdbcTemplate#queryForList(String, Object...)
*/ */
@Nullable T doInPreparedStatement(PreparedStatement ps) throws SQLException, DataAccessException; @Nullable T doInPreparedStatement(PreparedStatement ps) throws SQLException, DataAccessException;

10
spring-jdbc/src/main/kotlin/org/springframework/jdbc/core/JdbcOperationsExtensions.kt

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2024 the original author or authors. * Copyright 2002-2025 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -54,10 +54,8 @@ inline fun <reified T> JdbcOperations.queryForObject(sql: String, args: Array<ou
* @author Mario Arias * @author Mario Arias
* @since 5.0 * @since 5.0
*/ */
@Suppress("DEPRECATION")
// TODO Replace by the vararg variant in Spring Framework 6
inline fun <reified T> JdbcOperations.queryForObject(sql: String, args: Array<out Any>): T? = inline fun <reified T> JdbcOperations.queryForObject(sql: String, args: Array<out Any>): T? =
queryForObject(sql, args, T::class.java as Class<*>) as T queryForObject(sql, T::class.java as Class<*>, args) as T
/** /**
* Extension for [JdbcOperations.queryForList] providing a `queryForList<Foo>("...")` variant. * Extension for [JdbcOperations.queryForList] providing a `queryForList<Foo>("...")` variant.
@ -88,10 +86,8 @@ inline fun <reified T : Any> JdbcOperations.queryForList(sql: String, args: Arra
* @author Mario Arias * @author Mario Arias
* @since 5.0 * @since 5.0
*/ */
@Suppress("DEPRECATION")
// TODO Replace by the vararg variant in Spring Framework 6
inline fun <reified T : Any> JdbcOperations.queryForList(sql: String, args: Array<out Any>): List<T> = inline fun <reified T : Any> JdbcOperations.queryForList(sql: String, args: Array<out Any>): List<T> =
queryForList(sql, args, T::class.java) queryForList(sql, T::class.java, args)
/** /**

12
spring-jdbc/src/test/kotlin/org/springframework/jdbc/core/JdbcOperationsExtensionsTests.kt

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2021 the original author or authors * Copyright 2002-2025 the original author or authors
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -67,12 +67,11 @@ class JdbcOperationsExtensionsTests {
} }
@Test @Test
@Suppress("DEPRECATION")
fun `queryForObject with reified type parameters and args`() { fun `queryForObject with reified type parameters and args`() {
val args = arrayOf(3, 4) val args = arrayOf(3, 4)
every { template.queryForObject(sql, args, any<Class<Int>>()) } returns 2 every { template.queryForObject(sql, any<Class<Int>>(), args) } returns 2
assertThat(template.queryForObject<Int>(sql, args)).isEqualTo(2) assertThat(template.queryForObject<Int>(sql, args)).isEqualTo(2)
verify { template.queryForObject(sql, args, any<Class<Int>>()) } verify { template.queryForObject(sql, any<Class<Int>>(), args) }
} }
@Test @Test
@ -94,13 +93,12 @@ class JdbcOperationsExtensionsTests {
} }
@Test @Test
@Suppress("DEPRECATION")
fun `queryForList with reified type parameters and args`() { fun `queryForList with reified type parameters and args`() {
val list = listOf(1, 2, 3) val list = listOf(1, 2, 3)
val args = arrayOf(3, 4) val args = arrayOf(3, 4)
every { template.queryForList(sql, args, any<Class<Int>>()) } returns list every { template.queryForList(sql, any<Class<Int>>(), args) } returns list
template.queryForList<Int>(sql, args) template.queryForList<Int>(sql, args)
verify { template.queryForList(sql, args, any<Class<Int>>()) } verify { template.queryForList(sql, any<Class<Int>>(), args) }
} }
@Test @Test

Loading…
Cancel
Save