There is also a subinterface you can implement that is useful for retrieving multiple objects through a query. Suppose we want a method that retrieves all of our Person objects. To do this we would implement ResultReader. Spring provides an implementation of this interface that does exactly what we need: RowMapperResultReader.
The RowMapper interface is responsible for mapping a ResultSet row to an object.
class PersonRowMapper implements RowMapper {
public Object mapRow(ResultSet rs, int index)
throws SQLException {
Person person = new Person();
person.setId(new Integer(rs.getInt("id")));
person.setFirstName(rs.getString("first_name"));
person.setLastName(rs.getString("last_name"));
return person;
}
}
public List getAllPersons() {
String sql = "select id, first_name, last_name from person";
return jdbcTemplate.query(
sql, new RowMapperResultReader(new PersonRowMapper()));
}
Similarly, to execute a query to find the last name for a particular person id, we would write a method like this:
public String getLastNameForId(Integer id) {
String sql = "select last_name from person where id = ?";
return (String) jdbcTemplate.queryForObject(
sql, new Object[] { id }, String.class);
}
Calling stored procedures:
public void archiveStudentData() {
CallableStatementCallback cb = new CallableStatementCallback() {
public Object doInCallableStatement(CallableStatement cs)
throws SQLException{
cs.execute();
return null;
}
};
jdbcTemplate.execute("{ ARCHIVE_STUDENTS }", cb);
}
Creating operations as objects:
Spring provides a way to actually model database operations as objects. This adds another layer of insulation between your code and straight JDBC.
To create a reusable object for executing inserts or updates, you subclass the SqlUpdate class.
public class InsertPerson extends SqlUpdate {
public InsertPerson(DataSource ds) {
setDataSource(ds);
setSql("insert into person (id, firstName, lastName) " +
"values (?, ?, ?)";
declareParameter(new SqlParameter(Types.NUMERIC));
declareParameter(new SqlParameter(Types.VARCHAR));
declareParameter(new SqlParameter(Types.VARCHAR));
compile();
}
public int insert(Person person) {
Object[] params = new Object[] {
person.getId(),
person.getFirstName(),
person.getLastName()
};
return update(params);
}
}
private InsertPerson insertPerson;
public int insertPerson(Person person) {
return updatePerson.insert(person);
}
Querying the database with a MappingSqlQuery:
To model a query as an object, we subclass the MappingSqlQuery class.
private class PersonByIdQuery extends MappingSqlQuery {
public PersonByIdQuery(DataSource ds) {
super(ds, "select id, first_name, last_name from person " +
"where id = ?");
declareParameter(new SqlParameter("id", Types.INTEGER));
compile();
}
public Object mapRow(ResultSet rs, int rowNumber)
throws SQLException {
Person person = new Person();
person.setId( (Integer) rs.getObject("id"));
person.setFirstName(rs.getString("first_name"));
person.setLastName(rs.getString("last_name"));
return person;
}
}
private PersonByIdQuery personByIdQuery;
…
public Person getPerson(Integer id) {
Object[] params = new Object[] { id };
return (Person) personByIdQuery.execute(params).get(0);
}
Introducing Spring’s ORM framework support:
1.Lazy loading
2.Eager fetching—This is the opposite of lazy loading.
3.Caching.
4.Cascading.