報表統計——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);

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM