封装Spring-RowMapper,使得使用更加灵活

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map; import org.springframework.jdbc.core.RowMapper; public class MapRowMapper implements RowMapper<Map<String, Object>> { private String[] columns; public MapRowMapper(String[] columns) {
this.columns = columns;
} @Override
public Map<String, Object> mapRow(ResultSet rs, int rowNum) throws SQLException {
Map<String, Object> resultMap = new HashMap<String, Object>();
if (columns != null && columns.length > 0) {
if (columns.length > 0) {
for (String col : columns) {
resultMap.put(col, rs.getObject(col));
}
}
}
return resultMap;
} }
测试代码Demo

	public List<Map<String, Object>> queryMapData(){
String tableNames[] = { "TSDEV.FX_DL_DTL_EMGY",
"TSDEV.DL_MEM_FLD_NM",
"TSDEV.DL_ADJ_MEM_FLD_NM"};
Object obj[] = {"uuasroot"};
String columns[] = {"floorName", "productCode", "userCode", "statu"};
StringBuffer sb = new StringBuffer();
sb.append("SELECT T.FLD_NM as floorName ,T.PRDCT_CD as productCode ,T.USR_CD as userCode ,N.ST as statu FROM TSDEV.DL_MEM_FLD_NM T, TSDEV.DL_ADJ_MEM_FLD_NM N WHERE T.PRDCT_CD = N.PRDCT_CD AND N.USR_CD = ?");
TsJdbcTemplate jt = TsJdbcSingletonProvider.get().getTsJdbcTemplateByTable(DealMgmtHelper.PLUGIN_ID, tableNames);
return jt.query(sb.toString(), obj, new MapRowMapper(columns));
// return jt.query(sb.toString(), obj, new BeanPropertyRowMapper<ParamVO>(ParamVO.class)); spring-framework 使用
}
04-23 02:41