<resultMap id="AnalogValMap" type="com.cetcht.common.vo.echarts.TimeSeries"> <result column="name" jdbcType="VARCHAR" property="name" /> <collection property="values" ofType="com.cetcht.common.vo.echarts.TimeData"> <result column="val" jdbcType="DECIMAL" property="data" /> <result column="created_date" jdbcType="TIMESTAMP" property="time" /> </collection> </resultMap>
collection中的两个字段我想放在 Object[] 中,但是不知道怎么配置,只好暂时先用对象 timedata接收,再做二次处理
处理后的数据如下:
处理方案有两种,一种是前端处理:
async getAnalogData () { let {data} = await AnalogVal.timeLineData(); this.analogHistoryData = data.map(function (item) { return {name: item.name, values: item.values.map(function (item) { return [item.time, item.data]; })}; }); console.log(this.analogHistoryData) this.analogFlag = true }
第二种方案是controller返回的时候自定义对象转json的格式:
package com.cetcht.common.json; import java.io.IOException; import com.cetcht.common.vo.echarts.TimeData; import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.databind.JsonSerializer; import com.fasterxml.jackson.databind.SerializerProvider; public class TimeDataSerializer extends JsonSerializer<TimeData> { public TimeDataSerializer() { } public void serialize(TimeData timeData, JsonGenerator jgen, SerializerProvider provider) throws IOException { jgen.writeStartArray(); jgen.writeNumber(timeData.getTime()); jgen.writeNumber(timeData.getData()); jgen.writeEndArray(); } }
package com.cetcht.common.vo.echarts; import java.math.BigDecimal; import java.util.Date; import com.cetcht.common.json.TimeDataSerializer; import com.fasterxml.jackson.databind.annotation.JsonSerialize; @JsonSerialize(using = TimeDataSerializer.class) public class TimeData { /** * 值 */ private BigDecimal data; /** * 记录入库的时间 */ private Date time; public BigDecimal getData() { return data; } public void setData(BigDecimal data) { this.data = data; } public long getTime() { return time.getTime(); } public void setTime(Date time) { this.time = time; } }