【发布时间】:2021-05-18 13:13:54
【问题描述】:
在使用 MyBatis 时,使用 SQL 结果中的列中的值填充数组的推荐方法是什么?
我有以下代码正在运行,但我需要将响应折叠到包含所有约会 ID 的单个对象中。
记录
@Serializable
data class SiteSchedule(
val id: String,
@Serializable(with = LocalDateSerializer::class) val date: LocalDate,
val appointmentId: String
)
映射器
@Mapper
interface SiteScheduleMapper {
@Select(
"""
select schedule.id, schedule.date, appointment.id
from schedule, appointment
where schedule.date = #{date}
and appointment.schedule_id = schedule.id
and appointment.status = 'S'
""")
fun findSchedule(
@Param("date") date: LocalDate
): Array<SiteSchedule>
}
SQL查询结果
| id | date | appointmentId |
|---|---|---|
| 1 | 2021-07-01 | 100 |
| 1 | 2021-07-01 | 200 |
| 1 | 2021-07-01 | 300 |
| 1 | 2021-07-01 | 400 |
当前 API 响应
[
{
"id": "1",
"dateTime": "2021-07-01",
"appointmentId": "100"
},
{
"id": "1",
"dateTime": "2021-07-01",
"appointmentId": "200"
},
{
"id": "1",
"dateTime": "2021-07-01",
"appointmentId": "300"
},
{
"id": "1",
"date": "2021-07-01",
"appointmentId": "400"
}
]
所需的 API 响应
[
{
"id": "1",
"date": "2021-07-01",
"appointmentIds": ["100","200","300","400"]
}
]
【问题讨论】:
-
使用
<collection>标签。见mybatis.org/mybatis-3/sqlmap-xml.html#Advanced_Result_Maps
标签: kotlin mybatis spring-mybatis