Mybatis返回Map,List<Map>
2016-12-06 10:17
471 查看
上次写[简单]Spring_Mybatis_CRUD简单示例(带数据库),遇到一个问题,在返回Map类型时候没有解析正确,不得不返回一个JavaBean,趁着有空,重新看了下,现在可以用Mybatis返回Map,List<Map>了。
返回Map,Mybatis配置如下:
Sql代码
![](https://oscdn.geek-share.com/Uploads/Images/Content/201910/20/7b0ef68357655eefc8e581bfd89413ce.png)
<select id="getCountyHashMap" resultType="java.util.HashMap">
select name,id from
tsql_test_region where
id=#{id}
</select>
ServiceImpl如下:
Java代码
![](https://oscdn.geek-share.com/Uploads/Images/Content/201910/20/7b0ef68357655eefc8e581bfd89413ce.png)
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如下:
Java代码
![](https://oscdn.geek-share.com/Uploads/Images/Content/201910/20/7b0ef68357655eefc8e581bfd89413ce.png)
@RequestMapping(value = "/region3", method = RequestMethod.GET)
public @ResponseBody
Map<String, Long> getCountyMap(@RequestParam(required = true) int regionId) {
return regionInfoService.getCountyHashMap(regionId);
}
结果为:
![](https://oscdn.geek-share.com/Uploads/Images/Content/202003/31/53e5be689caef059eca7be818c342a3f.jpg)
返回List<Map>类似:
Mybatis配置:
Sql代码
![](https://oscdn.geek-share.com/Uploads/Images/Content/201910/20/7b0ef68357655eefc8e581bfd89413ce.png)
<select id="getRegionHashMap" resultType="java.util.HashMap">
select name,id from
tsql_test_region order by id
</select>
ServiceImpl如下:
Java代码
![](https://oscdn.geek-share.com/Uploads/Images/Content/201910/20/7b0ef68357655eefc8e581bfd89413ce.png)
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如下:
Java代码
![](https://oscdn.geek-share.com/Uploads/Images/Content/201910/20/7b0ef68357655eefc8e581bfd89413ce.png)
@RequestMapping(value = "/region2", method = RequestMethod.GET)
public @ResponseBody
Map<String, Long> getRegionMap() {
return regionInfoService.getRegionHashMap();
}
结果为:
![](https://oscdn.geek-share.com/Uploads/Images/Content/202003/31/4fe5d60b3161067d7562cffb4fd0c4cd.jpg)
本文系原创,转载请注明出处,谢谢。
返回Map,Mybatis配置如下:
Sql代码
![](https://oscdn.geek-share.com/Uploads/Images/Content/201910/20/7b0ef68357655eefc8e581bfd89413ce.png)
<select id="getCountyHashMap" resultType="java.util.HashMap">
select name,id from
tsql_test_region where
id=#{id}
</select>
ServiceImpl如下:
Java代码
![](https://oscdn.geek-share.com/Uploads/Images/Content/201910/20/7b0ef68357655eefc8e581bfd89413ce.png)
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如下:
Java代码
![](https://oscdn.geek-share.com/Uploads/Images/Content/201910/20/7b0ef68357655eefc8e581bfd89413ce.png)
@RequestMapping(value = "/region3", method = RequestMethod.GET)
public @ResponseBody
Map<String, Long> getCountyMap(@RequestParam(required = true) int regionId) {
return regionInfoService.getCountyHashMap(regionId);
}
结果为:
![](https://oscdn.geek-share.com/Uploads/Images/Content/202003/31/53e5be689caef059eca7be818c342a3f.jpg)
返回List<Map>类似:
Mybatis配置:
Sql代码
![](https://oscdn.geek-share.com/Uploads/Images/Content/201910/20/7b0ef68357655eefc8e581bfd89413ce.png)
<select id="getRegionHashMap" resultType="java.util.HashMap">
select name,id from
tsql_test_region order by id
</select>
ServiceImpl如下:
Java代码
![](https://oscdn.geek-share.com/Uploads/Images/Content/201910/20/7b0ef68357655eefc8e581bfd89413ce.png)
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如下:
Java代码
![](https://oscdn.geek-share.com/Uploads/Images/Content/201910/20/7b0ef68357655eefc8e581bfd89413ce.png)
@RequestMapping(value = "/region2", method = RequestMethod.GET)
public @ResponseBody
Map<String, Long> getRegionMap() {
return regionInfoService.getRegionHashMap();
}
结果为:
![](https://oscdn.geek-share.com/Uploads/Images/Content/202003/31/4fe5d60b3161067d7562cffb4fd0c4cd.jpg)
本文系原创,转载请注明出处,谢谢。
相关文章推荐
- mybatis查询如何返回List<Map>类型数据
- mybatis查询如何返回List<Map>类型数据
- mybatis返回List<Map>
- Mybatis 如何 返回 List<String> 类型 或 List<Map<String,Object>>类型
- Mybatis无实体类,以List<Map<String,Object>>方式返回
- SSM框架不用实体类,查询单个对象返回Map,多条记录返回list<Map>,及分页
- MyBatis查询,返回值Map或List<Map>
- MyBatis查询,返回值Map或List<Map>
- Mybatis_ 返回List<Map<K,V>> 结构类型数据
- mybatis返回List<Map<String,Object>>
- mybatis返回list<Map>
- mybatis返回List<Map>
- mybatis查询如何返回List<Map>类型数据
- mybatis调用oracle存储过程返回游标 读取到java的List<Map>
- mybatis返回List<Map>
- hibernate 返回List<Map>, List<xxx>对象列表类型
- Spring Mybatis 返回 List<String> 和List<Map<String,Object>>
- Mybatis返回list<object>方法
- hibernate下使用getHibernateTemplate 返回List<Map>
- 支持vector,map,list<map>序列化,反序列化实现