问题
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