轉載自:http://www.cnblogs.com/huhuixin/p/5953302.html
用Mybatis返回Map, List<Map>
1. 返回map
regionInfoMapper.xml
<select>
<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);
...
return resultMap;
}
Controller :
@RequestMapping(value = "/region3", method = RequestMethod.GET)
public @ResponseBody
Map<String, Long> getCountyMap(@RequestParam(required = true) int regionId) {
return regionInfoService.getCountyHashMap(regionId);
}
2. 返回List<Map>:
支持多個返回結果的即爲List<Map>
regionInfoMapper.xml
:
<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();
....
return resultMap;
}
Controller如下 :
@RequestMapping(value = "/region2", method = RequestMethod.GET)
public @ResponseBody
Map<String, Long> getRegionMap() {
return regionInfoService.getRegionHashMap();
}