In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail how to implement custom methods in Spring Data Jpa. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.
Implementation of Spring Data Jpa user-defined method
Recently, Spring Data JPA was used in the project, in which I inherited an interface of PagingAndSortingRepository, hoping to take advantage of the convenience provided by Spring Data JPA.
At the same time, I also want to have an interface that can define my own methods, because there are still a lot of business logic that can't be implemented by relying solely on the functions provided in Spring Data JPA, and I have to implement it myself.
So here comes the problem: the advantage of Spring Data JPA is that it saves us the process of implementing the interface, and they will automatically implement our business logic according to the naming convention they give us, so how can our own implemented interfaces be injected into it?
I looked up a lot of information on the Internet, but I didn't say it in too much detail, but I copied it blindly. I wrote it myself. Maybe many people will use it and don't say much about the code:
Own interface package com.mhc.dao; import org.springframework.stereotype.Repository; import com.mhc.entity.Person; @ Repositorypublic interface DeviceCategoryDaoCustom {public Person getsFather (Person person);} primary interface public interface DeviceCategoryDao extends PagingAndSortingRepository, DeviceCategoryDaoCustom {}
Above is my interface inheriting PagingAndSortingRepository, DeviceCategoryDaoCustom (the interface of my own method).
I create a new class to implement my own interface package com.mhc.dao; import javax.persistence.PersistenceContext;import javax.transaction.Transactional; import org.springframework.beans.factory.annotation.Autowired;import org.springframework.data.repository.CrudRepository;import org.springframework.data.repository.NoRepositoryBean;import org.springframework.stereotype.Component;import org.springframework.stereotype.Repository;import org.springframework.stereotype.Service; import com.mhc.entity.Person @ Repository ("crudRepositoryDaoCustom") class DeviceCategoryDaoImpl implements DeviceCategoryDaoCustom {@ Transactional public Person getsFather (Person person) {/ / TODO Auto-generated method stub Person father = new Person (); father = person.getParentPerson (); return father;}}
One thing to pay attention to here is whether to use implements or not. if so, he will call the compiler's implementation function to implement our custom interface: DevicecategoryCustom.
If removed, he will implement DeviceCategoryDao, then someone will ask, how did he find it himself?
As a matter of fact, he looked for it based on the following Impl. He won't prompt @ override, but if you write the same method, he will still overwrite (override the method of the same name in the main interface, if any) in the DeviceCategoryDao. You can give it a try.
At the same time, add @ Repository to add him to Bean so that Repository will find him automatically the next time you use this method (in other words, the Spring team really NB). Then we give it to spring for hosting and testing. Ok really likes it.
Spring Data Jpa custom method keyword keyword method name example corresponding SQLAndfindByNameAndAgewhere name =? And age =? OrfindByNameOrAgewhere name =? Or age = IsfindByNameIswhere name =? EqualsfindByNameEqualswhere name =? BetweenfindByAgeBetweenwhere age between? And? LessThanfindByAgeLessThanwhere age
< ?LessThanEqualsfindByAgeLessThanEqualwhere age ?GreatorThanEqualsfindByAgeGreaterThanEqualwhere age >=? AfterfindByAgeAfterwhere age >? BeforefindByAgeBeforewhere age IsNullfindByNameIsNullwhere name is nullIsNotNull,NotNullfindByNameIsNotNull,findByNameNotNullwhere name is not nullNotfindByNameNotwhere name? InfindByAgeInwhere age in (?) NotInfindByAgeNotInwhere age not in NotLikefindByNameNotLikewhere name not like? LikefindByNameLikewhere name like? StartingWithfindByNameStartingWithwhere name like'?% 'EndingWithfindByNameEndingWithwhere name like'%? 'Containing,ContainsfindByNameContaining,findByNameContainswhere name like'%?% 'OrderByfindByOrderByAgeDescorder by age descTruefindByBossTruewhere boss = trueFalsefindByBossFalsewhere boss = falseIgnoreCasefindByNameIgnoreCasewhere UPPER (name) = UPPER (?) This is the end of this article on "how to implement custom methods in Spring Data Jpa". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.
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.