2
我有一個實現RowMapper接口的行映射器類。我需要在那裏實現mapRow方法。它的參數是ResulSet和索引。我想在另一個bean中使用這個ResultSet對象。我如何得到它?在春天,如何將對象從一個bean傳遞到另一個bean?
我有一個實現RowMapper接口的行映射器類。我需要在那裏實現mapRow方法。它的參數是ResulSet和索引。我想在另一個bean中使用這個ResultSet對象。我如何得到它?在春天,如何將對象從一個bean傳遞到另一個bean?
將此對象設置爲實例變量。但我絕不會推薦用於ResultSet。一旦春季關閉,結果集將無用武之地(春天正在管理它)。
更好地從ResultSet中提取數據將數據存儲爲實例變量中的某個模型bean(但請記住,默認情況下,bean是單例,並且對於每個將實例存儲爲實例變量的執行沒有多大意義其一)。
EDIT--沒關係細化一點,我把一個例子這裏
// this is singleton by default, if you want to store data in this bean
// mark it as bean either by annotation or thorugh xml
// so that it can be accessed through spring context
public class MyBeanRowMapper implements RowMapper
{
// you can store something here,
// but with each execution of mapRow, it will be updated
// not recommended to store execution result here
// or inject other bean here // SomeOtherBean someOtherbean;
public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
MyBean myBean = new MyBean();
myBean.setId(rs.getInt("BEAN_ID"));
myBean.setName(rs.getString("NAME"));
// set this bean into gloablly accessible object here
// or to the bean in which you want to access the result
// something like -->// someOtherBean.setMyBean(myBean)
// again be careful if someOtherBean is singleton by default
return myBean;
}
}
你能有一些代碼闡述。我對春天來說很新鮮。 – Sriram
更新了答案 – kunal
謝謝Kunal。現在,我需要在配置文件中定義的另一個bean中使用此ResultSet rs對象。我將下面的屬性放在寫入bean中我想用上面的(HeaderCopierCallBack)定義的bean中的ResultSet對象 –
Sriram