【问题标题】:Get response in JSON format from @Query annotation in Spring从 Spring 中的 @Query 注释获取 JSON 格式的响应
【发布时间】:2019-04-09 01:16:50
【问题描述】:

存储库

@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"}]

查询有多个表,列列表中还有子查询。

【问题讨论】:

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


    【解决方案1】:

    你需要一个构造函数表达式: 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) [...]"
    

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

    【讨论】:

      猜你喜欢
      • 2019-04-08
      • 2017-02-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-05
      • 2020-10-30
      • 1970-01-01
      • 2015-12-28
      相关资源
      最近更新 更多