我无法从JSON_ARRAYAGG function
.
mybatis 映射器:
<mapper namespace="com.test.mapper.UserEntityMapper">
<resultMap id="userMap" type="UserVO">
<id property="id" column="id" />
<result property="username" column="username"/>
<collection property="pictures" ofType="PictureVO">
<id property="id" column="id" />
<result property="location" column="location" />
</collection>
</resultMap>
<select id="findUserById" parameterType="map" resultMap="userMap">
SELECT
users.id,
users.username,
JSON_ARRAYAGG(
JSON_OBJECT(
"id",
pic.id,
"location",
pic.location
)
) AS pictures
FROM
users
LEFT JOIN pictures pic ON users.id = pic.user_id
WHERE
users.id = 1
</select>
</mapper>
UserVO 型号:
@Getter
@Setter
@NoArgsConstructor
public class UserVO {
private Long id;
private String username;
private List<PictureVO> pictures;
}
图片VO型号:
@Getter
@Setter
@NoArgsConstructor
public class PictureVO {
private Long id;
private String location;
}
调用 API 的结果:
{
"id": 1,
"username": "connor",
"pictures": [
{
"id": 1,
"location": null
}
]
}
如您所见,pictures
只有一个id
可以正确映射。假设有2个结果pictures
,如果我直接执行sql,这里只返回1个。