1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.moral.mapper.HistoryFiveMinutelyMapper">
    <select id="getFiveMinutesSensorData" resultType="java.util.LinkedHashMap">
        select
        h.mac
        <foreach collection="sensorKeys" open="," separator="," item="sensorKey">
            ROUND(AVG(json->'$.${sensorKey}[0]'),3) AS 'AVG${sensorKey}',
            ROUND(MIN(json->'$.${sensorKey}[1]'),3) AS 'MIN${sensorKey}',
            ROUND(MAX(json->'$.${sensorKey}[2]'),3) AS 'MAX${sensorKey}'
        </foreach>
        FROM
        history_minutely_${yearAndMonth} h
        where  h.time<![CDATA[>=]]>#{start} and h.time <![CDATA[<=]]>  #{end}
        group by h.mac
    </select>
 
    <insert id="insertHistoryFiveMinutely">
        insert into
        history_five_minutely_${yearAndMonth}
        values
        <foreach collection="list" item="map" separator=",">
            (#{map.mac},#{map.time},#{map.json})
        </foreach>
    </insert>
 
    <update id="createHistoryFiveMinutely" parameterType="String">
        CREATE TABLE  IF NOT EXISTS `history_five_minutely_${yearAndMonth}` (
        `mac` varchar(20) CHARACTER SET latin1 DEFAULT NULL,
        `time` datetime DEFAULT NULL,
        `json` json DEFAULT NULL
        ) ENGINE=INNODB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
    </update>
 
</mapper>