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

Analysis of trampling pit based on spring data jpa@query returning map

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces "trampling analysis based on spring data jpa@query returning map". In daily operation, I believe that many people have doubts about trampling analysis based on spring data jpa@query returning map. Xiaobian consulted all kinds of data and sorted out simple and easy-to-use operation methods, hoping to help you answer the doubts of "trampling analysis based on spring data jpa@query returning map". Next, please follow the editor to study!

Spring data jpa @ query returns map trample record

Recently, I used spring data jpa to read some tutorials on the Internet, some said it was a returned Object [] array, and some said it was a direct return object. I personally verified it and found that it mainly looked at the writing method and returned results.

The version used is as follows:

The dao code is as follows:

Public interface UserRepository extends JpaRepository {/ * query some specific columns and use the Object [] array to receive * @ param realName * @ return * / @ Query (value = "select u.userID from User u where u.realName u.realName from User u where u.realName =: realName") public Object [] getUserByRealName2 (@ Param ("realName") String realName) / * query all the information of the whole entity object. You can receive * @ param realName * @ return * / @ Query (value = "select u from User u where u.realName =: realName") public User getUserByRealName3 (@ Param ("realName") String realName) directly with the entity object. / * return List information * @ param realName * @ return * / @ Query (value = "select new map (u as user) from User u where u.realName =: realName") public List getUserByRealName (@ Param ("realName") String realName) / * return map object * @ param realName * @ return * / @ Query (value = "select new map (u.realName as userName,o.orgName as orgName) from User u, Org o where u.realName =: realName") public Map getUserByRealName1 (@ Param ("realName") String realName);}

The code for the test is as follows, and there is no problem at all.

@ RunWith (SpringJUnit4ClassRunner.class) @ ContextConfiguration (locations = {"classpath:conf/root-context.xml"}) public class UserDaoTest {@ Autowired private UserRepository userRepository; @ Test public void test () {Object [] U2 = userRepository.getUserByRealName2 ("admin"); User U3 = userRepository.getUserByRealName3 ("admin") List u = userRepository.getUserByRealName ("test"); String userName = ((User) u.get (0). Get ("user")) .getRealName (); System.out.println (userName); Map U1 = userRepository.getUserByRealName1 ("admin") String name = ((User) u1.get ("userName")) .getRealName (); System.out.println (name); assert (userName.equals ("admin"));} set the Query of JPA to return the Map object

When executing a jpa query normally, you need to pass the data returned by a corresponding entity for mapping, so that sometimes if a sql is a composite sql associated with many tables, it is troublesome to create a new entity. You can map the returned results to map in the following ways.

In this way, the content of the returned result can be obtained at will.

Query query = this.entityManager.createNativeQuery (sql); / / sets the Query of JPA to return the Map object query.unwrap (SQLQuery.class) .setResultTransformer (Transformers.ALIAS_TO_ENTITY_MAP) .setParameter ("createuser", userInfo.getUsercode ()) .setParameter ("type", type); List res = query.getResultList (); at this point, the study on "trampling analysis based on spring data jpa@query returning map" is over, hoping to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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

Development

Wechat

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

12
Report