提交 7287baef 编写于 作者: J Juergen Hoeller

GenericSqlQuery configured with RowMapper instance

Issue: SPR-14489
上级 aae4874b
/*
* 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;
private RowMapper<T> rowMapper;
@SuppressWarnings("rawtypes")
private Class<? extends RowMapper> rowMapperClass;
RowMapper<?> rowMapper;
/**
* 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)
throws IllegalAccessException, InstantiationException {
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,11 +42,12 @@ import static org.mockito.BDDMockito.*;
/**
* @author Thomas Risberg
* @author Juergen Hoeller
*/
public class GenericSqlQueryTests {
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;
......@@ -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>
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册