Mybatis返回Map,List

            上次寫[簡單]Spring_Mybatis_CRUD簡單示例(帶數據庫)遇到一個問題,在返回Map類型時候沒有解析正確,不得不返回一個JavaBean,趁着有空,重新看了下,現在可以用Mybatis返回Map,List<Map>了

            返回Map,Mybatis配置如下

           

Sql代碼  收藏代碼
  1. <select id="getCountyHashMap" resultType="java.util.HashMap">  
  2.         select name,id from  
  3.         tsql_test_region where  
  4.         id=#{id}  
  5.     </select>  

    ServiceImpl如下

  

Java代碼  收藏代碼
  1. public Map<String, Long> getCountyHashMap(long id) {  
  2.         Map<String, Object> regionMap = regionInfoMapper.getCountyHashMap(id);  
  3.         Map<String, Long> resultMap = new HashMap<String, Long>();  
  4.         String region = null;  
  5.         Long vid = null;  
  6.         for (Map.Entry<String, Object> entry : regionMap.entrySet()) {  
  7.             if ("NAME".equals(entry.getKey())) {  
  8.                 region = (String) entry.getValue();  
  9.             } else if ("ID".equals(entry.getKey())) {  
  10.                 vid = ((java.math.BigDecimal) entry.getValue()).longValue();  
  11.             }  
  12.         }  
  13.         resultMap.put(region, vid);  
  14.         return resultMap;  
  15.     }  

    Controller如下:

  

Java代碼  收藏代碼
  1. @RequestMapping(value = "/region3", method = RequestMethod.GET)  
  2.     public @ResponseBody  
  3.     Map<String, Long> getCountyMap(@RequestParam(required = trueint regionId) {  
  4.         return regionInfoService.getCountyHashMap(regionId);  
  5.     }  

    結果爲

 

    

     返回List<Map>類似:

     Mybatis配置:

  

Sql代碼  收藏代碼
  1. <select id="getRegionHashMap" resultType="java.util.HashMap">  
  2.         select name,id from  
  3.         tsql_test_region order by id  
  4.     </select>  

   ServiceImpl如下:

  

Java代碼  收藏代碼
  1. public Map<String, Long> getRegionHashMap() {  
  2.         List<Map<String, Object>> regionMap = regionInfoMapper  
  3.                 .getRegionHashMap();  
  4.         Map<String, Long> resultMap = new HashMap<String, Long>();  
  5.         for (Map<String, Object> map : regionMap) {  
  6.             String region = null;  
  7.             Long id = null;  
  8.             for (Map.Entry<String, Object> entry : map.entrySet()) {  
  9.                 if ("NAME".equals(entry.getKey())) {  
  10.                     region = (String) entry.getValue();  
  11.                 } else if ("ID".equals(entry.getKey())) {  
  12.                     id = ((java.math.BigDecimal) entry.getValue()).longValue();  
  13.                 }  
  14.             }  
  15.             resultMap.put(region, id);  
  16.         }  
  17.         return resultMap;  
  18.     }  

   Controller如下:

  

Java代碼  收藏代碼
  1. @RequestMapping(value = "/region2", method = RequestMethod.GET)  
  2.     public @ResponseBody  
  3.     Map<String, Long> getRegionMap() {  
  4.         return regionInfoService.getRegionHashMap();  
  5.     }  

    結果爲

 

    

      本文系原創,轉載請註明出處,謝謝

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章