jdbcTemplate query 方法的回调
来源:互联网 发布:linux alias 配置文件 编辑:程序博客网 时间:2024/05/14 21:37
用于查询的回调接口定义主要有以下三种:
org.springframework.jdbc.core.ResultSetExtractor. 基本上属于JdbcTemplate内部使用的Callback接口,相对于下面两个Callback接口来说,ResultSetExtractor拥有更多的控制权,因为使用它,你需要自行处理ResultSet:
public interface ResultSetExtractor {Object extractData(ResultSet rs) throws SQLException, DataAccessException;}
在直接处理完ResultSet之后,你可以将处理后的结果以任何你想要的形式包装后返回。org.springframework.jdbc.core.RowCallbackHandler. RowCallbackHandler相对于ResultSetExtractor来说,仅仅关注单行结果的处理,处理后的结果可以根据需要存放到当前RowCallbackHandler对象内或者使用JdbcTemplate的程序上下文中,当然,这个完全是看个人爱好了。 RowCallbackHandler的定义如下:
public interface RowCallbackHandler {void processRow(ResultSet rs) throws SQLException;}
org.springframework.jdbc.core.RowMapper. ResultSetExtractor的精简版,功能类似于RowCallbackHandler,也只关注处理单行的结果,不过,处理后的结果会由ResultSetExtractor实现类进行组合。 RowMapper的接口定义如下:
public interface RowMapper {Object mapRow(ResultSet rs, int rowNum) throws SQLException; }
为了说明这三种Callback接口的使用和相互之间的区别,我们暂且设定如下场景:
数据库表customer中存在多行信息,对该表查询后,我们需要将每一行的顾客信息都映射到域对象Customer中,并以java.util.List的形式返回所有的查询结果。
现在,我们分别使用这三种Callback接口对customer表进行查询:
List customerList = (List)jdbcTemplate.query("select * from customer", new ResultSetExtractor(){public Object extractData(ResultSet rs) throws SQLException,DataAccessException {List customers = new ArrayList();while(rs.next()){Customer customer = new Customer();customer.setFirstName(rs.getString(1));customer.setLastName(rs.getString(2));...customers.add(customer);}return customers;}});
List customerList = jdbcTemplate.query("select * from customer", new RowMapper(){public Object mapRow(ResultSet rs, int rowNumber) throws SQLException {Customer customer = new Customer();customer.setFirstName(rs.getString(1));customer.setLastName(rs.getString(2));...return customer;}});
final List customerList = new ArrayList();jdbcTemplate.query("select * from customer", new RowCallbackHandler(){public void processRow(ResultSet rs) throws SQLException {Customer customer = new Customer();customer.setFirstName(rs.getString(1));customer.setLastName(rs.getString(2));...customerList.add(customer);}});
如果你没有发现最大的差异在哪里,那么容我细表:
使用三种Callback接口作为参数的query方法的返回值不同:
以ResultSetExtractor作为方法参数的query方法返回Object型结果,要使用查询结果,我们需要对其进行强制转型;
以RowMapper接口作为方法参数的query方法直接返回List型的结果;
以RowCallbackHandler作为方法参数的query方法,返回值为void;
使用ResultSetExtractor作为Callback接口处理查询结果,我们需要自己声明集合类,自己遍历ResultSet,自己根据每行数据组装Customer对象,自己将组装后的Customer对象添加到集合类中,方法最终只负责将组装完成的集合返回;
截取自:http://blog.csdn.net/congqian1120/archive/2008/01/16/2046311.aspx
- jdbcTemplate query 方法的回调
- JDBCTemplate基础方法query()的运用
- JdbcTemplate query查询方法
- JdbcTemplate query查询方法
- jdbctemplate.query()方法详解
- Spring 2.5中JdbcTemplate类query方法的三种回调接口
- Spring 2.5中JdbcTemplate类query方法的三种回调接口
- Spring 2.5中JdbcTemplate类query方法的三种回调接口
- Spring 2.5中JdbcTemplate类query方法的三种回调接口
- Spring 2.5中JdbcTemplate类query方法的三种回调接口
- Spring 2.5中JdbcTemplate类query方法的三种回调接口
- Spring中JdbcTemplate类query方法的三种回调接口
- java中使用jdbcTemplate的query方法举例与总结
- java中使用jdbcTemplate的query方法举例与总结
- JdbcTemplate query
- spring jdbcTemplate 使用占位符(?)的query方法进行多表查询
- Spring JDBCTemplate Query方法查询,改善系统效能
- JdbcTemplate之query
- 不得不知的容器生态圈发展趋势
- 1055: 【数组的距离】
- 【Unity】Unity中实现全局管理类的几种方式
- git详细操作
- 3A DC/DC 降压芯片电路,开关电源
- jdbcTemplate query 方法的回调
- 石油大OJ 1023 问题 L: Pong’s Birds【概率Dp】
- Java中Volatile关键字详解
- Error request for member in something not a structure or union(指针类型的转换)
- Android XML布局
- Linux网络协议栈 — skb
- app冷启动缓慢出现白屏之终极解决方案—IntentService高效异步处理
- JXLS中文教程
- mysql处理高并发,防止库存超卖