Spring data fetch only without mapping to database

血红的双手。 提交于 2019-12-11 16:02:24

问题


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.


回答1:


You can use projections.

In your case, for example:

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();

Pay attention on aliases in the query - they must correspond to projection method names (i.e. total -> getTotal()...).



来源:https://stackoverflow.com/questions/50103260/spring-data-fetch-only-without-mapping-to-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!