In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the knowledge of "how to use JPA to customize SQL query results". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
JPA Custom SQL query results
Many times you will encounter custom sql, custom return fields, rather than pojo classes. This situation is returned through the interface definition.
Directly add the code @ Query (value = "select m.field AS field,COUNT (m.field) AS size from MigrationObject m where m.xmlName =? 1 and m.groupName =? 2 group by m.field") List getKey (String xmlName, String groupName)
In this case, only two fields are returned, and you need to define an interface to receive (note the configuration of the AS alias)
Public interface WorkCenter {String getField (); String getSize ();} finally run the demo code List list = migrationObjectRepository.getKey ("EN_Work centerResource.xml", "Key"); for (WorkCenter workCenter:list) {System.out.println (workCenter.getField ()); System.out.println (workCenter.getSize ());}
ARBPL
five
SPRAS
two
CANUM
two
ENDDA
one
WERKS
five
JPA's SQL query is the finishing touch.
There are generally two ways for JAP to query through SQL: through NamedQuery and using @ Query query.
2. NamedQuery query of JPA
1 description
Spring Data JPA supports the use of JPA's NameQuery to define query methods, that is, a name maps a query statement.
2 definition
Entity @ NamedQuery (name= "Person.withNameAndAddressNamedQuery", query = "select p from Person p where p.name=?1 and address=?2") public class Person {.}
3 method of use
Public interface PersonRepo extends JpaRepository {/ / uses the query statement defined in NameQuery instead of querying Person withNameAndAddressNamedQuery (String name,String address) based on the method name;} three uses @ Query query
1 use parameter index
Public interface PersonRepo extends JpaRepository {@ Query ("select p from Person p where p.address1") List findByAddress (String address);}
2 use named parameters
Public interface PersonRepo extends JpaRepository {@ Query ("select p from Person p where p.namee =: name and p.address=: address") Person withNameAndAddressQuery (@ Param ("name") String name,@Param ("address") String address);}
3 update query
Public interface PersonRepo extends JpaRepository {@ Modifying @ Transactional @ Query ("update Person p set p.name=?1") int setName (String name); / / indicates the number of rows affected by the update statement} "how to use JPA to customize SQL query results". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.