Spring Study[12]
2007-11-09 22:33
141 查看
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.
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.
相关文章推荐
- WSS 3.0与Form Server 2007、MOSS 2007各版本功能对比(12)
- 算法题12 数组中所有的逆序对
- 欢迎下载和测试PB混淆器(支持5-12),保护代码免受反编译和破解危害.
- linux shell 编程 12 特殊符号 “&” 以及和他的恩怨
- 网络工程师职场生存发展的12大困惑(转)
- 【Android开发小记--12】同步歌词
- fedora12下安装VirtualBox
- 2016-2-10 linux基础学习12
- OGC标准介绍 12
- 越狱Season 1-Episode 12:Odd Man Out
- 12-13
- SQL Server代理(5/12):理解SQL代理错误日志
- 非常 有感觉 24l01 模块 一次成功!!!使用 stc12 系列 的 单片机
- 深入分析JavaWeb 12 -- jsp运行原理与基本语法
- Fedora 12 添加和配置源
- 机器学习中的神经网络Neural Networks for Machine Learning:Lecture 12 Quiz
- linux试用(1)硬盘安装Fedora core 12
- 12.关于UIWindow的总结
- C# Learning―Step By Step(12)
- 【原创】开源Math.NET基础数学类库使用(12)C#随机数扩展方法