added a concrete GenericSqlQuery class to make it possible to configure in application context (SPR-3986)
This commit is contained in:
parent
4145c299ef
commit
7ccb0b6e66
|
@ -0,0 +1,20 @@
|
|||
package org.springframework.jdbc.object;
|
||||
|
||||
import org.springframework.jdbc.core.RowMapper;
|
||||
import org.springframework.jdbc.Customer;
|
||||
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
|
||||
public class CustomerMapper implements RowMapper<Customer> {
|
||||
|
||||
private static final String[] COLUMN_NAMES = new String[] {"id", "forename"};
|
||||
|
||||
public Customer mapRow(ResultSet rs, int rownum) throws SQLException {
|
||||
Customer cust = new Customer();
|
||||
cust.setId(rs.getInt(COLUMN_NAMES[0]));
|
||||
cust.setForename(rs.getString(COLUMN_NAMES[1]));
|
||||
return cust;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,133 @@
|
|||
/*
|
||||
* 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.object;
|
||||
|
||||
import static org.easymock.EasyMock.createMock;
|
||||
import static org.easymock.EasyMock.expect;
|
||||
import static org.easymock.EasyMock.expectLastCall;
|
||||
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Types;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.easymock.EasyMock;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
import org.springframework.jdbc.AbstractJdbcTests;
|
||||
import org.springframework.jdbc.Customer;
|
||||
import org.springframework.jdbc.datasource.TestDataSourceWrapper;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.beans.factory.BeanFactory;
|
||||
import org.springframework.beans.factory.xml.XmlBeanFactory;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
|
||||
/**
|
||||
* @author Thomas Risberg
|
||||
*/
|
||||
public class GenericSqlQueryTests extends AbstractJdbcTests {
|
||||
|
||||
private static final String SELECT_ID_FORENAME_NAMED_PARAMETERS_PARSED =
|
||||
"select id, forename from custmr where id = ? and country = ?";
|
||||
|
||||
private final boolean debugEnabled = LogFactory.getLog(JdbcTemplate.class).isDebugEnabled();
|
||||
|
||||
private PreparedStatement mockPreparedStatement;
|
||||
private ResultSet mockResultSet;
|
||||
|
||||
private BeanFactory bf;
|
||||
|
||||
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
mockPreparedStatement = createMock(PreparedStatement.class);
|
||||
mockResultSet = createMock(ResultSet.class);
|
||||
this.bf = new XmlBeanFactory(
|
||||
new ClassPathResource("org/springframework/jdbc/object/GenericSqlQueryTests-context.xml"));
|
||||
TestDataSourceWrapper testDataSource = (TestDataSourceWrapper) bf.getBean("dataSource");
|
||||
testDataSource.setTarget(mockDataSource);
|
||||
}
|
||||
|
||||
protected void tearDown() throws Exception {
|
||||
super.tearDown();
|
||||
if (shouldVerify()) {
|
||||
EasyMock.verify(mockPreparedStatement);
|
||||
EasyMock.verify(mockResultSet);
|
||||
}
|
||||
}
|
||||
|
||||
protected void replay() {
|
||||
super.replay();
|
||||
EasyMock.replay(mockPreparedStatement);
|
||||
EasyMock.replay(mockResultSet);
|
||||
}
|
||||
|
||||
public void testPlaceHoldersCustomerQuery() throws SQLException {
|
||||
SqlQuery query = (SqlQuery) bf.getBean("queryWithPlaceHolders");
|
||||
testCustomerQuery(query, false);
|
||||
}
|
||||
|
||||
public void testNamedParameterCustomerQuery() throws SQLException {
|
||||
SqlQuery query = (SqlQuery) bf.getBean("queryWithNamedParameters");
|
||||
testCustomerQuery(query, true);
|
||||
}
|
||||
|
||||
private void testCustomerQuery(SqlQuery query, boolean namedParameters) throws SQLException {
|
||||
expect(mockResultSet.next()).andReturn(true);
|
||||
expect(mockResultSet.getInt("id")).andReturn(1);
|
||||
expect(mockResultSet.getString("forename")).andReturn("rod");
|
||||
expect(mockResultSet.next()).andReturn(false);
|
||||
mockResultSet.close();
|
||||
expectLastCall();
|
||||
|
||||
mockPreparedStatement.setObject(1, new Integer(1), Types.INTEGER);
|
||||
expectLastCall();
|
||||
mockPreparedStatement.setString(2, "UK");
|
||||
expectLastCall();
|
||||
expect(mockPreparedStatement.executeQuery()).andReturn(mockResultSet);
|
||||
if (debugEnabled) {
|
||||
expect(mockPreparedStatement.getWarnings()).andReturn(null);
|
||||
}
|
||||
mockPreparedStatement.close();
|
||||
expectLastCall();
|
||||
|
||||
mockConnection.prepareStatement(SELECT_ID_FORENAME_NAMED_PARAMETERS_PARSED);
|
||||
ctrlConnection.setReturnValue(mockPreparedStatement);
|
||||
|
||||
replay();
|
||||
|
||||
List l;
|
||||
if (namedParameters) {
|
||||
Map<String, Object> params = new HashMap<String, Object>(2);
|
||||
params.put("id", new Integer(1));
|
||||
params.put("country", "UK");
|
||||
l = query.executeByNamedParam(params);
|
||||
}
|
||||
else {
|
||||
Object[] params = new Object[] {new Integer(1), "UK"};
|
||||
l = query.execute(params);
|
||||
}
|
||||
assertTrue("Customer was returned correctly", l.size() == 1);
|
||||
Customer cust = (Customer) l.get(0);
|
||||
assertTrue("Customer id was assigned correctly", cust.getId() == 1);
|
||||
assertTrue("Customer forename was assigned correctly", cust.getForename().equals("rod"));
|
||||
}
|
||||
|
||||
}
|
|
@ -19,7 +19,6 @@ package org.springframework.jdbc.object;
|
|||
import static org.easymock.EasyMock.createMock;
|
||||
import static org.easymock.EasyMock.expect;
|
||||
import static org.easymock.EasyMock.expectLastCall;
|
||||
import static org.easymock.EasyMock.verify;
|
||||
|
||||
import java.sql.CallableStatement;
|
||||
import java.sql.Types;
|
||||
|
@ -64,7 +63,7 @@ public class GenericStoredProcedureTests extends AbstractJdbcTests {
|
|||
protected void tearDown() throws Exception {
|
||||
super.tearDown();
|
||||
if (shouldVerify()) {
|
||||
verify(mockCallable);
|
||||
EasyMock.verify(mockCallable);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,53 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:util="http://www.springframework.org/schema/util"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
|
||||
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd">
|
||||
|
||||
<bean id="dataSource" class="org.springframework.jdbc.datasource.TestDataSourceWrapper"/>
|
||||
|
||||
<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">
|
||||
<list>
|
||||
<bean class="org.springframework.jdbc.core.SqlParameter">
|
||||
<constructor-arg index="0" value="amount"/>
|
||||
<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="custid"/>
|
||||
<constructor-arg index="1">
|
||||
<util:constant static-field="java.sql.Types.VARCHAR"/>
|
||||
</constructor-arg>
|
||||
</bean>
|
||||
</list>
|
||||
</property>
|
||||
<property name="rowMapperClass" value="org.springframework.jdbc.object.CustomerMapper"/>
|
||||
</bean>
|
||||
|
||||
<bean id="queryWithNamedParameters" 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="rowMapperClass" value="org.springframework.jdbc.object.CustomerMapper"/>
|
||||
</bean>
|
||||
|
||||
</beans>
|
Loading…
Reference in New Issue