@Test public void testSelectClauseReturnMay() { String hql = " select new map(s.name as name,s.tel as tel,s.address as address) from Seller s"; Query query = session.createQuery(hql); List<Map> maps = query.list(); for(Map map : maps) { System.out.println("name:"+map.get("0") +", tel:"+map.get("1") +", address:"+map.get("2")); } System.out.println("###########"); //以别名获取信息 for(Map map : maps) { System.out.println("name:"+map.get("name") +", tel:"+map.get("tel") +", address:"+map.get("address")); } } 控制台输出: name:null, tel:null, address:null name:null, tel:null, address:null name:null, tel:null, address:null name:null, tel:null, address:null ########### name:A服装店, tel:13000000000, address:中国北京××区 name:B数码店, tel:15800000000, address:中国浙江杭州市××区 name:C电器店, tel:13012341234, address:中国广东深圳市××区 name:D书店, tel:18600000000, address:中国陕西西安市××区
为什么用别名就有数据输出了?求解!!
因为Map里面只有key 和 value ,使用别名时会将key(即索引)改为别名,不再是0,1,2。取消掉别名即可按照原索引值(0,1,2)进行控制台输出