zoukankan      html  css  js  c++  java
  • 用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 = "/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();
      }

    结果为 :

     

  • 相关阅读:
    day01-html
    Word中划线的方法(五种)
    关于获取请求参数并处理显示的总结
    确定目标,把握时间,创造机会,努力加油!(关于大三下学期人生规划浅谈)
    重载运算符(一个例子)
    关于连接数据库的那点总结(感觉挺好的)
    Ribbon负载均衡
    idea中一份代码模拟集群
    Eureka集群
    Eureka配置服务提供者、服务消费者、失效剔除和自我保护
  • 原文地址:https://www.cnblogs.com/huhuixin/p/5953302.html
Copyright © 2011-2022 走看看