<context:component-scan base-package="com.apress.prospring3.ch8.dao.jdbc.annotation"/>
<context:annotation-config/>
<jdbc:embedded-database id="dataSource" type="H2">
<jdbc:script location="classpath:schema.sql"/>
<jdbc:script location="classpath:test-data.sql"/>
</jdbc:embedded-database>
</beans>
There's nothing special about the configuration; we just declare the embedded database using H2
and use <context:component-scan> for automatic Spring bean discovery. Having the infrastructure in
place, we can now proceed to the implementation of JDBC operations.
Querying Data Using MappingSqlQuery<T>
Spring provides the MappingSqlQuery<T> class for modeling query operations. Basically, we construct a
MappingSqlQuery<T> class using the data source and the query string. On the other hand, we implement
the mapRow() method to map each resultset record into the corresponding domain object.
Let's implement the findAll() method first. We begin by creating the SelectAllContacts class
(which represents the query operation for selecting all contacts) that extends the MappingSqlQuery<T>
abstract class. Listing 8-35 shows the SelectAllContacts class.
Listing 8-35. The SelectAllContacts Class
package com.apress.prospring3.ch8.dao.jdbc.annotation;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.sql.DataSource;
import org.springframework.jdbc.object.MappingSqlQuery;
import com.apress.prospring3.ch8.domain.Contact;
public class SelectAllContacts extends MappingSqlQuery<Contact> {
private static final String SQL_SELECT_ALL_CONTACT =
"select id, first_name, last_name, birth_date from contact";
public SelectAllContacts(DataSource dataSource) {
super(dataSource, SQL_SELECT_ALL_CONTACT);
}
protected Contact mapRow(ResultSet rs, int rowNum) throws SQLException {
Contact contact = new Contact();
contact.setId(rs.getLong("id"));
contact.setFirstName(rs.getString("first_name"));
contact.setLastName(rs.getString("last_name"));
Search WWH :
Custom Search
Previous Page
Spring Framework 3 Topic Index
Next Page
Spring Framework 3 Bookmarks
Home