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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值