- 使用三种Callback接口作为参数的query方法的返回值不同:
- 以ResultSetExtractor作为方法参数的query方法返回Object型结果,要使用查询结果,我们需要对其进行强制转型;
- 以RowMapper接口作为方法参数的query方法直接返回List型的结果;
- 以RowCallbackHandler作为方法参数的query方法,返回值为void;
- RowCallbackHandler和RowMapper才是我们最常用的选择
- * @author Administrator
- *
- */
- public class SpringTest {
- /**
- * 返回结果是List里装Map,使用参数,使用回调 RowMapperResultSetExtractor用于处理单行记录,
- * 它内部持有一个RowMapper实例的引用,当处理结果集的时候, 会将单行数据的处理委派给其所持有的RowMapper实例,而其余工作它负责
- */
- public void getListRowMapperResultSetExtractor() {
- ApplicationContext context = new FileSystemXmlApplicationContext(
- "src/database_config.xml");
- // E:/demoworkspace/spring 为工程主目录
- JdbcTemplate jt = new JdbcTemplate((DataSource) context
- .getBean("oracleDataSourceTest")); // 测试用的方法
- Object[] arg = new Object[] { 10 };
- List list = (ArrayList) jt.query("select * from region where rownum<?",
- arg, new RowMapperResultSetExtractor(new RowMapper() {
- public Object mapRow(ResultSet rs, int index)
- throws SQLException {
- Map u = new HashMap(); //可以是自己的JavaBean值对象(简单Java对象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());
- }
- }
- /**返回结果是List里装Map,不使用参数,使用回调
- 使用RowMapper比直接使用ResultSetExtractor要方便的多,只负责处理单行结果就行,现在,我们只需要将单行的结果组装后返回就行,
- 剩下的工作,全部都是JdbcTemplate内部的事情了。 实际上,JdbcTemplae内部会使用一个ResultSetExtractor实现类来做其余的工作,
- 毕竟,该做的工作还得有人做不是?!
- */
- 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());
- }
- }
- // 返回记录集
- /**
- RowCallbackHandler虽然与RowMapper同是处理单行数据,不过,除了要处理单行结果,它还得负责最终结果的组装和获取工作,
- 在这里我们是使用当前上下文声明的List取得最终查询结果, 不过,我们也可以单独声明一个RowCallbackHandler实现类,
- 在其中声明相应的集合类,这样,我们可以通过该RowCallbackHandler实现类取得最终查询结果
- */
- 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>(); //一定要用final定义
- Object[] params = new Object[] { 0 };
- jt.query(sql, params, new RowCallbackHandler() {
- public void processRow(ResultSet rs) throws SQLException {
- Map u = new HashMap();
- 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());
- }
- }
- 转自:http://blog.csdn.net/taozi165/article/details/6578031
Spring 2.5中JdbcTemplate类query方法的三种回调接口
最新推荐文章于 2023-08-17 00:58:00 发布