Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to customize the return object Projection for SpringJPA repository nativeQuery

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

SpringJPA repository nativeQuery how to customize the return object Projection, for this question, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and easy way.

A set of CRUD operations encapsulated by Spring JPA greatly simplifies the development process and improves the development efficiency. But inevitably there will be some special queries, need to write their own sql, return custom fields, need to use custom objects to receive, is there any good way?

Through the online search of most other people's solutions, I found the corresponding document on the official website: https://www.baeldung.com/spring-data-jpa-projections

To put it simply, it is a custom method in repository. When you need to receive it with a custom object, you need to ask the interface type of the receiving object, and the get method of the required receiving field is defined. For example, if you need to receive a name field, you need to define String getName ().

@ Repositorypublic interface ItemRepository extends JpaRepository {/ * query user's total score in time period * / @ Query (nativeQuery = true, value = "select user_info_id userId, sum (score_val) score FROM tb_item where is_del = 0 and `status` = 1 and user_info_id in (: userIds) and end_date BETWEEN: startDate and: endDate GROUP BY user_info_id") List sumUserScore (@ Param ("userIds") List userIds, @ Param ("startDate") Date startDate @ Param ("endDate") Date endDate) } / / Custom receiving object / / ItemProjection:public interface ItemProjection {Integer getUserId (); BigDecimal getScore ();}

It is very clear that this is the most commonly used and relatively simple solution on the Internet.

Of course, there is also a solution of my own (at least I didn't find it on the Internet)

Is a custom converter, you can use whatever you want to receive! Because this solution is just born under the complaint of "lazy" programmers who don't even want to build an interface, I won't talk about it here.

This is the answer to the question about how SpringJPA repository nativeQuery customizes the returned object Projection. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report