SQL结果集转换器返回字符串数组

时间:2018-07-22 15:58:04

标签: spring hibernate

如何使用SqlResultSetMappingsResultTransformer返回json对象而不是json字符串数组。这是我在使用SqlResultSetMappingsResultTransformer

时获得的输出
[
    "Mel Bale ",
    "B.A.",
    "Interior Designer",
    "Daosa",
    "n/a"
]

我想要的是这样

{
   "name":"Mel Bale ",
   "qualification":"B.A.",
   "Job":"Interior Designer",
   "location":"Daosa",
   "phone":"n/a"
}

1 个答案:

答案 0 :(得分:0)

您需要像这样定义@SqlResultSetMappings,以便将数据映射为JSON

@SqlResultSetMapping(
    name = "findAllDataMapping",
    classes = @ConstructorResult(
            targetClass = Sample.class,
            columns = {
                    @ColumnResult(name = "name"),
                    @ColumnResult(name = "qualification"),
                    @ColumnResult(name = "Job"),
                    @ColumnResult(name = "location"),
                    @ColumnResult(name = "phone")
            }
    )
)