
/** *//**
使用三種Callback接口作為參數(shù)的query方法的返回值不同:
以ResultSetExtractor作為方法參數(shù)的query方法返回Object型結(jié)果,要使用查詢結(jié)果,我們需要對(duì)其進(jìn)行強(qiáng)制轉(zhuǎn)型;
以RowMapper接口作為方法參數(shù)的query方法直接返回List型的結(jié)果;
以RowCallbackHandler作為方法參數(shù)的query方法,返回值為void;
RowCallbackHandler和RowMapper才是我們最常用的選擇
* @author Administrator
*
*/

public class SpringTest
{

/** *//**
* 返回結(jié)果是List里裝Map,使用參數(shù),使用回調(diào) RowMapperResultSetExtractor用于處理單行記錄,
* 它內(nèi)部持有一個(gè)RowMapper實(shí)例的引用,當(dāng)處理結(jié)果集的時(shí)候, 會(huì)將單行數(shù)據(jù)的處理委派給其所持有的RowMapper實(shí)例,而其余工作它負(fù)責(zé)
*/

public void getListRowMapperResultSetExtractor()
{
ApplicationContext context = new FileSystemXmlApplicationContext(
"src/database_config.xml");
// E:\demoworkspace\spring 為工程主目錄
JdbcTemplate jt = new JdbcTemplate((DataSource) context
.getBean("oracleDataSourceTest")); // 測(cè)試用的方法

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值對(duì)象(簡(jiǎn)單Java對(duì)象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());
}
}

/** *//**返回結(jié)果是List里裝Map,不使用參數(shù),使用回調(diào)
使用RowMapper比直接使用ResultSetExtractor要方便的多,只負(fù)責(zé)處理單行結(jié)果就行,現(xiàn)在,我們只需要將單行的結(jié)果組裝后返回就行,
剩下的工作,全部都是JdbcTemplate內(nèi)部的事情了。 實(shí)際上,JdbcTemplae內(nèi)部會(huì)使用一個(gè)ResultSetExtractor實(shí)現(xiàn)類(lèi)來(lái)做其余的工作,
畢竟,該做的工作還得有人做不是?!
*/

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同是處理單行數(shù)據(jù),不過(guò),除了要處理單行結(jié)果,它還得負(fù)責(zé)最終結(jié)果的組裝和獲取工作,
在這里我們是使用當(dāng)前上下文聲明的List取得最終查詢結(jié)果, 不過(guò),我們也可以單獨(dú)聲明一個(gè)RowCallbackHandler實(shí)現(xiàn)類(lèi),
在其中聲明相應(yīng)的集合類(lèi),這樣,我們可以通過(guò)該RowCallbackHandler實(shí)現(xiàn)類(lèi)取得最終查詢結(jié)果
*/

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());
}
}
posted on 2010-03-10 10:27
J2EE學(xué)習(xí)筆記 閱讀(572)
評(píng)論(0) 編輯 收藏 所屬分類(lèi):
轉(zhuǎn)載 、
spring