There are three main callback interface definitions for queries:
-
org.springframework.jdbc.core.ResultSetExtractor. basically belongs to the callback interface used internally by JdbcTemplate, Compared to the following two callback interfaces, ResultSetExtractor has more control, because using it, you need to handle the resultset yourself:
public interface ResultSetExtractor {
Object extractData(ResultSet rs) throws SQLException, DataAccessException;
}
After you have processed the resultset directly, you can wrap the processed results back in any form you want.
-
org.springframework.jdbc.core.RowCallbackHandler. RowCallbackHandler relative to ResultSetExtractor, Just focus on the processing of single-row results, the results can be stored as needed in the current RowCallbackHandler object or use the JdbcTemplate program context, of course, this is entirely a personal hobby. RowCallbackHandler is defined as follows:
public interface RowCallbackHandler {
void processRow(ResultSet rs) throws SQLException;
}
-
The lite version of Org.springframework.jdbc.core.RowMapper. ResultSetExtractor, which functions like RowCallbackHandler and focuses only on the results of processing a single line, but , the processed results are combined by the ResultSetExtractor implementation class. The RowMapper interface is defined as follows:
public interface RowMapper {
Object mapRow(ResultSet rs, int rowNum) throws SQLException;
}
To illustrate the difference between the use of these three callback interfaces and the other, we set the following scenario:
A database table has multiple lines of information in customer, and after querying the table, we need to map the customer information for each row to the domain object customer and return all query results as java.util.List.
Now, we use these three callback interfaces to query the Customer table:
ResultSetExtractor
List customerList = (List)jdbcTemplate.query("select * from customer",
new ResultSetExtractor(){
/span>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;
}
});
RowMapper processing each row
List customerList = jdbcTemplate.query("select * from customer",
new RowMapper(){
P Ublic object Maprow ( resultset RS int RowNumber throws sqlexception {  
Customer customer = new Customer();
customer.setFirstName(rs.getString(1));
customer.setLastName(rs.getString(2));
...
return customer;
}
});
Processrow
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);
}
}
);
If you do not find the biggest difference in where, then let me fine table:
-
The return value of the query method that uses three callback interfaces as parameters is different:
-
The query method with ResultSetExtractor as the method parameter returns the object type result , and to use the result, we need to make a forced transformation to its input line;
-
The query method using the RowMapper interface as the method parameter directly returns the result of the list type ;
-
The Query method takes RowCallbackHandler as the method parameter, and the return value is void;
-
Using ResultSetExtractor as the callback interface to process query results, we need to declare the collection class ourselves, traverse the resultset ourselves, assemble the customer object based on each row of data, and add the assembled customer object to the collection class. , the method is ultimately only responsible for returning the assembled set;
http://dzllikelsw-163-com.iteye.com/blog/2038248
http://blog.csdn.net/congqian1120/article/details/2046311
From for notes (Wiz)
"JdbcTemplate" three callbacks using the JdbcTemplate query