Spring Study[12]
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.
转载于:https://www.cnblogs.com/tongzhiyong/archive/2007/11/09/954776.html
- Spring Study[12]
- TCP/IP详解学习笔记(12)-TCP的超时与重传
- 深入理解javascript原型和闭包(12)——简介【作用域】
- 记《浪潮之巅》-第一版-12.短暂的春秋--与机会失之交臂的公司之四Real Networks
- vb.net 教程 3-12 资源文件 1-3
- PKCS12 证书的生成及验证
- android基础知识12:android自动化测试06—Instrumentation 05 InstrumentationTestRunner
- 输入正整数k,找到所有的正整数x>=y,使得1/k=1/x + 1/y; 样例输入: 2 12 样例输出: 2 1/2 = 1/6 + 1/3 1/
- 12-高阶函数
- 深入理解JavaScript系列(12) 变量对象(Variable Object)
- 你好,C++(12)如何管理多个类型相同性质相同的数据?3.6 数组
- linux命令(12)uniq去重
- 12_打印1到最大的N位数
- 记录:《故乡的槐树》选入山东省日照市11-12学年度上学期高一语文模块考试卷(新)...
- SQL Server 对表的 12 种一般性操作
- 你应该知道的大数据领域12大动向
- 微信公众号开发获取access token方法,源代码-微信开发教程12
- cocos2d-x初探学习笔记(12)--图形绘制
- Intellij IDEA 12 详细开发教程(三)忽略编译错误设置和快捷键
- HTML网页开发学习笔记(燕十八)-12 margin首页实战