报表统计——java实现查询某年12个月数据,没数据补0

一般图表绘制例如echarts等,返回数据格式都大同小异。重点是利用sql或者java实现数据格式的转型,接下来是关键部分:

1.mapper层sql语句,返回统计好的月份与对应月份的数据。

<select id="getAllOrderCountByYear" parameterType="pd" resultType="OrderCount" >
        SELECT 
            sum(t.ordercount) ordercount,
            t.[month] month 
        from 
            order t
     where
        t.year=#{year} GROUP BY t.[month] ORDER BY t.[month] asc
</select>

2.service调用mapper,并将返回的数据转换成图表统计需要的数据格式(重点)

   Map<String,String> resultMap = new HashMap<String,String>();
//获取到数据库搜索的年份对应12个月份的订单量
    List<OrderCount> orderCountList = orderCountManager.getAllOrderCountByYear(pd);
    //定义数组
    int[] countVIP = new int[12];
    //将获取到不同月份对应不同的订单量放在数组中
    if (orderCountList.size()>0 && orderCountList!=null) {
        for (int i=0;i<orderCountList.size();i++) {
            OrderCount oc = orderCountList.get(i);
            if(oc!=null){
                //填充月份对应的数据
                countVIP[oc.getMonth()-1] = oc.getOrderCount();
            }
        }
    }
    //String countVIP2 = changArrToString(countVIP);
    //放入返回结果map中    
    resultMap.put("orderStaticstis", countVIP);
原文地址:https://www.cnblogs.com/yangyuke1994/p/10017575.html