Mybatis查询返回多个sum、count数据解决方案

来源:互联网 发布:淘宝店铺招牌怎么做 编辑:程序博客网 时间:2024/06/07 04:50

前言:

对于sum,count这种数据多个进行查询输出,mybatis应该如何对应输出?

示例:

以下数据进行Mybatis输出,输出3个sum结果:

select        sum(case when        status = #{status1} then 1 else 0 end) count1 ,        sum(case when status =        #{status2} then 1 else 0 end) count2 ,        sum(case when status =        #{status3} then 1 else 0 end) count3 ,        count(*) count4        from tb_article

注:case when 不明白可以参考:
http://blog.csdn.net/ink4t/article/details/77624299


解决以上情况可使用通用的Map集合来解决:

// mapper.xml内容如下:<select id="findArticleSumAll" resultType="Map">        select        sum(case when        status = #{status1} then 1 else 0 end) count1 ,        sum(case when status =        #{status2} then 1 else 0 end) count2 ,        sum(case when status =        #{status3} then 1 else 0 end) count3 ,        count(*) count4        from tb_article    </select>
// mapper.java内容如下:Map<String, Integer> findArticleSumAll(@Param("status1") int status1,            @Param("status2") int status2, @Param("status3") int status3);

如果需要获取结果直接使用 map.get(“status1”) 即可。

阅读全文
1 0