GenericSqlQuery configured with RowMapper instance
Issue: SPR-14489
This commit is contained in:
@@ -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");
|
* 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.
|
||||||
@@ -18,43 +18,57 @@ package org.springframework.jdbc.object;
|
|||||||
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import org.springframework.dao.InvalidDataAccessResourceUsageException;
|
import org.springframework.beans.BeanUtils;
|
||||||
import org.springframework.jdbc.core.RowMapper;
|
import org.springframework.jdbc.core.RowMapper;
|
||||||
import org.springframework.util.Assert;
|
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> {
|
public class GenericSqlQuery<T> extends SqlQuery<T> {
|
||||||
|
|
||||||
Class<?> rowMapperClass;
|
private RowMapper<T> rowMapper;
|
||||||
|
|
||||||
RowMapper<?> rowMapper;
|
|
||||||
|
|
||||||
@SuppressWarnings("rawtypes")
|
@SuppressWarnings("rawtypes")
|
||||||
public void setRowMapperClass(Class<? extends RowMapper> rowMapperClass)
|
private Class<? extends RowMapper> rowMapperClass;
|
||||||
throws IllegalAccessException, InstantiationException {
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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;
|
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
|
@Override
|
||||||
public void afterPropertiesSet() {
|
public void afterPropertiesSet() {
|
||||||
super.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
|
@Override
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
protected RowMapper<T> newRowMapper(Object[] parameters, Map<?, ?> context) {
|
protected RowMapper<T> newRowMapper(Object[] parameters, Map<?, ?> context) {
|
||||||
try {
|
return (this.rowMapper != null ? this.rowMapper : BeanUtils.instantiateClass(this.rowMapperClass));
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,7 +16,6 @@
|
|||||||
|
|
||||||
package org.springframework.jdbc.object;
|
package org.springframework.jdbc.object;
|
||||||
|
|
||||||
|
|
||||||
import java.sql.Connection;
|
import java.sql.Connection;
|
||||||
import java.sql.PreparedStatement;
|
import java.sql.PreparedStatement;
|
||||||
import java.sql.ResultSet;
|
import java.sql.ResultSet;
|
||||||
@@ -43,11 +42,12 @@ import static org.mockito.BDDMockito.*;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Thomas Risberg
|
* @author Thomas Risberg
|
||||||
|
* @author Juergen Hoeller
|
||||||
*/
|
*/
|
||||||
public class GenericSqlQueryTests {
|
public class GenericSqlQueryTests {
|
||||||
|
|
||||||
private static final String SELECT_ID_FORENAME_NAMED_PARAMETERS_PARSED =
|
private static final String SELECT_ID_FORENAME_NAMED_PARAMETERS_PARSED =
|
||||||
"select id, forename from custmr where id = ? and country = ?";
|
"select id, forename from custmr where id = ? and country = ?";
|
||||||
|
|
||||||
private BeanFactory beanFactory;
|
private BeanFactory beanFactory;
|
||||||
|
|
||||||
@@ -57,6 +57,7 @@ public class GenericSqlQueryTests {
|
|||||||
|
|
||||||
private ResultSet resultSet;
|
private ResultSet resultSet;
|
||||||
|
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setUp() throws Exception {
|
public void setUp() throws Exception {
|
||||||
this.beanFactory = new DefaultListableBeanFactory();
|
this.beanFactory = new DefaultListableBeanFactory();
|
||||||
@@ -72,17 +73,23 @@ public class GenericSqlQueryTests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testPlaceHoldersCustomerQuery() throws SQLException {
|
public void testCustomerQueryWithPlaceholders() throws SQLException {
|
||||||
SqlQuery<?> query = (SqlQuery<?>) beanFactory.getBean("queryWithPlaceHolders");
|
SqlQuery<?> query = (SqlQuery<?>) beanFactory.getBean("queryWithPlaceholders");
|
||||||
doTestCustomerQuery(query, false);
|
doTestCustomerQuery(query, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testNamedParameterCustomerQuery() throws SQLException {
|
public void testCustomerQueryWithNamedParameters() throws SQLException {
|
||||||
SqlQuery<?> query = (SqlQuery<?>) beanFactory.getBean("queryWithNamedParameters");
|
SqlQuery<?> query = (SqlQuery<?>) beanFactory.getBean("queryWithNamedParameters");
|
||||||
doTestCustomerQuery(query, true);
|
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 {
|
private void doTestCustomerQuery(SqlQuery<?> query, boolean namedParameters) throws SQLException {
|
||||||
given(resultSet.next()).willReturn(true);
|
given(resultSet.next()).willReturn(true);
|
||||||
given(resultSet.getInt("id")).willReturn(1);
|
given(resultSet.getInt("id")).willReturn(1);
|
||||||
|
|||||||
@@ -6,7 +6,7 @@
|
|||||||
|
|
||||||
<bean id="dataSource" class="org.springframework.jdbc.datasource.TestDataSourceWrapper"/>
|
<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="dataSource" ref="dataSource"/>
|
||||||
<property name="sql" value="select id, forename from custmr where id = ? and country = ?"/>
|
<property name="sql" value="select id, forename from custmr where id = ? and country = ?"/>
|
||||||
<property name="parameters">
|
<property name="parameters">
|
||||||
@@ -50,4 +50,28 @@
|
|||||||
<property name="rowMapperClass" value="org.springframework.jdbc.object.CustomerMapper"/>
|
<property name="rowMapperClass" value="org.springframework.jdbc.object.CustomerMapper"/>
|
||||||
</bean>
|
</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>
|
</beans>
|
||||||
Reference in New Issue
Block a user