queryForMap方法使用不当,就会出错,使用方式如下:
The queryForMap method in JdbcTemplate only expects a single row to be returned, The return value for this method will be a map of column names to column values for the single result row.
queryForMap方法返回的结果集大小必须是1,并且返回的map中,以列的名字作为key,获取的值作为value
public void test(){
String SQL_GET_ATTRIBUTE="select * from test where rownum<1";
Map<Object,Object> map=getJdbcTemplate().queryForMap(SQL_GET_ATTRIBUTE);
Set<Entry<Object, Object>> entrySet=map.entrySet();
for (Entry<Object, Object> entry : entrySet) {
System.out.println("key is " + entry.getKey());
System.out.println("value is " + entry.getValue());
}
}
输出会列出test所有字段为key,查询出来的值为vlaue的字符串,这里使用oracle数据库,强制使用“where rownum<1”来限制只返回一行。