Spring 2.5中JdbcTemplate类query方法的三种回调接口

来源:互联网 发布:js获取焦点和失去焦点 编辑:程序博客网 时间:2024/04/29 08:40
  1. 使用三种Callback接口作为参数的query方法的返回值不同:  
  2. 以ResultSetExtractor作为方法参数的query方法返回Object型结果,要使用查询结果,我们需要对其进行强制转型;  
  3. 以RowMapper接口作为方法参数的query方法直接返回List型的结果;  
  4. 以RowCallbackHandler作为方法参数的query方法,返回值为void; 
  5. RowCallbackHandler和RowMapper才是我们最常用的选择  
  6.  * @author Administrator 
  7.  *  
  8.  */  
  9. public class SpringTest {  
  10.  /** 
  11.   * 返回结果是List里装Map,使用参数,使用回调 RowMapperResultSetExtractor用于处理单行记录, 
  12.   * 它内部持有一个RowMapper实例的引用,当处理结果集的时候, 会将单行数据的处理委派给其所持有的RowMapper实例,而其余工作它负责 
  13.   */  
  14.  public void getListRowMapperResultSetExtractor() {  
  15.   ApplicationContext context = new FileSystemXmlApplicationContext(  
  16.     "src/database_config.xml");  
  17.   // E:/demoworkspace/spring 为工程主目录  
  18.   JdbcTemplate jt = new JdbcTemplate((DataSource) context  
  19.     .getBean("oracleDataSourceTest")); // 测试用的方法  
  20.   Object[] arg = new Object[] { 10 };  
  21.   List list = (ArrayList) jt.query("select * from region where rownum<?",  
  22.     arg, new RowMapperResultSetExtractor(new RowMapper() {  
  23.      public Object mapRow(ResultSet rs, int index)  
  24.        throws SQLException {  
  25.       Map u = new HashMap(); //可以是自己的JavaBean值对象(简单Java对象POJO)  
  26.       u.put("region_id", rs.getString("region_id"));  
  27.       u.put("region_name", rs.getString("region_name"));  
  28.       return u;  
  29.      }  
  30.     }));  
  31.   Iterator it = list.iterator();  
  32.   while (it.hasNext()) {  
  33.    Map map = (Map) it.next();  
  34.    System.out.println(map.toString());  
  35.   }  
  36.  }  
  37.  /**返回结果是List里装Map,不使用参数,使用回调 
  38.   使用RowMapper比直接使用ResultSetExtractor要方便的多,只负责处理单行结果就行,现在,我们只需要将单行的结果组装后返回就行, 
  39.   剩下的工作,全部都是JdbcTemplate内部的事情了。 实际上,JdbcTemplae内部会使用一个ResultSetExtractor实现类来做其余的工作, 
  40.   毕竟,该做的工作还得有人做不是?!   
  41.   */  
  42.  public void getListRowMapper() {  
  43.   ApplicationContext context = new FileSystemXmlApplicationContext(  
  44.     "src/database_config.xml");  
  45.   JdbcTemplate jt = new JdbcTemplate((DataSource) context  
  46.     .getBean("oracleDataSourceTest"));  
  47.   List list = jt.query(  
  48.     "select * from region where rownum<10"new RowMapper() {  
  49.      public Object mapRow(ResultSet rs, int index)  
  50.        throws SQLException {  
  51.       Map u = new HashMap();  
  52.       u.put("region_id", rs.getString("region_id"));  
  53.       u.put("region_name", rs.getString("region_name"));  
  54.       return u;  
  55.      }  
  56.     });  
  57.   Iterator it = list.iterator();  
  58.   while (it.hasNext()) {  
  59.    Map map = (Map) it.next();  
  60.    System.out.println(map.toString());  
  61.   }  
  62.  }  
  63.  // 返回记录集  
  64.  /** 
  65.   RowCallbackHandler虽然与RowMapper同是处理单行数据,不过,除了要处理单行结果,它还得负责最终结果的组装和获取工作, 
  66.   在这里我们是使用当前上下文声明的List取得最终查询结果, 不过,我们也可以单独声明一个RowCallbackHandler实现类, 
  67.   在其中声明相应的集合类,这样,我们可以通过该RowCallbackHandler实现类取得最终查询结果  
  68.   */  
  69.  public void getListRowCallbackHandler() {  
  70.   ApplicationContext context = new FileSystemXmlApplicationContext(  
  71.     "src/database_config.xml");  
  72.   JdbcTemplate jt = new JdbcTemplate((DataSource) context  
  73.     .getBean("oracleDataSourceTest"));  
  74.   String sql = "select * from region  where region_id>?";  
  75.   final List<Map> list=new ArrayList<Map>(); //一定要用final定义  
  76.   Object[] params = new Object[] { 0 };  
  77.   jt.query(sql, params, new RowCallbackHandler() {  
  78.    public void processRow(ResultSet rs) throws SQLException {  
  79.     Map u = new HashMap();    
  80.     u.put("region_id", rs.getString("region_id"));  
  81.     u.put("region_name", rs.getString("region_name"));  
  82.     list.add(u);  
  83.    }  
  84.   });  
  85.     
  86.   Iterator it = list.iterator();  
  87.   while (it.hasNext()) {  
  88.    Map map = (Map) it.next();  
  89.    System.out.println(map.toString());  
  90.   }  
  91.  }  
  92. 转自:http://blog.csdn.net/taozi165/article/details/6578031
原创粉丝点击