Remove deprecated SimpleJdbcTemplate and supporting classes

Issue: SPR-11895
This commit is contained in:
Juergen Hoeller 2014-12-30 14:59:38 +01:00
parent f8a483f2d4
commit d6b16ffee3
8 changed files with 0 additions and 1475 deletions

View File

@ -1,71 +0,0 @@
/*
* Copyright 2002-2012 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.jdbc.core.simple;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
/**
* {@link ParameterizedRowMapper} implementation that converts a row into a new instance
* of the specified mapped target class. The mapped target class must be a top-level class
* and it must have a default or no-arg constructor.
*
* <p>Uses Java 5 covariant return types to override the return type of the {@link #mapRow}
* method to be the type parameter {@code T}.
*
* <p>Column values are mapped based on matching the column name as obtained from result set
* metadata to public setters for the corresponding properties. The names are matched either
* directly or by transforming a name separating the parts with underscores to the same name
* using "camel" case.
*
* <p>Mapping is provided for fields in the target class for many common types, e.g.:
* String, boolean, Boolean, byte, Byte, short, Short, int, Integer, long, Long,
* float, Float, double, Double, BigDecimal, {@code java.util.Date}, etc.
*
* <p>The mapper can be configured to use the primitives default value when mapping null values
* by setting the '{@link #setPrimitivesDefaultedForNullValue primitivesDefaultedForNullValue}'
* flag to 'true'.
*
* <p>To facilitate mapping between columns and fields that don't have matching names,
* try using column aliases in the SQL statement like "select fname as first_name from customer".
*
* <p>Please note that this class is designed to provide convenience rather than high performance.
* For best performance, consider using a custom {@link org.springframework.jdbc.core.RowMapper}
* implementation.
*
* @author Thomas Risberg
* @author Juergen Hoeller
* @since 2.5
* @see ParameterizedRowMapper
* @deprecated along with {@link SimpleJdbcTemplate}, in favor of the regular
* {@link org.springframework.jdbc.core.BeanPropertyRowMapper}
*/
@Deprecated
public class ParameterizedBeanPropertyRowMapper<T> extends BeanPropertyRowMapper<T>
implements ParameterizedRowMapper<T> {
/**
* Static factory method to create a new ParameterizedBeanPropertyRowMapper
* (with the mapped class specified only once).
* @param mappedClass the class that each row should be mapped to
*/
public static <T> ParameterizedBeanPropertyRowMapper<T> newInstance(Class<T> mappedClass) {
ParameterizedBeanPropertyRowMapper<T> newInstance = new ParameterizedBeanPropertyRowMapper<T>();
newInstance.setMappedClass(mappedClass);
return newInstance;
}
}

View File

@ -1,36 +0,0 @@
/*
* Copyright 2002-2008 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.jdbc.core.simple;
import org.springframework.jdbc.core.RowMapper;
/**
* Extension of the {@link org.springframework.jdbc.core.RowMapper} interface,
* adding type parameterization. As of Spring 3.0, this is equivalent to
* using the RowMapper interface directly.
*
* @author Rob Harrop
* @author Juergen Hoeller
* @since 2.0
* @see org.springframework.jdbc.core.simple.SimpleJdbcOperations
* @deprecated along with {@link SimpleJdbcTemplate}, in favor of the regular
* {@link RowMapper}
*/
@Deprecated
public interface ParameterizedRowMapper<T> extends RowMapper<T> {
}

View File

@ -1,53 +0,0 @@
/*
* Copyright 2002-2012 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.jdbc.core.simple;
import org.springframework.jdbc.core.SingleColumnRowMapper;
/**
* {@link ParameterizedRowMapper} implementation that converts a single column
* into a single result value per row. Expects to operate on a
* {@code java.sql.ResultSet} that just contains a single column.
*
* <p>The type of the result value for each row can be specified. The value
* for the single column will be extracted from the {@code ResultSet}
* and converted into the specified target type.
*
* <p>Uses Java 5 covariant return types to override the return type of the
* {@link #mapRow} method to be the type parameter {@code T}.
*
* @author Juergen Hoeller
* @since 2.5.2
* @deprecated along with {@link SimpleJdbcTemplate}, in favor of the regular
* {@link org.springframework.jdbc.core.SingleColumnRowMapper}
*/
@Deprecated
public class ParameterizedSingleColumnRowMapper<T> extends SingleColumnRowMapper<T>
implements ParameterizedRowMapper<T> {
/**
* Static factory method to create a new ParameterizedSingleColumnRowMapper
* (with the required type specified only once).
* @param requiredType the type that each result object is expected to match
*/
public static <T> ParameterizedSingleColumnRowMapper<T> newInstance(Class<T> requiredType) {
ParameterizedSingleColumnRowMapper<T> newInstance = new ParameterizedSingleColumnRowMapper<T>();
newInstance.setRequiredType(requiredType);
return newInstance;
}
}

View File

@ -1,55 +0,0 @@
/*
* Copyright 2002-2012 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.jdbc.core.simple;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
/**
* Extension of {@link org.springframework.jdbc.core.support.JdbcDaoSupport}
* that exposes a {@link #getSimpleJdbcTemplate() SimpleJdbcTemplate} as well.
* Only usable on Java 5 and above.
*
* @author Rod Johnson
* @author Juergen Hoeller
* @since 2.0
* @see SimpleJdbcTemplate
* @deprecated since Spring 3.1 in favor of {@link org.springframework.jdbc.core.support.JdbcDaoSupport} and
* {@link org.springframework.jdbc.core.namedparam.NamedParameterJdbcDaoSupport}. The JdbcTemplate and
* NamedParameterJdbcTemplate now provide all the functionality of the SimpleJdbcTemplate.
*/
@Deprecated
public class SimpleJdbcDaoSupport extends JdbcDaoSupport {
private SimpleJdbcTemplate simpleJdbcTemplate;
/**
* Create a SimpleJdbcTemplate based on the configured JdbcTemplate.
*/
@Override
protected void initTemplateConfig() {
this.simpleJdbcTemplate = new SimpleJdbcTemplate(getJdbcTemplate());
}
/**
* Return a SimpleJdbcTemplate wrapping the configured JdbcTemplate.
*/
public SimpleJdbcTemplate getSimpleJdbcTemplate() {
return this.simpleJdbcTemplate;
}
}

View File

