MyBatis的返回参数类型和查询结果遍历
2016-09-20 17:25
561 查看
MyBatis的返回参数类型分两种
1. 对应的分类为:
1.1.resultMap:
1.2.resultType:
2 .对应返回值类型:
2.1.resultMap:结果集
2.2.resultType:int,string ,long ,class
遇到一个问题,在返回Map类型时候没有解析正确,不得不返回一个JavaBean,趁着有空,重新看了下,现在可以用Mybatis返回Map,List<Map>了 。
返回Map,Mybatis配置如下 :
ServiceImpl如下 :
Controller如下 :
结果为 :
返回List<Map>类似 :
Mybatis配置 :
ServiceImpl如下 :
Controller如下 :
结果为 :
本文系原创,转载请注明出处,谢谢 。
1. 对应的分类为:
1.1.resultMap:
1.2.resultType:
2 .对应返回值类型:
2.1.resultMap:结果集
2.2.resultType:int,string ,long ,class
遇到一个问题,在返回Map类型时候没有解析正确,不得不返回一个JavaBean,趁着有空,重新看了下,现在可以用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(); }
结果为 :
本文系原创,转载请注明出处,谢谢 。
相关文章推荐
- 解决mybatis使用char类型字段查询oracle数据库时结果返回null问题
- 创建一个包,其中定义一个过程,可以查询得到编号为7900的员工的姓名ename、薪水sal、佣金comm、上司编号mgr (通过OUT类型的参数将结果返回并打印) (注意包有包头部分的声明和包体部分的
- mybatis使用char类型字段查询oracle数据库时结果返回null
- MyBatis查询语句返回结果类型为HashMap
- mybatis查询的返回类型为基础类型(int、long等),但结果为null时的异常解决
- 使用MyBatis查询 返回类型为int,但是当查询结果为空NULL,报异常的解决方法
- 好947 Mybatis 配置resultMap 带参数查询Map 注意selectOne数据库返回结果一条数据库 否则会报错
- mybatis查询返回类型为基础类型,结果为null时的异常解决
- MyBatis查询结果resultType返回值类型详细介绍
- MyBatis中,传String类型参数到xml配置文件,并执行SQL语句后返回String类型的结果写法(jeesite)
- 关于mybatis中文参数查询不到结果
- [mysql]当mysql查询语句查询的结果为空时,返回query结果是什么类型的呢?
- Ibatis/mybatis中,查询SQL参数和返回都是Map的情况
- mybatis查询数据库返回结果为空
- mybatis返回HashMap结果类型与映射
- mybatis查询参数为0且类型不是string的小问题
- 在使用mybatis时遇到查询结果返回为空的情况,但是查数据库能查到
- Mybatis中查询条件和返回的结果Map封装
- Mybatis进行id类型、String类型、map类型、ben类型参数传入Sql查询
- Mybatis中查询条件和返回的结果Map封装