仅在不映射到数据库的情况下获取Spring数据 [英] Spring data fetch only without mapping to database
问题描述
如查询和结果中所示,我想获取结果并将其添加到列表中,但是如何在Spring数据中执行该操作.如何将结果保存在列表中?
As shown in query and result, I want to fetch and add the result in list but how do I do it in Spring data. How to hold the result in a list?
我应该创建一个新的实体类吗?请记住,我绝对不需要将模型类映射到数据库.我只是想获取并使用控制器中的列表.
Should I create a new entity class? Keeping in mind that I absolutely do not need to map my model class to database. I simple want to fetch and use the list in controller.
感谢任何可以提供帮助的人.
Thanks to anyone who can help.
推荐答案
您可以使用投影.
例如,您的情况:
public interface TotalPerMonth {
String getMonth();
Long getTotal();
}
然后在查询方法中使用它:
Then use it in your query method:
@Query(value="select date_format(...) as month, sum(...) as total from ...", nativeQuery = true)
List<TotalPerMonth> curentYearSales();
请注意查询中的别名-它们必须与投影方法名称相对应(即total
-> getTotal()
...).
Pay attention on aliases in the query - they must correspond to projection method names (i.e. total
-> getTotal()
...).
这篇关于仅在不映射到数据库的情况下获取Spring数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!