首页 > 代码库 > 用Mybatis返回Map,List<Map>
用Mybatis返回Map,List<Map>
返回Map,Mybatis配置如下 :
<select id="getCountyHashMap" resultType="java.util.HashMap"> select name,id from tsql_test_region where id=#{id} </select>
ServiceImpl如下 :
public Map<String, Long> getCountyHashMap(long id) { Map<String, Object> regionMap = regionInfoMapper.getCountyHashMap(id); Map<String, Long> resultMap = new HashMap<String, Long>(); String region = null; Long vid = null; for (Map.Entry<String, Object> entry : regionMap.entrySet()) { if ("NAME".equals(entry.getKey())) { region = (String) entry.getValue(); } else if ("ID".equals(entry.getKey())) { vid = ((java.math.BigDecimal) entry.getValue()).longValue(); } } resultMap.put(region, vid); return resultMap; }
Controller如下 :
@RequestMapping(value = http://www.mamicode.com/"/region3", method = RequestMethod.GET) public @ResponseBody Map<String, Long> getCountyMap(@RequestParam(required = true) int regionId) { return regionInfoService.getCountyHashMap(regionId); }
结果为 :
返回List<Map>类似 :
Mybatis配置 :
<select id="getRegionHashMap" resultType="java.util.HashMap"> select name,id from tsql_test_region order by id </select>
ServiceImpl如下 :
public Map<String, Long> getRegionHashMap() { List<Map<String, Object>> regionMap = regionInfoMapper .getRegionHashMap(); Map<String, Long> resultMap = new HashMap<String, Long>(); for (Map<String, Object> map : regionMap) { String region = null; Long id = null; for (Map.Entry<String, Object> entry : map.entrySet()) { if ("NAME".equals(entry.getKey())) { region = (String) entry.getValue(); } else if ("ID".equals(entry.getKey())) { id = ((java.math.BigDecimal) entry.getValue()).longValue(); } } resultMap.put(region, id); } return resultMap; }
Controller如下 :
@RequestMapping(value = http://www.mamicode.com/"/region2", method = RequestMethod.GET) public @ResponseBody Map<String, Long> getRegionMap() { return regionInfoService.getRegionHashMap(); }
结果为 :
用Mybatis返回Map,List<Map>
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。