@ -1,491 +0,0 @@
/*
* Copyright 2002-2012 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.jdbc.core.simple;
import java.util.List;
import java.util.Map;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcOperations;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
/**
* JDBC operations interface usable on Java 5 and above, exposing a
* set of common JDBC operations, whose interface is simplified
* through the use of varargs and autoboxing.
*
* @author Rod Johnson
* @author Rob Harrop
* @author Thomas Risberg
* @since 2.0
* @see org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate
* @see SimpleJdbcTemplate
* @see org.springframework.jdbc.core.JdbcOperations
* @deprecated since Spring 3.1 in favor of {@link org.springframework.jdbc.core.JdbcOperations} and
* {@link org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations}. The JdbcTemplate and
* NamedParameterJdbcTemplate now provide all the functionality of the SimpleJdbcTemplate.
*/
@Deprecated
public interface SimpleJdbcOperations {
/**
* Expose the classic Spring JdbcTemplate to allow invocation of less
* commonly used methods.
*/
JdbcOperations getJdbcOperations();
/**
* Expose the Spring NamedParameterJdbcTemplate to allow invocation of less
* commonly used methods.
*/
NamedParameterJdbcOperations getNamedParameterJdbcOperations();
/**
* Query for an {@code int} passing in a SQL query
* using the named parameter support provided by the
* {@link org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate}
* and a map containing the arguments.
* @param sql the SQL query to run.
* @param args the map containing the arguments for the query
*/
int queryForInt(String sql, Map<String, ?> args) throws DataAccessException;
/**
* Query for an {@code int} passing in a SQL query
* using the named parameter support provided by the
* {@link org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate}
* and a {@code SqlParameterSource} containing the arguments.
* @param sql the SQL query to run.
* @param args the {@code SqlParameterSource} containing the arguments for the query.
*/
int queryForInt(String sql, SqlParameterSource args) throws DataAccessException;
/**
* Query for an {@code int} passing in a SQL query
* using the standard '?' placeholders for parameters
* and a variable number of arguments.
* @param sql the SQL query to run.
* @param args the variable number of arguments for the query
*/
int queryForInt(String sql, Object... args) throws DataAccessException;
/**
* Query for an {@code long} passing in a SQL query
* using the named parameter support provided by the
* {@link org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate}
* and a map containing the arguments.
* @param sql the SQL query to run.
* @param args the map containing the arguments for the query
*/
long queryForLong(String sql, Map<String, ?> args) throws DataAccessException;
/**
* Query for an {@code long} passing in a SQL query
* using the named parameter support provided by the
* {@link org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate}
* and a {@code SqlParameterSource} containing the arguments.
* @param sql the SQL query to run.
* @param args the {@code SqlParameterSource} containing the arguments for the query
*/
long queryForLong(String sql, SqlParameterSource args) throws DataAccessException;
/**
* Query for an {@code long} passing in a SQL query
* using the standard '?' placeholders for parameters
* and a variable number of arguments.
* @param sql the SQL query to run.
* @param args the variable number of arguments for the query
*/
long queryForLong(String sql, Object... args) throws DataAccessException;
/**
* Query for an object of type {@code T} identified by the supplied @{@link Class}.
* Uses sql with the named parameter support provided by the
* {@link org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate}
* @param sql the SQL query to run
* @param requiredType the required type of the return value
* @param args the map containing the arguments for the query
* @see JdbcOperations#queryForObject(String, Class)
* @see JdbcOperations#queryForObject(String, Object[], Class)
*/
<T> T queryForObject(String sql, Class<T> requiredType, Map<String, ?> args)
throws DataAccessException;
/**
* Query for an object of type {@code T} identified by the supplied @{@link Class}.
* Uses sql with the named parameter support provided by the
* {@link org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate}
* @param sql the SQL query to run
* @param requiredType the required type of the return value
* @param args the {@code SqlParameterSource} containing the arguments for the query
* @see JdbcOperations#queryForObject(String, Class)
* @see JdbcOperations#queryForObject(String, Object[], Class)
*/
<T> T queryForObject(String sql, Class<T> requiredType, SqlParameterSource args)
throws DataAccessException;
/**
* Query for an object of type {@code T} identified by the supplied @{@link Class}.
* Uses sql with the standard '?' placeholders for parameters
* @param sql the SQL query to run
* @param requiredType the required type of the return value
* @param args the variable number of arguments for the query
* @see JdbcOperations#queryForObject(String, Class)
* @see JdbcOperations#queryForObject(String, Object[], Class)
*/
<T> T queryForObject(String sql, Class<T> requiredType, Object... args)
throws DataAccessException;
/**
* Query for an object of type {@code T} using the supplied
* {@link RowMapper} to the query results to the object.
* Uses sql with the named parameter support provided by the
* {@link org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate}
* @param sql the SQL query to run
* @param rm the @{@link RowMapper} to use for result mapping
* @param args the map containing the arguments for the query
* @see JdbcOperations#queryForObject(String, org.springframework.jdbc.core.RowMapper)
* @see JdbcOperations#queryForObject(String, Object[], org.springframework.jdbc.core.RowMapper)
*/
<T> T queryForObject(String sql, RowMapper<T> rm, Map<String, ?> args)
throws DataAccessException;
/**
* Query for an object of type {@code T} using the supplied
* {@link ParameterizedRowMapper} to the query results to the object.
* Uses sql with the named parameter support provided by the
* {@link org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate}
* @param sql the SQL query to run
* @param rm the @{@link ParameterizedRowMapper} to use for result mapping
* @param args the map containing the arguments for the query
* @see JdbcOperations#queryForObject(String, org.springframework.jdbc.core.RowMapper)
* @see JdbcOperations#queryForObject(String, Object[], org.springframework.jdbc.core.RowMapper)
* @deprecated as of Spring 3.0: Use the method using the newly genericized RowMapper interface
* instead since the RowMapper and ParameterizedRowMapper interfaces are equivalent now.
*/
@Deprecated
<T> T queryForObject(String sql, ParameterizedRowMapper<T> rm, Map<String, ?> args)
throws DataAccessException;
/**
* Query for an object of type {@code T} using the supplied
* {@link RowMapper} to the query results to the object.
* Uses sql with the named parameter support provided by the
* {@link org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate}
* @param sql the SQL query to run
* @param rm the @{@link RowMapper} to use for result mapping
* @param args the {@code SqlParameterSource} containing the arguments for the query
* @see JdbcOperations#queryForObject(String, org.springframework.jdbc.core.RowMapper)
* @see JdbcOperations#queryForObject(String, Object[], org.springframework.jdbc.core.RowMapper)
*/
<T> T queryForObject(String sql, RowMapper<T> rm, SqlParameterSource args)
throws DataAccessException;
/**
* Query for an object of type {@code T} using the supplied
* {@link ParameterizedRowMapper} to the query results to the object.
* Uses sql with the named parameter support provided by the
* {@link org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate}
* @param sql the SQL query to run
* @param rm the @{@link ParameterizedRowMapper} to use for result mapping
* @param args the {@code SqlParameterSource} containing the arguments for the query
* @see JdbcOperations#queryForObject(String, org.springframework.jdbc.core.RowMapper)
* @see JdbcOperations#queryForObject(String, Object[], org.springframework.jdbc.core.RowMapper)
* @deprecated as of Spring 3.0: Use the method using the newly genericized RowMapper interface
* instead since the RowMapper and ParameterizedRowMapper interfaces are equivalent now.
*/
@Deprecated
<T> T queryForObject(String sql, ParameterizedRowMapper<T> rm, SqlParameterSource args)
throws DataAccessException;
/**
* Query for an object of type {@code T} using the supplied
* {@link RowMapper} to the query results to the object.
* Uses sql with the standard '?' placeholders for parameters
* @param sql the SQL query to run
* @param rm the @{@link RowMapper} to use for result mapping
* @param args the variable number of arguments for the query
* @see JdbcOperations#queryForObject(String, org.springframework.jdbc.core.RowMapper)
* @see JdbcOperations#queryForObject(String, Object[], org.springframework.jdbc.core.RowMapper)
*/
<T> T queryForObject(String sql, RowMapper<T> rm, Object... args)
throws DataAccessException;
/**
* Query for an object of type {@code T} using the supplied
* {@link ParameterizedRowMapper} to the query results to the object.
* Uses sql with the standard '?' placeholders for parameters
* @param sql the SQL query to run
* @param rm the @{@link ParameterizedRowMapper} to use for result mapping
* @param args the variable number of arguments for the query
* @see JdbcOperations#queryForObject(String, org.springframework.jdbc.core.RowMapper)
* @see JdbcOperations#queryForObject(String, Object[], org.springframework.jdbc.core.RowMapper)
* @deprecated as of Spring 3.0: Use the method using the newly genericized RowMapper interface
* instead since the RowMapper and ParameterizedRowMapper interfaces are equivalent now.
*/
@Deprecated
<T> T queryForObject(String sql, ParameterizedRowMapper<T> rm, Object... args)
throws DataAccessException;
/**
* Query for a {@link List} of {@code Objects} of type {@code T} using
* the supplied {@link RowMapper} to the query results to the object.
* Uses sql with the named parameter support provided by the
* {@link org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate}
* @param sql the SQL query to run
* @param rm the @{@link RowMapper} to use for result mapping
* @param args the map containing the arguments for the query
* @see JdbcOperations#queryForObject(String, org.springframework.jdbc.core.RowMapper)
* @see JdbcOperations#queryForObject(String, Object[], org.springframework.jdbc.core.RowMapper)
*/
<T> List<T> query(String sql, RowMapper<T> rm, Map<String, ?> args)
throws DataAccessException;
/**
* Query for a {@link List} of {@code Objects} of type {@code T} using
* the supplied {@link ParameterizedRowMapper} to the query results to the object.
* Uses sql with the named parameter support provided by the
* {@link org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate}
* @param sql the SQL query to run
* @param rm the @{@link ParameterizedRowMapper} to use for result mapping
* @param args the map containing the arguments for the query
* @see JdbcOperations#queryForObject(String, org.springframework.jdbc.core.RowMapper)
* @see JdbcOperations#queryForObject(String, Object[], org.springframework.jdbc.core.RowMapper)
* @deprecated as of Spring 3.0: Use the method using the newly genericized RowMapper interface
* instead since the RowMapper and ParameterizedRowMapper interfaces are equivalent now.
*/
@Deprecated
<T> List<T> query(String sql, ParameterizedRowMapper<T> rm, Map<String, ?> args)
throws DataAccessException;
/**
* Query for a {@link List} of {@code Objects} of type {@code T} using
* the supplied {@link RowMapper} to the query results to the object.
* Uses sql with the named parameter support provided by the
* {@link org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate}
* @param sql the SQL query to run
* @param rm the @{@link RowMapper} to use for result mapping
* @param args the {@code SqlParameterSource} containing the arguments for the query
* @see JdbcOperations#queryForObject(String, org.springframework.jdbc.core.RowMapper)
* @see JdbcOperations#queryForObject(String, Object[], org.springframework.jdbc.core.RowMapper)
*/
<T> List<T> query(String sql, RowMapper<T> rm, SqlParameterSource args)
throws DataAccessException;
/**
* Query for a {@link List} of {@code Objects} of type {@code T} using
* the supplied {@link ParameterizedRowMapper} to the query results to the object.
* Uses sql with the named parameter support provided by the
* {@link org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate}
* @param sql the SQL query to run
* @param rm the @{@link ParameterizedRowMapper} to use for result mapping
* @param args the {@code SqlParameterSource} containing the arguments for the query
* @see JdbcOperations#queryForObject(String, org.springframework.jdbc.core.RowMapper)
* @see JdbcOperations#queryForObject(String, Object[], org.springframework.jdbc.core.RowMapper)
* @deprecated as of Spring 3.0: Use the method using the newly genericized RowMapper interface
* instead since the RowMapper and ParameterizedRowMapper interfaces are equivalent now.
*/
@Deprecated
<T> List<T> query(String sql, ParameterizedRowMapper<T> rm, SqlParameterSource args)
throws DataAccessException;
/**
* Query for a {@link List} of {@code Objects} of type {@code T} using
* the supplied {@link RowMapper} to the query results to the object.
* Uses sql with the standard '?' placeholders for parameters
* @param sql the SQL query to run
* @param rm the @{@link RowMapper} to use for result mapping
* @param args the variable number of arguments for the query
* @see JdbcOperations#queryForObject(String, org.springframework.jdbc.core.RowMapper)
* @see JdbcOperations#queryForObject(String, Object[], org.springframework.jdbc.core.RowMapper)
*/
<T> List<T> query(String sql, RowMapper<T> rm, Object... args)
throws DataAccessException;
/**
* Query for a {@link List} of {@code Objects} of type {@code T} using
* the supplied {@link ParameterizedRowMapper} to the query results to the object.
* Uses sql with the standard '?' placeholders for parameters
* @param sql the SQL query to run
* @param rm the @{@link ParameterizedRowMapper} to use for result mapping
* @param args the variable number of arguments for the query
* @see JdbcOperations#queryForObject(String, org.springframework.jdbc.core.RowMapper)
* @see JdbcOperations#queryForObject(String, Object[], org.springframework.jdbc.core.RowMapper)
* @deprecated as of Spring 3.0: Use the method using the newly genericized RowMapper interface
* instead since the RowMapper and ParameterizedRowMapper interfaces are equivalent now.
*/
@Deprecated
<T> List<T> query(String sql, ParameterizedRowMapper<T> rm, Object... args)
throws DataAccessException;
/**
* Execute the supplied query with the supplied arguments.
* <p>The query is expected to be a single row query; the result row will be
* mapped to a Map<String, Object> (one entry for each column, using the column name as the key).
* Uses sql with the named parameter support provided by the
* {@link org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate}
* @param sql the SQL query to run
* @param args the map containing the arguments for the query
* @see JdbcOperations#queryForMap(String)
* @see JdbcOperations#queryForMap(String, Object[])
*/
Map<String, Object> queryForMap(String sql, Map<String, ?> args)
throws DataAccessException;
/**
* Execute the supplied query with the supplied arguments.
* <p>The query is expected to be a single row query; the result row will be
* mapped to a Map<String, Object> (one entry for each column, using the column name as the key).
* Uses sql with the named parameter support provided by the
* {@link org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate}
* @param sql the SQL query to run
* @param args the {@code SqlParameterSource} containing the arguments for the query
* @see JdbcOperations#queryForMap(String)
* @see JdbcOperations#queryForMap(String, Object[])
*/
Map<String, Object> queryForMap(String sql, SqlParameterSource args)
throws DataAccessException;
/**
* Execute the supplied query with the (optional) supplied arguments.
* <p>The query is expected to be a single row query; the result row will be
* mapped to a Map<String, Object> (one entry for each column, using the column name as the key).
* Uses sql with the standard '?' placeholders for parameters
* @param sql the SQL query to run
* @param args the variable number of arguments for the query
* @see JdbcOperations#queryForMap(String)
* @see JdbcOperations#queryForMap(String, Object[])
*/
Map<String, Object> queryForMap(String sql, Object... args)
throws DataAccessException;
/**
* Execute the supplied query with the supplied arguments.
* <p>Each element in the returned {@link List} is constructed as a {@link Map}
* as described in {@link #queryForMap}
* Uses sql with the named parameter support provided by the
* {@link org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate}
* @param sql the SQL query to run
* @param args the map containing the arguments for the query
* @see JdbcOperations#queryForList(String)
* @see JdbcOperations#queryForList(String, Object[])
*/
List<Map<String, Object>> queryForList(String sql, Map<String, ?> args)
throws DataAccessException;
/**
* Execute the supplied query with the supplied arguments.
* <p>Each element in the returned {@link List} is constructed as a {@link Map}
* as described in {@link #queryForMap}
* Uses sql with the named parameter support provided by the
* {@link org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate}
* @param sql the SQL query to run
* @param args the {@code SqlParameterSource} containing the arguments for the query
* @see JdbcOperations#queryForList(String)
* @see JdbcOperations#queryForList(String, Object[])
*/
List<Map<String, Object>> queryForList(String sql, SqlParameterSource args)
throws DataAccessException;
/**
* Execute the supplied query with the (optional) supplied arguments.
* <p>Each element in the returned {@link List} is constructed as a {@link Map}
* as described in {@link #queryForMap}
* Uses sql with the standard '?' placeholders for parameters
* @param sql the SQL query to run
* @param args the variable number of arguments for the query
* @see JdbcOperations#queryForList(String)
* @see JdbcOperations#queryForList(String, Object[])
*/
List<Map<String, Object>> queryForList(String sql, Object... args)
throws DataAccessException;
/**
* Execute the supplied SQL statement with (optional) supplied arguments.
* Uses sql with the named parameter support provided by the
* {@link org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate}
* @param sql the SQL statement to execute
* @param args the map containing the arguments for the query
* @return the numbers of rows affected by the update
* @see NamedParameterJdbcOperations#update(String, Map)
*/
int update(String sql, Map<String, ?> args) throws DataAccessException;
/**
* Execute the supplied SQL statement with supplied arguments.
* Uses sql with the named parameter support provided by the
* {@link org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate}
* @param sql the SQL statement to execute
* @param args the {@code SqlParameterSource} containing the arguments for the statement
* @return the numbers of rows affected by the update
* @see NamedParameterJdbcOperations#update(String, SqlParameterSource)
*/
int update(String sql, SqlParameterSource args) throws DataAccessException;
/**
* Execute the supplied SQL statement with supplied arguments.
* Uses sql with the standard '?' placeholders for parameters
* @param sql the SQL statement to execute
* @param args the variable number of arguments for the query
* @return the numbers of rows affected by the update
* @see JdbcOperations#update(String)
* @see JdbcOperations#update(String, Object[])
*/
int update(String sql, Object... args) throws DataAccessException;
/**
* Executes a batch using the supplied SQL statement with the batch of supplied arguments.
* Uses sql with the named parameter support.
* @param sql the SQL statement to execute
* @param batchValues the array of Maps containing the batch of arguments for the query
* @return an array containing the numbers of rows affected by each update in the batch
*/
public int[] batchUpdate(String sql, Map<String, ?>[] batchValues);
/**
* Execute a batch using the supplied SQL statement with the batch of supplied arguments.
* Uses sql with the named parameter support.
* @param sql the SQL statement to execute
* @param batchArgs the array of {@link SqlParameterSource} containing the batch of arguments for the query
* @return an array containing the numbers of rows affected by each update in the batch
*/
public int[] batchUpdate(String sql, SqlParameterSource[] batchArgs);
/**
* Execute a batch using the supplied SQL statement with the batch of supplied arguments.
* Uses sql with the standard '?' placeholders for parameters
* @param sql the SQL statement to execute
* @param batchArgs the List of Object arrays containing the batch of arguments for the query
* @return an array containing the numbers of rows affected by each update in the batch
*/
public int[] batchUpdate(String sql, List<Object[]> batchArgs);
/**
* Execute a batch using the supplied SQL statement with the batch of supplied arguments.
* Uses sql with the standard '?' placeholders for parameters
* @param sql the SQL statement to execute.
* @param batchArgs the List of Object arrays containing the batch of arguments for the query
* @param argTypes SQL types of the arguments
* (constants from {@code java.sql.Types})
* @return an array containing the numbers of rows affected by each update in the batch
*/
public int[] batchUpdate(String sql, List<Object[]> batchArgs, int[] argTypes);
}

