一、org.springframework.jdbc.core.ResultSetExtractor. java
基本上屬於JdbcTemplate內部使用的Callback接口,相對於下面兩個Callback接口來講,ResultSetExtractor擁有更多的控制權,由於使用它,你須要自行處理ResultSet:spring
package org.springframework.jdbc.core; import java.sql.ResultSet; import java.sql.SQLException; import org.springframework.dao.DataAccessException; public interface ResultSetExtractor { Object extractData(ResultSet rs) throws SQLException, DataAccessException; }
在直接處理完ResultSet以後,你能夠將處理後的結果以任何你想要的形式包裝後返回。 sql
二、org.springframework.jdbc.core.RowCallbackHandler. app
RowCallbackHandler相對於ResultSetExtractor來講,僅僅關注單行結果的處理,處理後的結果能夠根據須要存放到當前RowCallbackHandler對象內或者使用JdbcTemplate的程序上下文中,固然,這個徹底是看我的愛好了。ide
RowCallbackHandler的定義以下: this
package org.springframework.jdbc.core; import java.sql.ResultSet; import java.sql.SQLException; public interface RowCallbackHandler { void processRow(ResultSet rs) throws SQLException; }
三、org.springframework.jdbc.core.RowMapper.google
它是 ResultSetExtractor的精簡版,功能相似於 RowCallbackHandler,也是隻關注當行結果的處理。不過它的返回的結果會有 ResultSetExtractor實現類進行組合。spa
RowMapper的接口定義以下:code
package org.springframework.jdbc.core; import java.sql.ResultSet; import java.sql.SQLException; public interface RowMapper { Object mapRow(ResultSet rs, int rowNum) throws SQLException; }
爲了說明這三種回調接口的使用方法,咱們暫時設置以下的場景: 假設咱們有一表users,裏面有userid,username,userpwd三個字段,咱們爲此創建了一個JavaBean:對象
package com.google.spring.jdbc; public class UserBean{
private Integer userId; private String username; private String userpwd; public Integer getUserId(){ return userId; } public void setUserId(Integer userId){ this.userId = userId; } public String getUsername(){ return username; } public void setUsername(String username){ this.username = username; } public String getUserpwd(){ return userpwd; } public void setUserpwd(String userpwd){ this.userpwd = userpwd; } }
使用自定義的ResultSetExtractor,能夠以下進行處理:
List users = (List)jdbcTemplate.query("SELECT * FROM USERS WHERE USERNAME LIKE '%n%'", new ResultSetExtractor() { @Override public Object extractData(ResultSet rs) throws SQLException, DataAccessException { List users = new ArrayList(); while(rs.next()) { UserBean userBean = new UserBean(); userBean.setUserId(rs.getInt("userId")); userBean.setUsername(rs.getString("username")); userBean.setUserpwd(rs.getString("userpwd")); users.add(userBean); } return users; } }); System.out.println(users);
使用RowCallbackHandler可進行以下的處理:
final List users = new ArrayList(); jdbcTemplate.query("SELECT * FROM USERS WHERE USERNAME LIKE '%n%'", new RowCallbackHandler() { @Override public void processRow(ResultSet rs) throws SQLException { UserBean userBean = new UserBean(); userBean.setUserId(rs.getInt("userId")); userBean.setUsername(rs.getString("username")); userBean.setUserpwd(rs.getString("userpwd")); users.add(userBean); } }); System.out.println(users.size());
使用RowMapper,可進行以下的處理:
List users = jdbcTemplate.query("SELECT * FROM USERS WHERE USERNAME LIKE '%n%'", new RowMapper() { @Override public Object mapRow(ResultSet rs, int rowNum) throws SQLException { UserBean userBean = new UserBean(); userBean.setUserId(rs.getInt("userId")); userBean.setUsername(rs.getString("username")); userBean.setUserpwd(rs.getString("userpwd")); return userBean; } }); System.out.println(users.size());
以上是以jdbcTemplate爲例,介紹了3種回調接口的用法,其實還能夠擴展到hbaseTemplate上