[Z] Three callbacks using the JdbcTemplate query

Source: Internet
Author: User

From http://blog.csdn.net/e5945/article/details/6004921

There are three main callback interface definitions for queries:

org.springframework.jdbc.core.ResultSetExtractor. basically belongs to the callback interface used internally by JdbcTemplate, relative to the following two callback interfaces , ResultSetExtractor has more control, because using it, you need to handle resultset yourself:

 Public Interface  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 {voidthrows  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  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:

List customerlist = (list) jdbctemplate.query ("SELECT * FROM Customer",NewResultSetExtractor () { PublicObject Extractdata (ResultSet rs)throwssqlexception,dataaccessexception {List customers=NewArrayList (); while(Rs.next ()) {Customer Customer=NewCustomer (); Customer.setfirstname (Rs.getstring (1)); Customer.setlastname (Rs.getstring (2));... Customers.add (customer);}returncustomers;}}); List customerlist= Jdbctemplate.query ("SELECT * FROM Customer",NewRowMapper () { PublicObject Maprow (ResultSet RS,intRowNumber)throwsSQLException {Customer Customer=NewCustomer (); Customer.setfirstname (Rs.getstring (1)); Customer.setlastname (Rs.getstring (2));...returncustomer;}});FinalList customerlist =NewArrayList (); Jdbctemplate.query ("SELECT * FROM Customer",NewRowCallbackHandler () { Public voidProcessrow (ResultSet rs)throwsSQLException {Customer Customer=NewCustomer (); 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 we need to make a forced transformation to use the result.

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;

[Z] Three callbacks using the JdbcTemplate query

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.