一般圖表繪制例如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);