仅在不映射到数据库的情况下获取Spring数据

时间:2018-04-30 15:06:39

标签: hibernate spring-boot spring-data spring-data-jpa hibernate-mapping

enter image description here

enter image description here

如查询和结果所示,我想在列表中获取并添加结果,但是如何在Spring数据中执行此操作。如何将结果保存在列表中?

我应该创建一个新的实体类吗?请记住,我绝对不需要将我的模型类映射到数据库。我很简单想要获取并使用控制器中的列表。

感谢任何可以提供帮助的人。

1 个答案:

答案 0 :(得分:2)

您可以使用projections

在您的情况下,例如:

public interface TotalPerMonth {
    String getMonth();
    Long getTotal();
}

然后在查询方法中使用它:

@Query(value="select date_format(...) as month, sum(...) as total from ...", nativeQuery = true)
List<TotalPerMonth> curentYearSales();

注意查询中的别名 - 它们必须与投影方法名称相对应(即total - &gt; getTotal() ...)。