欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

jdbc查询返回来的List> 转换成List

程序员文章站 2022-07-01 07:49:57
...

 my.oschina.net/jiefalcon/blog/136581

jdbc查询返回来的List<Map<String, Object>> 转换成List<Users>

public List find() {
	    String sql = "select * from users";
	    List<Users> users = new ArrayList<Users>();
	    List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
	    for (int i = 0; i < list.size(); i++) {//遍历设置实体
	        Users user = new Users();
	        user.setId((Integer) list.get(i).get("id"));
	        user.setPassword(list.get(i).get("pwd").toString());
	        user.setUserName(list.get(i).get("uname").toString());
	        users.add(user);//添加到List<Users>
	    }

 

另外一种方式:

for(Map<String,Object> map:list){
  for(Map.Entry<String,Object> e:map.entrySet()){
  e.getKey() 和 e.getValue()
  }
} 

 第种方式,待测试

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("key1", "value1");
		map.put("key2", "value2");
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		list.add(map);

		for (Map<String, Object> m : list) {
			for (String k : m.keySet()) {
				System.out.println(k + " : " + m.get(k));
			}
		}