diff --git a/spring-jdbc/src/main/java/org/springframework/jdbc/core/JdbcTemplate.java b/spring-jdbc/src/main/java/org/springframework/jdbc/core/JdbcTemplate.java index a98804f2d8bd..c09cc68dec2e 100644 --- a/spring-jdbc/src/main/java/org/springframework/jdbc/core/JdbcTemplate.java +++ b/spring-jdbc/src/main/java/org/springframework/jdbc/core/JdbcTemplate.java @@ -798,12 +798,12 @@ public List query(String sql, @Nullable Object @Nullable [] args, int[] a @Deprecated @Override public List query(String sql, @Nullable Object @Nullable [] args, RowMapper rowMapper) throws DataAccessException { - return result(query(sql, args, new RowMapperResultSetExtractor<>(rowMapper))); + return result(query(sql, newArgPreparedStatementSetter(args), new RowMapperResultSetExtractor<>(rowMapper))); } @Override public List query(String sql, RowMapper 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 Stream queryForStream(String sql, RowMapper rowMapper, @Nullabl @Deprecated @Override public @Nullable T queryForObject(String sql,@Nullable Object @Nullable [] args, RowMapper rowMapper) throws DataAccessException { - List results = query(sql, args, new RowMapperResultSetExtractor<>(rowMapper, 1)); + List results = query(sql, newArgPreparedStatementSetter(args), new RowMapperResultSetExtractor<>(rowMapper, 1)); return DataAccessUtils.nullableSingleResult(results); } @Override public @Nullable T queryForObject(String sql, RowMapper rowMapper, @Nullable Object @Nullable ... args) throws DataAccessException { - List results = query(sql, args, new RowMapperResultSetExtractor<>(rowMapper, 1)); + List results = query(sql, newArgPreparedStatementSetter(args), new RowMapperResultSetExtractor<>(rowMapper, 1)); return DataAccessUtils.nullableSingleResult(results); } @@ -885,12 +885,12 @@ public Stream queryForStream(String sql, RowMapper rowMapper, @Nullabl @Deprecated @Override public @Nullable T queryForObject(String sql, @Nullable Object @Nullable [] args, Class requiredType) throws DataAccessException { - return queryForObject(sql, args, getSingleColumnRowMapper(requiredType)); + return queryForObject(sql, getSingleColumnRowMapper(requiredType), args); } @Override public @Nullable T queryForObject(String sql, Class requiredType, @Nullable Object @Nullable ... args) throws DataAccessException { - return queryForObject(sql, args, getSingleColumnRowMapper(requiredType)); + return queryForObject(sql, getSingleColumnRowMapper(requiredType), args); } @Override @@ -900,7 +900,7 @@ public Map queryForMap(String sql, @Nullable Object @Nullable [] @Override public Map queryForMap(String sql, @Nullable Object @Nullable ... args) throws DataAccessException { - return result(queryForObject(sql, args, getColumnMapRowMapper())); + return result(queryForObject(sql, getColumnMapRowMapper(), args)); } @Override @@ -911,12 +911,12 @@ public List queryForList(String sql, @Nullable Object @Nullable [] args, @Deprecated @Override public List queryForList(String sql, @Nullable Object @Nullable [] args, Class elementType) throws DataAccessException { - return query(sql, args, getSingleColumnRowMapper(elementType)); + return query(sql, newArgPreparedStatementSetter(args), getSingleColumnRowMapper(elementType)); } @Override public List queryForList(String sql, Class elementType, @Nullable Object @Nullable ... args) throws DataAccessException { - return query(sql, args, getSingleColumnRowMapper(elementType)); + return query(sql, newArgPreparedStatementSetter(args), getSingleColumnRowMapper(elementType)); } @Override @@ -926,7 +926,7 @@ public List> queryForList(String sql, @Nullable Object @Null @Override public List> queryForList(String sql, @Nullable Object @Nullable ... args) throws DataAccessException { - return query(sql, args, getColumnMapRowMapper()); + return query(sql, newArgPreparedStatementSetter(args), getColumnMapRowMapper()); } @Override @@ -936,7 +936,7 @@ public SqlRowSet queryForRowSet(String sql, @Nullable Object @Nullable [] args, @Override 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) diff --git a/spring-jdbc/src/main/java/org/springframework/jdbc/core/PreparedStatementCallback.java b/spring-jdbc/src/main/java/org/springframework/jdbc/core/PreparedStatementCallback.java index 235bf55234db..23d26f61aea9 100644 --- a/spring-jdbc/src/main/java/org/springframework/jdbc/core/PreparedStatementCallback.java +++ b/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"); * you may not use this file except in compliance with the License. @@ -72,8 +72,8 @@ public interface PreparedStatementCallback { * @throws SQLException if thrown by a JDBC method, to be auto-converted * to a DataAccessException by an SQLExceptionTranslator * @throws DataAccessException in case of custom exceptions - * @see JdbcTemplate#queryForObject(String, Object[], Class) - * @see JdbcTemplate#queryForList(String, Object[]) + * @see JdbcTemplate#queryForObject(String, Class, Object...) + * @see JdbcTemplate#queryForList(String, Object...) */ @Nullable T doInPreparedStatement(PreparedStatement ps) throws SQLException, DataAccessException; diff --git a/spring-jdbc/src/main/kotlin/org/springframework/jdbc/core/JdbcOperationsExtensions.kt b/spring-jdbc/src/main/kotlin/org/springframework/jdbc/core/JdbcOperationsExtensions.kt index f6480ab7511b..e29bb0f01b66 100644 --- a/spring-jdbc/src/main/kotlin/org/springframework/jdbc/core/JdbcOperationsExtensions.kt +++ b/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"); * you may not use this file except in compliance with the License. @@ -54,10 +54,8 @@ inline fun JdbcOperations.queryForObject(sql: String, args: Array JdbcOperations.queryForObject(sql: String, args: Array): 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("...")` variant. @@ -88,10 +86,8 @@ inline fun JdbcOperations.queryForList(sql: String, args: Arra * @author Mario Arias * @since 5.0 */ -@Suppress("DEPRECATION") -// TODO Replace by the vararg variant in Spring Framework 6 inline fun JdbcOperations.queryForList(sql: String, args: Array): List = - queryForList(sql, args, T::class.java) + queryForList(sql, T::class.java, args) /** diff --git a/spring-jdbc/src/test/kotlin/org/springframework/jdbc/core/JdbcOperationsExtensionsTests.kt b/spring-jdbc/src/test/kotlin/org/springframework/jdbc/core/JdbcOperationsExtensionsTests.kt index 6ed59b8a468d..894033e9bb28 100644 --- a/spring-jdbc/src/test/kotlin/org/springframework/jdbc/core/JdbcOperationsExtensionsTests.kt +++ b/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"); * you may not use this file except in compliance with the License. @@ -67,12 +67,11 @@ class JdbcOperationsExtensionsTests { } @Test - @Suppress("DEPRECATION") fun `queryForObject with reified type parameters and args`() { val args = arrayOf(3, 4) - every { template.queryForObject(sql, args, any>()) } returns 2 + every { template.queryForObject(sql, any>(), args) } returns 2 assertThat(template.queryForObject(sql, args)).isEqualTo(2) - verify { template.queryForObject(sql, args, any>()) } + verify { template.queryForObject(sql, any>(), args) } } @Test @@ -94,13 +93,12 @@ class JdbcOperationsExtensionsTests { } @Test - @Suppress("DEPRECATION") fun `queryForList with reified type parameters and args`() { val list = listOf(1, 2, 3) val args = arrayOf(3, 4) - every { template.queryForList(sql, args, any>()) } returns list + every { template.queryForList(sql, any>(), args) } returns list template.queryForList(sql, args) - verify { template.queryForList(sql, args, any>()) } + verify { template.queryForList(sql, any>(), args) } } @Test