1、在Mybatis中写了一个UserMapper.java,一个UserMapper.xml,其中有一个SQL会返回一个hashmap,然后在UserMapper中也是用Map接收,但在测试的时候会出现一个返回多条记录的异常。Map不是本来就可以接收多条吗?难道我非得通过List去接收吗?可是我业务层就是需要用Map,也不想在XML通过resultMap去转。有点感觉多此一举一样。有谁碰到过没?
1、
[code="java"]
public Map queryUserMenuAuthByUser(String userId) ;
[/code]
2、
select auth_code,menu_code from sys_user_auth where user_id=#{userId}
这里会报回多条记录如下
exceptions.TooManyResultsException: Expected one result (or null) to be returned by selectOne()
谁告诉你返回的第一个字段值就是Map的key,第二个字段值就是Map的Value呢???
没有任何官方文档有这种写法,凡事不要想当然好不!
Mybatis返回的Map是这样的一种格式:[color=red]Map<字段名称,字段值>[/color]
一个Map<字段名,字段值>对象代表[color=red]一行[/color]数据!
返回多行(多条)数据当然是多个Map啦,多个Map就只好放在List中,比较只能返回一个参数对象!
用JSON表示Mybatis3返回的List>!
[code="js"][
{'auth_code':'0001', 'menu_code':'M0001'},
{'auth_code':'0002', 'menu_code':'M0002'},
{'auth_code':'0003', 'menu_code':'M0003'}
][/code]
看懂了吱一声!
这个肯定不行啊,因为一个一条查询结果就对应一个map,多条记录当然需要List>来接受返回结果。
dao接口上添加注解@MapKey("auth_code")
这个值就是你map中key的指定名称
参考:http://blog.csdn.net/sou_liu/article/details/47755635