View File

@ -1,323 +0,0 @@
/*
* Copyright 2002-2014 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.jdbc.core.simple;
import java.util.List;
import java.util.Map;
import javax.sql.DataSource;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BatchUpdateUtils;
import org.springframework.jdbc.core.JdbcOperations;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.util.ObjectUtils;
/**
* Java-5-based convenience wrapper for the classic Spring
* {@link org.springframework.jdbc.core.JdbcTemplate},
* taking advantage of varargs and autoboxing, and exposing only the most
* commonly required operations in order to simplify JdbcTemplate usage.
*
* <p>Use the {@link #getJdbcOperations()} method (or a straight JdbcTemplate)
* if you need to invoke less commonly used template methods. This includes
* any methods specifying SQL types, methods using less commonly used callbacks
* such as RowCallbackHandler, updates with PreparedStatementSetters rather than
* argument arrays, and stored procedures as well as batch operations.
*
* <p><b>NOTE: An instance of this class is thread-safe once configured.</b>
*
* @author Rod Johnson
* @author Rob Harrop
* @author Juergen Hoeller
* @author Thomas Risberg
* @since 2.0
* @see ParameterizedRowMapper
* @see SimpleJdbcDaoSupport
* @see org.springframework.jdbc.core.JdbcTemplate
* @deprecated since Spring 3.1 in favor of {@link org.springframework.jdbc.core.JdbcTemplate} and
* {@link org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate}. The JdbcTemplate and
* NamedParameterJdbcTemplate now provide all the functionality of the SimpleJdbcTemplate.
*/
@Deprecated
public class SimpleJdbcTemplate implements SimpleJdbcOperations {
/** The NamedParameterJdbcTemplate that we are wrapping */
private final NamedParameterJdbcOperations namedParameterJdbcOperations;
/**
* Create a new SimpleJdbcTemplate for the given DataSource.
* <p>Creates a classic Spring JdbcTemplate and wraps it.
* @param dataSource the JDBC DataSource to access
*/
public SimpleJdbcTemplate(DataSource dataSource) {
this.namedParameterJdbcOperations = new NamedParameterJdbcTemplate(dataSource);
}
/**
* Create a new SimpleJdbcTemplate for the given classic Spring JdbcTemplate.
* @param classicJdbcTemplate the classic Spring JdbcTemplate to wrap
*/
public SimpleJdbcTemplate(JdbcOperations classicJdbcTemplate) {
this.namedParameterJdbcOperations = new NamedParameterJdbcTemplate(classicJdbcTemplate);
}
/**
* Create a new SimpleJdbcTemplate for the given Spring NamedParameterJdbcTemplate.
* @param namedParameterJdbcTemplate the Spring NamedParameterJdbcTemplate to wrap
*/
public SimpleJdbcTemplate(NamedParameterJdbcOperations namedParameterJdbcTemplate) {
this.namedParameterJdbcOperations = namedParameterJdbcTemplate;
}
/**
* Expose the classic Spring JdbcTemplate to allow invocation of
* less commonly used methods.
*/
@Override
public JdbcOperations getJdbcOperations() {
return this.namedParameterJdbcOperations.getJdbcOperations();
}
/**
* Expose the Spring NamedParameterJdbcTemplate to allow invocation of
* less commonly used methods.
*/
@Override
public NamedParameterJdbcOperations getNamedParameterJdbcOperations() {
return this.namedParameterJdbcOperations;
}
@Override
public int queryForInt(String sql, Map<String, ?> args) throws DataAccessException {
return getNamedParameterJdbcOperations().queryForInt(sql, args);
}
@Override
public int queryForInt(String sql, SqlParameterSource args) throws DataAccessException {
return getNamedParameterJdbcOperations().queryForInt(sql, args);
}
@Override
public int queryForInt(String sql, Object... args) throws DataAccessException {
return (ObjectUtils.isEmpty(args) ?
getJdbcOperations().queryForInt(sql) :
getJdbcOperations().queryForInt(sql, getArguments(args)));
}
@Override
public long queryForLong(String sql, Map<String, ?> args) throws DataAccessException {
return getNamedParameterJdbcOperations().queryForLong(sql, args);
}
@Override
public long queryForLong(String sql, SqlParameterSource args) throws DataAccessException {
return getNamedParameterJdbcOperations().queryForLong(sql, args);
}
@Override
public long queryForLong(String sql, Object... args) throws DataAccessException {
return (ObjectUtils.isEmpty(args) ?
getJdbcOperations().queryForLong(sql) :
getJdbcOperations().queryForLong(sql, getArguments(args)));
}
@Override
public <T> T queryForObject(String sql, Class<T> requiredType, Map<String, ?> args) throws DataAccessException {
return getNamedParameterJdbcOperations().queryForObject(sql, args, requiredType);
}
@Override
public <T> T queryForObject(String sql, Class<T> requiredType, SqlParameterSource args)
throws DataAccessException {
return getNamedParameterJdbcOperations().queryForObject(sql, args, requiredType);
}
@Override
public <T> T queryForObject(String sql, Class<T> requiredType, Object... args) throws DataAccessException {
return (ObjectUtils.isEmpty(args) ?
getJdbcOperations().queryForObject(sql, requiredType) :
getJdbcOperations().queryForObject(sql, getArguments(args), requiredType));
}
@Override
public <T> T queryForObject(String sql, RowMapper<T> rm, Map<String, ?> args) throws DataAccessException {
return getNamedParameterJdbcOperations().queryForObject(sql, args, rm);
}
@Override
@Deprecated
public <T> T queryForObject(String sql, ParameterizedRowMapper<T> rm, Map<String, ?> args) throws DataAccessException {
return queryForObject(sql, (RowMapper<T>) rm, args);
}
@Override
public <T> T queryForObject(String sql, RowMapper<T> rm, SqlParameterSource args)
throws DataAccessException {
return getNamedParameterJdbcOperations().queryForObject(sql, args, rm);
}
@Override
@Deprecated
public <T> T queryForObject(String sql, ParameterizedRowMapper<T> rm, SqlParameterSource args)
throws DataAccessException {
return queryForObject(sql, (RowMapper<T>) rm, args);
}
@Override
public <T> T queryForObject(String sql, RowMapper<T> rm, Object... args) throws DataAccessException {
return (ObjectUtils.isEmpty(args) ?
getJdbcOperations().queryForObject(sql, rm):
getJdbcOperations().queryForObject(sql, getArguments(args), rm));
}
@Override
@Deprecated
public <T> T queryForObject(String sql, ParameterizedRowMapper<T> rm, Object... args) throws DataAccessException {
return queryForObject(sql, (RowMapper<T>) rm, args);
}
@Override
public <T> List<T> query(String sql, RowMapper<T> rm, Map<String, ?> args) throws DataAccessException {
return getNamedParameterJdbcOperations().query(sql, args, rm);
}
@Override
@Deprecated
public <T> List<T> query(String sql, ParameterizedRowMapper<T> rm, Map<String, ?> args) throws DataAccessException {
return query(sql, (RowMapper<T>) rm, args);
}
@Override
public <T> List<T> query(String sql, RowMapper<T> rm, SqlParameterSource args)
throws DataAccessException {
return getNamedParameterJdbcOperations().query(sql, args, rm);
}
@Override
@Deprecated
public <T> List<T> query(String sql, ParameterizedRowMapper<T> rm, SqlParameterSource args)
throws DataAccessException {
return query(sql, (RowMapper<T>) rm, args);
}
@Override
public <T> List<T> query(String sql, RowMapper<T> rm, Object... args) throws DataAccessException {
return (ObjectUtils.isEmpty(args) ?
getJdbcOperations().query(sql, rm) :
getJdbcOperations().query(sql, getArguments(args), rm));
}
@Override
@Deprecated
public <T> List<T> query(String sql, ParameterizedRowMapper<T> rm, Object... args) throws DataAccessException {
return query(sql, (RowMapper<T>) rm, args);
}
@Override
public Map<String, Object> queryForMap(String sql, Map<String, ?> args) throws DataAccessException {
return getNamedParameterJdbcOperations().queryForMap(sql, args);
}
@Override
public Map<String, Object> queryForMap(String sql, SqlParameterSource args)
throws DataAccessException {
return getNamedParameterJdbcOperations().queryForMap(sql, args);
}
@Override
public Map<String, Object> queryForMap(String sql, Object... args) throws DataAccessException {
return (ObjectUtils.isEmpty(args) ?
getJdbcOperations().queryForMap(sql) :
getJdbcOperations().queryForMap(sql, getArguments(args)));
}
@Override
public List<Map<String, Object>> queryForList(String sql, Map<String, ?> args) throws DataAccessException {
return getNamedParameterJdbcOperations().queryForList(sql, args);
}
@Override
public List<Map<String, Object>> queryForList(String sql, SqlParameterSource args)
throws DataAccessException {
return getNamedParameterJdbcOperations().queryForList(sql, args);
}
@Override
public List<Map<String, Object>> queryForList(String sql, Object... args) throws DataAccessException {
return (ObjectUtils.isEmpty(args) ?
getJdbcOperations().queryForList(sql) :
getJdbcOperations().queryForList(sql, getArguments(args)));
}
@Override
public int update(String sql, Map<String, ?> args) throws DataAccessException {
return getNamedParameterJdbcOperations().update(sql, args);
}
@Override
public int update(String sql, SqlParameterSource args) throws DataAccessException {
return getNamedParameterJdbcOperations().update(sql, args);
}
@Override
public int update(String sql, Object ... args) throws DataAccessException {
return (ObjectUtils.isEmpty(args) ?
getJdbcOperations().update(sql) :
getJdbcOperations().update(sql, getArguments(args)));
}
@Override
public int[] batchUpdate(String sql, List<Object[]> batchArgs) {
return batchUpdate(sql, batchArgs, new int[0]);
}
@Override
public int[] batchUpdate(String sql, List<Object[]> batchArgs, int[] argTypes) {
return BatchUpdateUtils.executeBatchUpdate(sql, batchArgs, argTypes, getJdbcOperations());
}
@Override
public int[] batchUpdate(String sql, Map<String, ?>[] batchValues) {
return getNamedParameterJdbcOperations().batchUpdate(sql, batchValues);
}
@Override
public int[] batchUpdate(String sql, SqlParameterSource[] batchArgs) {
return getNamedParameterJdbcOperations().batchUpdate(sql, batchArgs);
}
/*
* Considers an Object array passed into a varargs parameter as
* collection of arguments rather than as single argument.
*/
private Object[] getArguments(Object[] varArgs) {
if (varArgs.length == 1 && varArgs[0] instanceof Object[]) {
return (Object[]) varArgs[0];
}
else {
return varArgs;
}
}
}

