Three kinds of callback interfaces for the JdbcTemplate class query method in Spring 2.5 _query

Source: Internet
Author: User

/** Author: Ouchi qq:35712069 Mobile: 13873195792 Please do not change the original content * */** The return values of the Query method using three callback interfaces as parameters are different: The query method with ResultSetExtractor as the method parameter returns the object type result, we need to force the transformation to use the query result, and the query method with the RowMapper interface as the method parameter returns the result of the list type directly; The query method with RowCallbackHandler as the method parameter, the return value is void; RowCallbackHandler and RowMapper are our most common choices. * @author Administrator * * public class Springtest {/** * Returns the result is the list of map, using parameters, use callback Rowmapperresultsetextractor to process a single record, * It holds a reference to a RowMapper instance, When the result set is processed, the processing of the Single-line data is delegated to the RowMapper instance it holds, while the rest of the work is responsible for/public void Getlistrowmapperresultsetextractor () { ApplicationContext context = new Filesystemxmlapplicationcontext ("Src/database_config.xml"); E:/demoworkspace/spring for the Engineering home directory JdbcTemplate JT = new JdbcTemplate (DataSource) context. Getbean (" Oracledatasourcetest ")); Test method object[] arg = new object[] {10}; List List = (ArrayList) jt.query ("Select * from region where rownum<?", Arg, new Rowmapperresultsetextractor (New Rowmap Per () {public Object Maprow (ResultSet rs, int index) throws SQLException {Map u= new HashMap (); Can be its own JavaBean value object (Simple Java object Pojo) u.put ("region_id", Rs.getstring ("region_id")); U.put ("Region_name", Rs.getstring ("Region_name")); return u; } })); Iterator it = List.iterator (); while (It.hasnext ()) {map map = (map) it.next (); System.out.println (Map.tostring ()); }/** return result is the list of the map, do not use parameters, use callback to use rowmapper than direct use ResultSetExtractor is more convenient, only responsible for processing a single line of results, now, we only need to assemble the results of a single line to return on the row, the rest of the work, It's all inside the JdbcTemplate. In fact, Jdbctemplae will use a ResultSetExtractor implementation class to do the rest of the work, after all, the work has to be done by someone else. */public void Getlistrowmapper () {ApplicationContext context = new Filesystemxmlapplicationcontext ("Src/database_ Config.xml "); JdbcTemplate JT = new JdbcTemplate (DataSource) context. Getbean ("Oracledatasourcetest")); List List = Jt.query ("SELECT * from region where rownum<10", new RowMapper () {public Object Maprow (ResultSet rs, int Index) throws SQLException {Map u = new HashMap (); U.put ("region_id", Rs.getstring ("region_id")); U.put ("Region_name", RS . getString ("Region_name")); return u; } });Iterator it = List.iterator (); while (It.hasnext ()) {map map = (map) it.next (); System.out.println (Map.tostring ()); }//Return recordset/** RowCallbackHandler although it handles single line data with RowMapper, in addition to dealing with single-line results, it is responsible for the assembly and acquisition of the final result, where we use the list of the current context declaration to obtain the final query result , however, we can also declare a RowCallbackHandler implementation class, in which the corresponding collection class is declared, so that we can obtain the final query result through the RowCallbackHandler implementation class/public void Getlistrowcallbackhandler () {ApplicationContext context = new Filesystemxmlapplicationcontext ("Src/database_ Config.xml "); JdbcTemplate JT = new JdbcTemplate (DataSource) context. Getbean ("Oracledatasourcetest")); String sql = "SELECT * from region where region_id>?"; Final list<map> list=new arraylist<map> (); Be sure to use final definition object[] params = new object[] {0}; Jt.query (SQL, params, new RowCallbackHandler () {public void Processrow (ResultSet rs) throws SQLException {Map u = new Ha Shmap (); U.put ("region_id", Rs.getstring ("region_id")); U.put ("Region_name", Rs.getstring ("Region_name")); List.add (U); } }); Iterator it = List.iterator (); WhIle (It.hasnext ()) {map map = (map) it.next (); System.out.println (Map.tostring ()); } }

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.