mybatis mapper把传递来的参数当作字段写入结果集中
<resultMap id="StatsResultMap" type="com.extracme.evshare.business.entity.StationStatsInfo" >
<result column="Station_Id" jdbcType="VARCHAR" property="StationID" />
<result column="Station_Electricity" jdbcType="DOUBLE" property="StationElectricity" />
<result column="StartTime" property="StartTime" />
<result column="EndTime" property="EndTime" />
<collection property="EquipmentStatsInfos" javaType="ArrayList" select="com.extracme.evshare.business.tserver.mapper.EquipmentStatsMapper.queryEquipmentStatsInfoByStatsId"
column="{StationStatsId=stationStatsId,EndTime=EndTime,StartTime=StartTime}" ofType="com.extracme.evshare.business.entity.EquipmentStatsInfo">
</collection>
</resultMap>
-------------mapper把传递来的参数当作字段写入结果集中----------
<select id="queryStationStats" parameterType="java.util.Map" resultMap="StatsResultMap">
select
# {startTime,jdbcType=VARCHAR} as StartTime,
# {endTime,jdbcType=VARCHAR} as EndTime,
Station_Stats_Id stationStatsId ,
Station_Id
from station_stats
where 1=1
<if test="stationId != null and stationId !='' " >
and Station_Id = #{stationId,jdbcType=VARCHAR}
</if>
<if test="startTime != null and startTime !='' ">
and UNIX_TIMESTAMP(Start_Time) >= UNIX_TIMESTAMP(#{startTime,jdbcType=VARCHAR})
</if>
<if test="endTime != null and endTime !='' ">
and UNIX_TIMESTAMP(End_Time) <= UNIX_TIMESTAMP(#{endTime,jdbcType=CARCHAR})
</if>
<!-- GROUP BY stationStatsId WITH ROLLUP -->
</select>
ps:求站的总电量,站下各个设备的总量以及设备下各个接口的总量;一个站在每天会insert一条记录:时间段内query如下
SELECT
s.Station_Stats_Id,
c.Connector_Stats_Id,e.Equipment_Stats_Id,
s.Station_Id,s.Station_Electricity,
e.Equipment_Id,e.Equipment_Electricity,
c.Connector_Id,c.Connector_Electricity
FROM
station_stats s
inner JOIN equipment_stats e ON s.Station_Stats_Id = e.Station_Stats_Id
inner JOIN connector_stats c ON e.Equipment_Stats_Id = c.Equipment_Stats_Id
where s.Station_Id=720 #and s.Start_Time>'2017-07-15 00:00:00' and s.End_Time<'2017-07-18 00:00:00'
AND UNIX_TIMESTAMP(s.Start_Time) >= UNIX_TIMESTAMP('2017-07-15')
AND UNIX_TIMESTAMP(s.End_Time) <= UNIX_TIMESTAMP('2017-07-18')
GROUP BY s.Station_Stats_Id,s.Station_Id,e.Equipment_Id,c.Connector_Id
最终优化之后数据结构:
SELECT
s.Station_Id,
#s.Station_Electricity,
e.Equipment_Id,
#e.Equipment_Electricity,
c.Connector_Id,
SUM(s.Station_Electricity) Station_Electricity,
SUM(e.Equipment_Electricity) Equipment_Electricity,
SUM(c.Connector_Electricity) Connector_Electricity
FROM
station_stats s
inner JOIN equipment_stats e ON s.Station_Stats_Id = e.Station_Stats_Id
inner JOIN connector_stats c ON e.Equipment_Stats_Id = c.Equipment_Stats_Id
where s.Station_Id=720 #and s.Start_Time>'2017-07-15 00:00:00' and s.End_Time<'2017-07-18 00:00:00'
AND UNIX_TIMESTAMP(s.Start_Time) > UNIX_TIMESTAMP('2017-07-15')
AND UNIX_TIMESTAMP(s.End_Time) < UNIX_TIMESTAMP('2017-07-18')
GROUP BY s.Station_Id,e.Equipment_Id,c.Connector_Id