View File

@ -1,77 +0,0 @@
/*
* Copyright 2002-2014 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.jdbc.core.simple;
import java.util.List;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.springframework.dao.InvalidDataAccessApiUsageException;
import org.springframework.jdbc.core.AbstractRowMapperTests;
import org.springframework.jdbc.core.test.ConcretePerson;
import org.springframework.jdbc.core.test.Person;
import static org.junit.Assert.*;
/**
* @author Thomas Risberg
*/
@SuppressWarnings("deprecation")
public class ParameterizedBeanPropertyRowMapperTests extends AbstractRowMapperTests {
@Rule
public ExpectedException thrown = ExpectedException.none();
@Test
@SuppressWarnings({ "unchecked", "rawtypes" })
public void testOverridingDifferentClassDefinedForMapping() {
ParameterizedBeanPropertyRowMapper mapper = ParameterizedBeanPropertyRowMapper.newInstance(Person.class);
thrown.expect(InvalidDataAccessApiUsageException.class);
mapper.setMappedClass(Long.class);
}
@Test
public void testOverridingSameClassDefinedForMapping() {
ParameterizedBeanPropertyRowMapper<Person> mapper = ParameterizedBeanPropertyRowMapper.newInstance(Person.class);
mapper.setMappedClass(Person.class);
}
@Test
public void testStaticQueryWithRowMapper() throws Exception {
Mock mock = new Mock();
List<Person> result = mock.getJdbcTemplate().query(
"select name, age, birth_date, balance from people",
ParameterizedBeanPropertyRowMapper.newInstance(Person.class));
assertEquals(1, result.size());
verifyPerson(result.get(0));
mock.verifyClosed();
}
@Test
public void testMappingWithInheritance() throws Exception {
Mock mock = new Mock();
List<ConcretePerson> result = mock.getJdbcTemplate().query(
"select name, age, birth_date, balance from people",
ParameterizedBeanPropertyRowMapper.newInstance(ConcretePerson.class));
assertEquals(1, result.size());
verifyConcretePerson(result.get(0));
mock.verifyClosed();
}
}

