mybatis返回List<Map>

mapperl.xml中:

<select id="getAmount" parameterType="int" resultType="java.util.HashMap">     
    <![CDATA[   
        select count(*) as amount, estate_type as type, status from estate where status=#{status}  group by estate_type 
    ]]>    
</select>  

在mapper.java中:

List<Map> getAmount(int status);  

需要注意的问题:

1.注意select标签上设置为resultType,而不是ibatis支持的resultClass

2.返回map时select列中最好设置别名。(经验证,当不设置别名时,你需要这个来取数据map.get("count(*)"))。

3.在mybatis中,无论你指定还是不指定返回类型,mybatis都会默认的先将查询回的值放入一个hashMap中(如果返回的值不止一条就是一个包含hashMap的list)。这其中的区别在于,如果你指定了返回类型,mybatis将会根据返回类型的实体类来从hashMap中获取值并set到这个实体类中。如果不指定就默认返回一个HashMap<String,Object>(List<HashMap<String,Object>>)。

原文地址:https://www.cnblogs.com/fangts/p/7392336.html