我有 2 个这样的 java 模型
public class DeviceData {
private String name;
private List<Metric> up;
private List<Metric> down;
}
public class Metric{
private Long data;
private LocalDateTime timeStamp;
}
实际查询从具有这样列名的 db 中按名称获取结果..
-------------------------------------------------------------
data | timestamp | name | trafficType
-------------------------------------------------------------
10 | 2020-10-08 13:10:00 | test1 | down
20 | 2020-10-08 12:15:00 | test1 | up
30 | 2020-10-08 13:10:00 | test2 | down
40 | 2020-10-08 12:15:00 | test2 | up
不,我一直在尝试使mybatis xml根据trafficType列将上述查询结果分为两组,将划分的集合分别复制到DeviceData.java类的up列表,down列表中。我尝试使用鉴别器,但似乎没有用,因为我需要这两种情况。
到目前为止 resultMap 是这样的..(基本)
<resultMap id="trafficData" type="DeviceData" autoMapping="true">
<id property="name" column="name" jdbcType="VARCHAR"/>
<collection property="upload" ofType="Metric" autoMapping="true">
</collection>
</resultMap>
试图实现这个 JSON 响应:
[
{
"name": "test1",
"down": [
{
"data": 10,
"timeStamp": "2020-10-08 13:10:00"
}
],
"up": [
{
"data": 20,
"timeStamp": "2020-10-08 12:15:00"
}
]
},
{
"name": "test2,
"down": [
{
"data": 30,
"timeStamp": "2020-10-08 13:10:00"
}
],
"up": [
{
"data": 40,
"timeStamp": "2020-10-08 12:15:00"
}
]
}
]
感谢任何想法/语法,谢谢!