Posted on 2009-04-08 08:32
landor 阅读(2231)
评论(0) 编辑 收藏 所属分类:
spring
spring的jdbc支持中,2.0中
RowMapperResultSetExtractor 替换1.0中 RowMapperResultReader
但是用法基本一样,以下是个例子:
dbcTemplate jdbcTemplate = new JdbcTemplate(dataSource); // reusable object
RowMapper rowMapper = new UserRowMapper(); // reusable object
List allUsers = (List) jdbcTemplate.query("select * from user",new RowMapperResultSetExtractor(rowMapper, 10));
User user = (User) jdbcTemplate.queryForObject("select * from user where id=?", new Object[] {id},
new RowMapperResultSetExtractor(rowMapper, 1));
//其中UserRowMapper可以这么实现:
public class UserRowMapper implements RowMapper {
public Object mapRow(ResultSet rs, int index) throws SQLException {
// TODO Auto-generated method stub
User user=new User();
user.setId(rs.getString("001"));
user.setPassword(rs.getString("xxx"));
return user;
}
}
参考网址
http://static.springframework.org/spring/docs/2.0.x/api/org/springframework/jdbc/core/RowMapperResultSetExtractor.html