在Spring中从@Query注释获取JSON格式的响应

时间:2018-11-05 16:33:35

标签: json spring-boot spring-data-jpa dto jsonresult

存储库

@Repository
public interface TestNativeQRepository extends CrudRepository<TestNativeQ, String> {
    @Query( value="SELECT qplt.name price_list_name,  qplab.status_code, qplab.start_date, (SELECT charge_definition_code FROM oalfsaas_repl.QP_CHARGE_DEFINITIONS_B WHERE charge_definition_id=qplab.charge_definition_id  ) chargedefinitioncode "
            + "FROM  pricelistsall qplab, PRICELISTSTL qplt "
            + " WHERE qplab.price_list_id  =qplt.price_list_id ", nativeQuery = false)
    public List<TestNativeQDTO> getAllDetails();
}

实际结果:

[{"ABC", "DEF", "15/05/2018", "XXZ"}]

预期结果

[{name: "ABC", statuscode: "DEF", startDate: "15/05/2018", chargedefintioncode: "XXZ"}]

查询具有多个表,并且在列列表中还有子查询。

1 个答案:

答案 0 :(得分:0)

您将需要一个构造函数表达式: https://javaee.github.io/tutorial/persistence-querylanguage006.html#BNBWC

例如:

SELECT NEW mypackage.TestNativeQDTO(qplt.name price_list_name,  qplab.status_code, qplab.start_date, (SELECT charge_definition_code FROM oalfsaas_repl.QP_CHARGE_DEFINITIONS_B WHERE charge_definition_id=qplab.charge_definition_id  ) chargedefinitioncode) [...]"

不过,我不确定它是否可用于子查询。