View File

@ -1,369 +0,0 @@
/*
* Copyright 2002-2013 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.jdbc.core.simple;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import javax.sql.DataSource;
import org.junit.Before;
import org.junit.Test;
import org.springframework.jdbc.core.JdbcOperations;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import static org.junit.Assert.*;
import static org.mockito.BDDMockito.*;
/**
* @author Rod Johnson
* @author Rob Harrop
* @author Juergen Hoeller
* @author Thomas Risberg
*/
@Deprecated
public class SimpleJdbcTemplateTests {
private static final String SQL = "sql";
private static final Object[] ARGS_ARRAY = { 24.7, "foo", new Object() };
private static final Map<String, Object> ARGS_MAP;
private static final MapSqlParameterSource ARGS_SOURCE;
static {
ARGS_MAP = new HashMap<String, Object>(3);
ARGS_SOURCE = new MapSqlParameterSource();
for (int i = 0; i < ARGS_ARRAY.length; i++) {
ARGS_MAP.put(String.valueOf(i), ARGS_ARRAY[i]);
ARGS_SOURCE.addValue(String.valueOf(i), ARGS_ARRAY[i]);
}
}
private JdbcOperations operations;
private NamedParameterJdbcOperations namedParameterOperations;
private SimpleJdbcTemplate template;
private SimpleJdbcTemplate namedParameterTemplate;
@Before
public void setup() {
this.operations = mock(JdbcOperations.class);
this.namedParameterOperations = mock(NamedParameterJdbcOperations.class);
this.template = new SimpleJdbcTemplate(operations);
this.namedParameterTemplate = new SimpleJdbcTemplate(namedParameterOperations);
}
@Test
public void testQueryForIntWithoutArgs() {
given(operations.queryForInt(SQL)).willReturn(666);
int result = template.queryForInt(SQL);
assertEquals(666, result);
}
@Test
public void testQueryForIntWithArgs() {
given(operations.queryForInt(SQL, new Object[] { 24, "foo" })).willReturn(666);
int result = template.queryForInt(SQL, 24, "foo");
assertEquals(666, result);
}
@Test
public void testQueryForIntWithMap() {
Map<String, Object> args = new HashMap<String, Object>(2);
args.put("id", 24);
args.put("xy", "foo");
given(namedParameterOperations.queryForInt(SQL, args)).willReturn(666);
int result = namedParameterTemplate.queryForInt(SQL, args);
assertEquals(666, result);
}
@Test
public void testQueryForIntWitSqlParameterSource() {
SqlParameterSource args = new MapSqlParameterSource().addValue("id", 24).addValue("xy", "foo");
given(namedParameterOperations.queryForInt(SQL, args)).willReturn(666);
int result = namedParameterTemplate.queryForInt(SQL, args);
assertEquals(666, result);
}
@Test
public void testQueryForLongWithoutArgs() {
given(operations.queryForLong(SQL)).willReturn((long) 666);
long result = template.queryForLong(SQL);
assertEquals(666, result);
}
@Test
public void testQueryForLongWithArgs() {
long expectedResult = 666;
given(operations.queryForLong(SQL, ARGS_ARRAY)).willReturn(expectedResult);
long result = template.queryForLong(SQL, ARGS_ARRAY);
assertEquals(expectedResult, result);
}
@Test
public void testQueryForObjectWithoutArgs() throws Exception {
Date expectedResult = new Date();
given(operations.queryForObject(SQL, Date.class)).willReturn(expectedResult);
Date result = template.queryForObject(SQL, Date.class);
assertSame(expectedResult, result);
}
@Test
public void testQueryForObjectWithArgs() throws Exception {
Date expectedResult = new Date();
given(operations.queryForObject(SQL, ARGS_ARRAY, Date.class)
).willReturn(expectedResult);
Date result = template.queryForObject(SQL, Date.class,ARGS_ARRAY);
assertSame(expectedResult, result);
}
@Test
public void testQueryForObjectWithArgArray() throws Exception {
Date expectedResult = new Date();
given(operations.queryForObject(SQL, ARGS_ARRAY, Date.class)
).willReturn(expectedResult);
Date result = template.queryForObject(SQL, Date.class, ARGS_ARRAY);
assertSame(expectedResult, result);
}
@Test
public void testQueryForObjectWithMap() throws Exception {
Date expectedResult = new Date();
given(operations.queryForObject(SQL, ARGS_ARRAY, Date.class)
).willReturn(expectedResult);
Date result = template.queryForObject(SQL, Date.class, ARGS_ARRAY);
assertSame(expectedResult, result);
}
@Test
public void testQueryForObjectWithRowMapperAndWithoutArgs() throws Exception {
Date expectedResult = new Date();
ParameterizedRowMapper<Date> rm = new ParameterizedRowMapper<Date>() {
@Override
public Date mapRow(ResultSet rs, int rowNum) {
return new Date();
}
};
given(operations.queryForObject(SQL, rm)).willReturn(expectedResult);
Date result = template.queryForObject(SQL, rm);
assertSame(expectedResult, result);
}
@Test
public void testQueryForObjectWithRowMapperAndArgs() throws Exception {
Date expectedResult = new Date();
ParameterizedRowMapper<Date> rm = new ParameterizedRowMapper<Date>() {
@Override
public Date mapRow(ResultSet rs, int rowNum) {
return new Date();
}
};
given(operations.queryForObject(SQL, ARGS_ARRAY, rm)
).willReturn(expectedResult);
Date result = template.queryForObject(SQL, rm, ARGS_ARRAY);
assertSame(expectedResult, result);
}
@Test
public void testQueryForObjectWithRowMapperAndMap() throws Exception {
String sql = "SELECT SOMEDATE FROM BAR WHERE ID=? AND XY=?";
Date expectedResult = new Date();
ParameterizedRowMapper<Date> rm = new ParameterizedRowMapper<Date>() {
@Override
public Date mapRow(ResultSet rs, int rowNum) {
return new Date();
}
};
given(operations.queryForObject(sql, ARGS_ARRAY, rm)
).willReturn(expectedResult);
Date result = template.queryForObject(sql, rm, ARGS_ARRAY);
assertSame(expectedResult, result);
}
@Test
public void testQueryForListWithoutArgs() throws Exception {
List<Map<String, Object>> expectedResult = mockListMapResult();
given(operations.queryForList(SQL)).willReturn(expectedResult);
List<Map<String, Object>> result = template.queryForList(SQL);
assertSame(expectedResult, result);
}
@Test
public void testQueryForListWithArgs() throws Exception {
List<Map<String, Object>> expectedResult = mockListMapResult();
given(operations.queryForList(SQL, 1, 2, 3)).willReturn(expectedResult);
List<Map<String, Object>> result = template.queryForList(SQL, 1,2,3);
assertSame(expectedResult, result);
}
@Test
public void testQueryForListWithMap() throws Exception {
List<Map<String, Object>> expectedResult = mockListMapResult();
given(namedParameterOperations.queryForList(SQL, ARGS_MAP)).willReturn(expectedResult);
List<Map<String, Object>> result = namedParameterTemplate.queryForList(SQL, ARGS_MAP);
assertSame(expectedResult, result);
}
@Test
public void testQueryForListWithSqlParameterSource() throws Exception {
List<Map<String, Object>> expectedResult = mockListMapResult();
given(namedParameterOperations.queryForList(SQL, ARGS_SOURCE)).willReturn(expectedResult);
List<Map<String, Object>> result = namedParameterTemplate.queryForList(SQL, ARGS_SOURCE);
assertSame(expectedResult, result);
}
@Test
public void testQueryForMapWithoutArgs() throws Exception {
Map<String, Object> expectedResult = new HashMap<String, Object>();
given(operations.queryForMap(SQL)).willReturn(expectedResult);
Map<String, Object> result = template.queryForMap(SQL);
assertSame(expectedResult, result);
}
@Test
public void testQueryForMapWithArgs() throws Exception {
Map<String, Object> expectedResult = new HashMap<String, Object>();
given(operations.queryForMap(SQL, 1, 2, 3)).willReturn(expectedResult);
Map<String, Object> result = template.queryForMap(SQL, 1,2,3);
assertSame(expectedResult, result);
}
@Test
public void testQueryForMapWithMap() throws Exception {
Map<String, Object> expectedResult = new HashMap<String, Object>();
given(namedParameterOperations.queryForMap(SQL, ARGS_MAP)).willReturn(expectedResult);
Map<String, Object> result = namedParameterTemplate.queryForMap(SQL, ARGS_MAP);
assertSame(expectedResult, result);
}
@Test
public void testQueryForMapWithSqlParameterSource() throws Exception {
Map<String, Object> expectedResult = new HashMap<String, Object>();
given(namedParameterOperations.queryForMap(SQL, ARGS_SOURCE)).willReturn(expectedResult);
Map<String, Object> result = namedParameterTemplate.queryForMap(SQL, ARGS_SOURCE);
assertSame(expectedResult, result);
}
@Test
public void testUpdateWithoutArgs() throws Exception {
given(operations.update(SQL)).willReturn(666);
int result = template.update(SQL);
assertEquals(666, result);
}
@Test
public void testUpdateWithArgs() throws Exception {
given(operations.update(SQL, 1, 2, 3)).willReturn(666);
int result = template.update(SQL, 1, 2, 3);
assertEquals(666, result);
}
@Test
public void testUpdateWithMap() throws Exception {
given(namedParameterOperations.update(SQL, ARGS_MAP)).willReturn(666);
int result = namedParameterTemplate.update(SQL, ARGS_MAP);
assertEquals(666, result);
}
@Test
public void testUpdateWithSqlParameterSource() throws Exception {
given(namedParameterOperations.update(SQL, ARGS_SOURCE)).willReturn(666);
int result = namedParameterTemplate.update(SQL, ARGS_SOURCE);
assertEquals(666, result);
}
@Test
public void testBatchUpdateWithSqlParameterSource() throws Exception {
PreparedStatement preparedStatement = setupBatchOperation();
final SqlParameterSource[] ids = new SqlParameterSource[2];
ids[0] = new MapSqlParameterSource("id", 100);
ids[1] = new MapSqlParameterSource("id", 200);
int[] actualRowsAffected = template.batchUpdate("UPDATE NOSUCHTABLE SET DATE_DISPATCHED = SYSDATE WHERE ID = :id", ids);
assertTrue("executed 2 updates", actualRowsAffected.length == 2);
assertEquals(1, actualRowsAffected[0]);
assertEquals(2, actualRowsAffected[1]);
verify(preparedStatement).setObject(1, 100);
verify(preparedStatement).setObject(1, 200);
verify(preparedStatement, times(2)).addBatch();
verify(preparedStatement).close();
}
@Test
public void testBatchUpdateWithListOfObjectArrays() throws Exception {
PreparedStatement preparedStatement = setupBatchOperation();
List<Object[]> ids = new ArrayList<Object[]>();
ids.add(new Object[] { 100 });
ids.add(new Object[] { 200 });
int[] actualRowsAffected = template.batchUpdate(SQL, ids);
assertTrue("executed 2 updates", actualRowsAffected.length == 2);
assertEquals(1, actualRowsAffected[0]);
assertEquals(2, actualRowsAffected[1]);
verify(preparedStatement).setObject(1, 100);
verify(preparedStatement).setObject(1, 200);
verify(preparedStatement, times(2)).addBatch();
verify(preparedStatement).close();
}
@Test
public void testBatchUpdateWithListOfObjectArraysPlusTypeInfo() throws Exception {
int[] sqlTypes = new int[] { Types.NUMERIC };
PreparedStatement preparedStatement = setupBatchOperation();
List<Object[]> ids = new ArrayList<Object[]>();
ids.add(new Object[] { 100 });
ids.add(new Object[] { 200 });
int[] actualRowsAffected = template.batchUpdate(SQL, ids, sqlTypes);
assertTrue("executed 2 updates", actualRowsAffected.length == 2);
assertEquals(1, actualRowsAffected[0]);
assertEquals(2, actualRowsAffected[1]);
verify(preparedStatement).setObject(1, 100, Types.NUMERIC);
verify(preparedStatement).setObject(1, 200, Types.NUMERIC);
verify(preparedStatement, times(2)).addBatch();
verify(preparedStatement).close();
}
private PreparedStatement setupBatchOperation() throws SQLException {
DataSource dataSource = mock(DataSource.class);
Connection connection = mock(Connection.class);
PreparedStatement preparedStatement = mock(PreparedStatement.class);
DatabaseMetaData databaseMetaData = mock(DatabaseMetaData.class);
given(dataSource.getConnection()).willReturn(connection);
given(preparedStatement.getConnection()).willReturn(connection);
given(preparedStatement.executeBatch()).willReturn(new int[] { 1, 2 });
given(databaseMetaData.getDatabaseProductName()).willReturn("MySQL");
given(databaseMetaData.supportsBatchUpdates()).willReturn(true);
given(connection.prepareStatement(anyString())).willReturn(preparedStatement);
given(connection.getMetaData()).willReturn(databaseMetaData);
template = new SimpleJdbcTemplate(new JdbcTemplate(dataSource, false));
return preparedStatement;
}
private List<Map<String, Object>> mockListMapResult() {
return new LinkedList<Map<String, Object>>();
}
}