In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "how to add custom methods for Repository". In daily operation, I believe many people have doubts about how to add custom methods for Repository. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the questions of "how to add custom methods for Repository". Next, please follow the editor to study!
First, add a custom method for a Repository
1. Define an interface PersonDao and declare the method to be added.
Public interface PersonDao {public List getlist ();}
2. Provide the implementation class PersonRepositoryImpl of the interface. The class name needs to add the Impl after the Repository to be declared and implement the method declared in the PersonDao.
Repositorypublic class PersonRepositoryImpl implements PersonDao {@ PersistenceContext private EntityManager em; @ Override public List getlist () {Query query = em.createQuery ("from Person"); return query.getResultList ();}}
3. Use Repository interface and inherit PersonDao interface.
Public interface PersonRepository extends JpaRepository, JpaSpecificationExecutor, PersonDao {}
4. SpringData will automatically load the implementation class of PersonRepositoryImpl at this time.
@ Testpublic void testList () {List list = service.getList (); for (Person p: list) {System.out.println (p);}}
Note: the names of XXXRepositoryImpl and XXXRepository must be the same before, and the latter must be written according to the rules. If you put the XXXRepositoryImpl and XXXRepository interfaces under the consent package, XXXRepositoryImpl does not need to add @ Repository annotation, but when the XXXRepositoryImpl and XXXRepository interfaces are not in the same package, you need to add Repository annotations to the XXXRepositoryImpl class to modify them.
2. Add global Repository
1. Declare an interface in which the method that needs to be customized is declared, and the interface needs to inherit Spring Data's Repository.
@ NoRepositoryBeanpublic interface BaseRepository extends JpaRepository {public void helloworld ();}
Note: global extension implementation classes should not use Imp as the suffix, or add @ NoRepositoryBean annotation to the global extension interface to tell Spring Data: Spring Data does not use it as Repository
2. Provide the implementation class of the interface declared by BaseRepository. And inherit SimpleJpaRepository, and provide the implementation of the method.
Public class BaseRepositoryImpl extends SimpleJpaRepository implements BaseRepository {private EntityManager em; public BaseRepositoryImpl (Class domainClass, EntityManager em) {super (domainClass, em); this.em = em;} @ Override public void helloworld () {System.out.println ("helloworld");}}
3. Define the implementation class of JpaRepositoryFactoryBean and make it generate the objects of the interface implementation class defined by BaseRepository.
Public class BaseRepositoryFactoryBean extends JpaRepositoryFactoryBean {public BaseRepositoryFactoryBean (Class
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.