GenericSqlQuery configured with RowMapper instance
Issue: SPR-14489
This commit is contained in:
parent
aae4874b85
commit
7287baefeb
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
* Copyright 2002-2016 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.
|
||||
|
|
@ -18,43 +18,57 @@ package org.springframework.jdbc.object;
|
|||
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.dao.InvalidDataAccessResourceUsageException;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.jdbc.core.RowMapper;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* A concrete variant of {@link SqlQuery} which can be configured
|
||||
* with a {@link RowMapper}.
|
||||
*
|
||||
* @author Thomas Risberg
|
||||
* @author Juergen Hoeller
|
||||
* @since 3.0
|
||||
* @see #setRowMapper
|
||||
* @see #setRowMapperClass
|
||||
*/
|
||||
public class GenericSqlQuery<T> extends SqlQuery<T> {
|
||||
|
||||
Class<?> rowMapperClass;
|
||||
|
||||
RowMapper<?> rowMapper;
|
||||
private RowMapper<T> rowMapper;
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
public void setRowMapperClass(Class<? extends RowMapper> rowMapperClass)
|
||||
throws IllegalAccessException, InstantiationException {
|
||||
private Class<? extends RowMapper> rowMapperClass;
|
||||
|
||||
|
||||
/**
|
||||
* Set a specific {@link RowMapper} instance to use for this query.
|
||||
* @since 4.3.2
|
||||
*/
|
||||
public void setRowMapper(RowMapper<T> rowMapper) {
|
||||
this.rowMapper = rowMapper;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a {@link RowMapper} class for this query, creating a fresh
|
||||
* {@link RowMapper} instance per execution.
|
||||
*/
|
||||
@SuppressWarnings("rawtypes")
|
||||
public void setRowMapperClass(Class<? extends RowMapper> rowMapperClass) {
|
||||
this.rowMapperClass = rowMapperClass;
|
||||
if (!RowMapper.class.isAssignableFrom(rowMapperClass))
|
||||
throw new IllegalStateException("The specified class '" +
|
||||
rowMapperClass.getName() + " is not a sub class of " +
|
||||
"'org.springframework.jdbc.core.RowMapper'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterPropertiesSet() {
|
||||
super.afterPropertiesSet();
|
||||
Assert.notNull(rowMapperClass, "The 'rowMapperClass' property is required");
|
||||
Assert.isTrue(this.rowMapper != null || this.rowMapperClass != null,
|
||||
"'rowMapper' or 'rowMapperClass' is required");
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
protected RowMapper<T> newRowMapper(Object[] parameters, Map<?, ?> context) {
|
||||
try {
|
||||
return (RowMapper<T>) rowMapperClass.newInstance();
|
||||
}
|
||||
catch (InstantiationException e) {
|
||||
throw new InvalidDataAccessResourceUsageException("Unable to instantiate RowMapper", e);
|
||||
}
|
||||
catch (IllegalAccessException e) {
|
||||
throw new InvalidDataAccessResourceUsageException("Unable to instantiate RowMapper", e);
|
||||
}
|
||||
return (this.rowMapper != null ? this.rowMapper : BeanUtils.instantiateClass(this.rowMapperClass));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -16,7 +16,6 @@
|
|||
|
||||
package org.springframework.jdbc.object;
|
||||
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
|
|
@ -43,6 +42,7 @@ import static org.mockito.BDDMockito.*;
|
|||
|
||||
/**
|
||||
* @author Thomas Risberg
|
||||
* @author Juergen Hoeller
|
||||
*/
|
||||
public class GenericSqlQueryTests {
|
||||
|
||||
|
|
@ -57,6 +57,7 @@ public class GenericSqlQueryTests {
|
|||
|
||||
private ResultSet resultSet;
|
||||
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
this.beanFactory = new DefaultListableBeanFactory();
|
||||
|
|
@ -72,17 +73,23 @@ public class GenericSqlQueryTests {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void testPlaceHoldersCustomerQuery() throws SQLException {
|
||||
SqlQuery<?> query = (SqlQuery<?>) beanFactory.getBean("queryWithPlaceHolders");
|
||||
public void testCustomerQueryWithPlaceholders() throws SQLException {
|
||||
SqlQuery<?> query = (SqlQuery<?>) beanFactory.getBean("queryWithPlaceholders");
|
||||
doTestCustomerQuery(query, false);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNamedParameterCustomerQuery() throws SQLException {
|
||||
public void testCustomerQueryWithNamedParameters() throws SQLException {
|
||||
SqlQuery<?> query = (SqlQuery<?>) beanFactory.getBean("queryWithNamedParameters");
|
||||
doTestCustomerQuery(query, true);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCustomerQueryWithRowMapperInstance() throws SQLException {
|
||||
SqlQuery<?> query = (SqlQuery<?>) beanFactory.getBean("queryWithRowMapperBean");
|
||||
doTestCustomerQuery(query, true);
|
||||
}
|
||||
|
||||
private void doTestCustomerQuery(SqlQuery<?> query, boolean namedParameters) throws SQLException {
|
||||
given(resultSet.next()).willReturn(true);
|
||||
given(resultSet.getInt("id")).willReturn(1);
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
|
||||
<bean id="dataSource" class="org.springframework.jdbc.datasource.TestDataSourceWrapper"/>
|
||||
|
||||
<bean id="queryWithPlaceHolders" class="org.springframework.jdbc.object.GenericSqlQuery">
|
||||
<bean id="queryWithPlaceholders" class="org.springframework.jdbc.object.GenericSqlQuery">
|
||||
<property name="dataSource" ref="dataSource"/>
|
||||
<property name="sql" value="select id, forename from custmr where id = ? and country = ?"/>
|
||||
<property name="parameters">
|
||||
|
|
@ -50,4 +50,28 @@
|
|||
<property name="rowMapperClass" value="org.springframework.jdbc.object.CustomerMapper"/>
|
||||
</bean>
|
||||
|
||||
<bean id="queryWithRowMapperBean" class="org.springframework.jdbc.object.GenericSqlQuery">
|
||||
<property name="dataSource" ref="dataSource"/>
|
||||
<property name="sql" value="select id, forename from custmr where id = :id and country = :country"/>
|
||||
<property name="parameters">
|
||||
<list>
|
||||
<bean class="org.springframework.jdbc.core.SqlParameter">
|
||||
<constructor-arg index="0" value="id"/>
|
||||
<constructor-arg index="1">
|
||||
<util:constant static-field="java.sql.Types.INTEGER"/>
|
||||
</constructor-arg>
|
||||
</bean>
|
||||
<bean class="org.springframework.jdbc.core.SqlParameter">
|
||||
<constructor-arg index="0" value="country"/>
|
||||
<constructor-arg index="1">
|
||||
<util:constant static-field="java.sql.Types.VARCHAR"/>
|
||||
</constructor-arg>
|
||||
</bean>
|
||||
</list>
|
||||
</property>
|
||||
<property name="rowMapper">
|
||||
<bean class="org.springframework.jdbc.object.CustomerMapper"/>
|
||||
</property>
|
||||
</bean>
|
||||
|
||||
</beans>
|
||||
Loading…
Reference in New Issue