日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

定制Spring Data JPA存储库

發(fā)布時間:2023/12/3 javascript 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 定制Spring Data JPA存储库 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
Spring Data是一個非常方便的庫。 但是,由于該項目是一個相當(dāng)新的項目,因此功能不佳。 默認(rèn)情況下,Spring Data JPA將基于SimpleJpaRepository提供DAO的實現(xiàn)。 在最近的項目中,我開發(fā)了一個定制的存儲庫基類,以便可以在其上添加更多功能。 您可以根據(jù)需要向該存儲庫基類添加特定于供應(yīng)商的功能。

組態(tài)

您必須在spring bean配置文件中添加以下配置。 您必須指定一個新的存儲庫工廠類。 我們將在以后開發(fā)課程。

<jpa:repositories base-package='example.borislam.dao' factory-class='example.borislam.data.springData.DefaultRepositoryFactoryBean/>

只需開發(fā)一個擴展JpaRepository的接口即可。 您應(yīng)該記得用@NoRepositoryBean對其進行注釋。

@NoRepositoryBean public interface GenericRepository <T, ID extends Serializable> extends JpaRepository<T, ID> { }

定義自定義存儲庫基礎(chǔ)實現(xiàn)類

下一步是開發(fā)定制的基礎(chǔ)存儲庫類。 您可以看到我只是這個自定義基礎(chǔ)存儲庫中的一個屬性(即springDataRepositoryInterface)。 我只想對存儲庫接口的自定義行為的行為進行更多控制。 在下一篇文章中,我將展示如何添加此基礎(chǔ)存儲庫類的更多功能。

@SuppressWarnings('unchecked') @NoRepositoryBean public class GenericRepositoryImpl<T, ID extends Serializable> extends SimpleJpaRepository<T, ID> implements GenericRepository<T, ID> , Serializable{private static final long serialVersionUID = 1L;static Logger logger = Logger.getLogger(GenericRepositoryImpl.class);private final JpaEntityInformation<T, ?> entityInformation;private final EntityManager em;private final DefaultPersistenceProvider provider;private Class<?> springDataRepositoryInterface; public Class<?> getSpringDataRepositoryInterface() {return springDataRepositoryInterface;}public void setSpringDataRepositoryInterface(Class<?> springDataRepositoryInterface) {this.springDataRepositoryInterface = springDataRepositoryInterface;}/*** Creates a new {@link SimpleJpaRepository} to manage objects of the given* {@link JpaEntityInformation}.* * @param entityInformation* @param entityManager*/public GenericRepositoryImpl (JpaEntityInformation<T, ?> entityInformation, EntityManager entityManager , Class<?> springDataRepositoryInterface) {super(entityInformation, entityManager);this.entityInformation = entityInformation;this.em = entityManager;this.provider = DefaultPersistenceProvider.fromEntityManager(entityManager);this.springDataRepositoryInterface = springDataRepositoryInterface;}/*** Creates a new {@link SimpleJpaRepository} to manage objects of the given* domain type.* * @param domainClass* @param em*/public GenericRepositoryImpl(Class<T> domainClass, EntityManager em) {this(JpaEntityInformationSupport.getMetadata(domainClass, em), em, null); }public <S extends T> S save(S entity){ if (this.entityInformation.isNew(entity)) {this.em.persist(entity);flush();return entity;}entity = this.em.merge(entity);flush();return entity;}public T saveWithoutFlush(T entity){return super.save(entity);}public List<T> saveWithoutFlush(Iterable<? extends T> entities){List<T> result = new ArrayList<T>();if (entities == null) {return result;}for (T entity : entities) {result.add(saveWithoutFlush(entity));}return result;} }

作為一個簡單的示例,我只是覆蓋了SimpleJPARepository的默認(rèn)保存方法。 持久保存后,save方法的默認(rèn)行為不會刷新。 我進行了修改,使其在持久化后保持沖洗狀態(tài)。 另一方面,我添加了另一個名為saveWithoutFlush()的方法,以允許開發(fā)人員調(diào)用保存實體而無需刷新。

定義自定義存儲庫工廠bean

最后一步是創(chuàng)建一個工廠bean類和一個工廠類,以根據(jù)您自定義的基礎(chǔ)存儲庫類來生成存儲庫。

public class DefaultRepositoryFactoryBean <T extends JpaRepository<S, ID>, S, ID extends Serializable>extends JpaRepositoryFactoryBean<T, S, ID> {/*** Returns a {@link RepositoryFactorySupport}.* * @param entityManager* @return*/protected RepositoryFactorySupport createRepositoryFactory(EntityManager entityManager) {return new DefaultRepositoryFactory(entityManager);} }/*** * The purpose of this class is to override the default behaviour of the spring JpaRepositoryFactory class.* It will produce a GenericRepositoryImpl object instead of SimpleJpaRepository. * */ public class DefaultRepositoryFactory extends JpaRepositoryFactory{private final EntityManager entityManager;private final QueryExtractor extractor;public DefaultRepositoryFactory(EntityManager entityManager) {super(entityManager);Assert.notNull(entityManager);this.entityManager = entityManager;this.extractor = DefaultPersistenceProvider.fromEntityManager(entityManager);}@SuppressWarnings({ 'unchecked', 'rawtypes' })protected <T, ID extends Serializable> JpaRepository<?, ?> getTargetRepository(RepositoryMetadata metadata, EntityManager entityManager) {Class<?> repositoryInterface = metadata.getRepositoryInterface();JpaEntityInformation<?, Serializable> entityInformation =getEntityInformation(metadata.getDomainType());if (isQueryDslExecutor(repositoryInterface)) {return new QueryDslJpaRepository(entityInformation, entityManager);} else {return new GenericRepositoryImpl(entityInformation, entityManager, repositoryInterface); //custom implementation}}@Overrideprotected Class<?> getRepositoryBaseClass(RepositoryMetadata metadata) {if (isQueryDslExecutor(metadata.getRepositoryInterface())) {return QueryDslJpaRepository.class;} else {return GenericRepositoryImpl.class;}}/*** Returns whether the given repository interface requires a QueryDsl* specific implementation to be chosen.* * @param repositoryInterface* @return*/private boolean isQueryDslExecutor(Class<?> repositoryInterface) {return QUERY_DSL_PRESENT&& QueryDslPredicateExecutor.class.isAssignableFrom(repositoryInterface);} }

結(jié)論

現(xiàn)在,您可以向基礎(chǔ)存儲庫類添加更多功能。 在您的程序中,您現(xiàn)在可以創(chuàng)建自己的存儲庫接口,擴展GenericRepository而不是JpaRepository。

public interface MyRepository <T, ID extends Serializable>extends GenericRepository <T, ID> {void someCustomMethod(ID id); }

在下一篇文章中,我將向您展示如何向此GenericRepository添加Hibernate過濾器功能。

參考:來自我們的JCG合作伙伴 Boris Lam在Programming Peacely博客上定制Spring Data JPA存儲庫 。


翻譯自: https://www.javacodegeeks.com/2012/08/customizing-spring-data-jpa-repository.html

總結(jié)

以上是生活随笔為你收集整理的定制Spring Data JPA存储库的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。