码迷,mamicode.com
首页 > 其他好文 > 详细

用Mybatis返回Map,List<Map>

时间:2016-10-12 17:08:32      阅读:261      评论:0      收藏:0      [点我收藏+]

标签:

 

            返回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 = "/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 = "/region2", method = RequestMethod.GET)
  public @ResponseBody
  Map<String, Long> getRegionMap() {
    return regionInfoService.getRegionHashMap();
  }

结果为 :

技术分享 

用Mybatis返回Map,List<Map>

标签:

原文地址:http://www.cnblogs.com/huhuixin/p/5953302.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!