GenericSqlQuery configured with RowMapper instance

Issue: SPR-14489
This commit is contained in:
Juergen Hoeller
2016-07-19 18:44:06 +02:00
parent aae4874b85
commit 7287baefeb
3 changed files with 72 additions and 27 deletions

View File

@@ -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);

View File

@@ -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>