遍历存放Map的List集合
周五下午在做数据库移植时,遇到一个问题.原来Oracle语句用到一个wm_concat()函数返回字符串"0123",我在转变成mysql语句的时候
select wm_concat(usertype) as usertype
from ( select usertype from tbUserPurview where userid=1 group by usertype);
我想用这个函数GROUP_CONCAT()代替wm_concat()可是没有成功。sql返回的结果集,虽然也是0123。但用Map中的key对应的value值确实一个内存地址。
Map map = baseDAO.getJdbcTemplate().queryForMap(sql);
select GROUP_CONCAT(fromChildTab.usertype) as usertype
from ( select usertype from tbUserPurview where userid =1 group by usertype) as fromChildTab;
----------------------------
所以我迫不得已 只好用一个简单的sql语句,结果集有3条记录,原来用GROUP_CONCAT()函数,只有一条记录。这回返回一个List集合,里边放的是Map。所以要遍历这个list,然后再拼接字符串、
List list = baseDAO.getJdbcTemplate().queryForList(sql);
[{sertype=0}, {usertype=1}, {usertype=2}, {usertype=3}]
List list = baseDAO.getJdbcTemplate().queryForList(sql);
String str1 ="";//String str2="";
for(Iterator iter = list.iterator();iter.hasNext();){
Map map =(Map)iter.next();
Set keySet = map.keySet();
Iterator it = keySet.iterator();
while(it.hasNext()){
String usertype =(String)it.next();
Object usertype_value =map.get(usertype);//通过键获取值
String str2 = usertype_value.toString();
str1 = str1+str2;
}
}