In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-13 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article is about how spring-data-jpa uses custom repository to implement native sql. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
Using custom repository to implement native sql
The Repository in Spring Data JPA is an interface, which is automatically generated by JPA for us based on the method name. But a lot of times, we need to provide some custom implementations for Repository. Today we'll look at how to add custom methods to Repository.
Custom Repository interface
First, let's add a custom interface:
Add BaseRepository Interfac
BaseRepository inherits JpaRepository, which ensures that all Repository have the basic methods provided by jpa.
Add the @ NoRepositoryBean tag to BaseRepository so that Spring Data Jpa does not de-instantiate the interface BaseRepository at startup
/ * Created by liangkun on 2016-12-7. * / @ NoRepositoryBeanpublic interface BaseRepository extends JpaRepository {/ / sql native query List listBySQL (String sql);}
Next, implement the BaseRepository interface and inherit the SimpleJpaRepository class to have the method implementation provided by JpaRepository.
/ * Created by liangkun on 2017-12-7. * / public class BaseRepositoryImpl extends SimpleJpaRepository implements BaseRepository {private final EntityManager entityManager; / / the parent class does not have a constructor without parameters. Here the parent class public BaseRepositoryImpl (Class domainClass, EntityManager entityManager) {super (domainClass, entityManager) {super (domainClass, entityManager) is constructed manually; this.entityManager = entityManager } / / complete the query @ Override public List listBySQL (String sql) {return entityManager.createNativeQuery (sql) .getResultList ();}} through EntityManager
Let's focus on EntityManager.
EntityManager is the interface for adding, deleting, modifying and querying in JPA. It acts as a bridge between the java object in memory and the data storage of the database. You can also carry out the native search of sql based on him.
The source code is as follows
Public interface EntityManager {T find (Class var1, Object var2); Query createNativeQuery (String var1); Query createNativeQuery (String var1, Class var2); Query createNativeQuery (String var1, String var2);}
From the above, we can see that it has a specific native query implementation interface createNativeQuery
Next, we need to add our custom Repository interface to the Spring container through the factory mode:
Create a custom RepositoryFactoryBean
Next let's create a custom RepositoryFactoryBean instead of the default RepositoryFactoryBean. RepositoryFactoryBean is responsible for returning a RepositoryFactory,Spring Data Jpa that will use RepositoryFactory to create the Repository concrete implementation.
Check the source code of JpaRepositoryFactoryBean and return the JpaRepositoryFactory instance through createRepositoryFactory:
Public class JpaRepositoryFactoryBean extends TransactionalRepositoryFactoryBeanSupport {private EntityManager entityManager; public JpaRepositoryFactoryBean (Class mappingContext) {super.setMappingContext (mappingContext);} @ Override protected RepositoryFactorySupport doCreateRepositoryFactory () {return createRepositoryFactory (entityManager);} protected RepositoryFactorySupport createRepositoryFactory (EntityManager entityManager) {return new JpaRepositoryFactory (entityManager);} @ Override public void afterPropertiesSet () {Assert.notNull (entityManager, "EntityManager must not be null!"); super.afterPropertiesSet ();}}
In the end, we can create a custom RepositoryFactoryBean according to the corresponding rules
/ * Created by liangkun on 2018-07-20. * / 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: 226
*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.