spring?data?jpa如何使用自定義repository實(shí)現(xiàn)類
spring data jpa使用自定義repository實(shí)現(xiàn)類
spring data jpa中使用JpaRepository等接口定義repository時,將默認(rèn)使用SimpleJpaRepository
可通過自定義實(shí)現(xiàn)類,改寫或自定義接口方法邏輯:
創(chuàng)建MyJpaRepository實(shí)現(xiàn)類
@Repository @Transactional(readOnly = true) public class MyRepositoryImpl<T,ID> extends SimpleJpaRepository<T, ID> { public MyRepositoryImpl(JpaEntityInformation<T, ID> entityInformation, EntityManager entityManager) { super(entityInformation, entityManager); } public MyRepositoryImpl(Class<T> domainClass, EntityManager em) { super(domainClass, em); } ... }
創(chuàng)建MyJpaRepositoryFactoryBean
public class MyJpaRepositoryFactoryBean<R extends Repository<T, ID>, T, ID> extends JpaRepositoryFactoryBean<R, T, ID> { public MyJpaRepositoryFactoryBean(Class<? extends R> repositoryInterface) { super(repositoryInterface); } @SuppressWarnings("rawtypes") protected RepositoryFactorySupport createRepositoryFactory(EntityManager em) { return new MyJpaRepositoryFactory(em); } private static class MyJpaRepositoryFactory<T, ID> extends JpaRepositoryFactory { private final EntityManager em; public MyJpaRepositoryFactory(EntityManager em) { super(em); this.em = em; } @Override protected JpaRepositoryImplementation<?, ?> getTargetRepository(RepositoryInformation information, EntityManager entityManager) { return new EntityJPARepositoryImpl<E>( (Class<E>) information.getDomainType(), entityManager); } protected Class<?> getRepositoryBaseClass(RepositoryMetadata metadata) { return MyRepositoryImpl.class; } } }
配置JPA
@Configuration @EnableJpaRepositories(basePackages = {...}, repositoryFactoryBeanClass = MyJpaRepositoryFactoryBean.class) public class JPAConfig {}
Jpa自定義Repository方法
如果不使用SpringData的方法,想要自己實(shí)現(xiàn),該怎么辦呢?
定義一個接口: 聲明要添加的, 并自實(shí)現(xiàn)的方法
提供該接口的實(shí)現(xiàn)類: 類名需在要聲明的 Repository 后添加 Impl, 并實(shí)現(xiàn)方法
聲明 Repository 接口, 并繼承 1) 聲明的接口
注意: 默認(rèn)情況下, Spring Data 會在 base-package 中查找 "接口名Impl" 作為實(shí)現(xiàn)類. 也可以通過 repository-impl-postfix 聲明后綴.
這張圖是類與接口之間的關(guān)系
下面是具體的實(shí)現(xiàn):
包結(jié)構(gòu)
類與接口之間的關(guān)系代碼
public interface PersonRepositoiry extends JpaRepository<Person, Integer> ,PersonDao{ public interface PersonDao { void test(); } @Repository public class PersonRepositoiryImpl implements PersonDao{ @PersistenceContext private EntityManager em; @Override public void test() { //只是用來測試 Person person = em.find(Person.class, 1); System.out.println(person); } }
測試代碼
@Test public void testCustomerRepositoryMethod() { personRepositoiry.test(); }
經(jīng)過實(shí)踐發(fā)現(xiàn)
- XXXRepositoryImpl 與XXXRepository前面的名字必須相同,后面的也需要按照規(guī)則寫
- 若將XXXRepositoryImpl與XXXRepository接口放在同意包下,XXXRepositoryImpl不需要添加@Repository注解,但是當(dāng)XXXRepositoryImpl與XXXRepository接口不在同一包下,需要在在XXXRepositoryImpl類上加@Repository注解進(jìn)行修飾
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
解決maven update project 后項(xiàng)目jdk變成1.5的問題
下面小編就為大家?guī)硪黄鉀Qmaven update project 后項(xiàng)目jdk變成1.5的問題。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起 小編過來看看吧2016-11-11SpringBoot使用prometheus監(jiān)控的示例代碼
這篇文章主要介紹了SpringBoot使用prometheus監(jiān)控的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03MybatisPlus分頁查詢與多條件查詢介紹及查詢過程中空值問題的解決
mybatisplus是個很好用的插件,相信小伙伴們都知道,下面這篇文章主要給大家介紹了關(guān)于mybatis-plus實(shí)現(xiàn)分頁查詢與多條件查詢介紹及查詢過程中空值問題的相關(guān)資料,需要的朋友可以參考下2022-10-10Mybatis分頁插件PageHelper的配置和簡單使用方法(推薦)
在使用Java Spring開發(fā)的時候,Mybatis算是對數(shù)據(jù)庫操作的利器了。這篇文章主要介紹了Mybatis分頁插件PageHelper的配置和使用方法,需要的朋友可以參考下2017-12-12詳解hashCode()和equals()的本質(zhì)區(qū)別和聯(lián)系
這篇文章主要介紹了詳解hashCode()和equals()的本質(zhì)區(qū)別和聯(lián)系,本文先對兩種方法作了介紹,然后對二者聯(lián)系進(jìn)行分析,具有一定參考價值,需要的朋友可以了解下。2017-09-09