Spring data fetch only without mapping to database

2019-08-23 03:42发布

enter image description here

enter image description here

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条回答
Summer. ? 凉城
2楼-- · 2019-08-23 04:39

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

查看更多
登录 后发表回答