如果想把一个方法加到所有的repository中,用前一篇提到的方法就不合适了。

英文原版,请看

http://docs.spring.io/spring-data/data-mongo/docs/1.5.2.RELEASE/reference/html/repositories.html#repositories.custom-behaviour-for-all-repositories

1. 定义自己的repository,要从基础的repository进行继承。

  1. public interface MyRepository<T, ID extends Serializable> extends JpaRepository<T,ID> {
  2. void sharedCustomMethod(ID id);
  3. }

2. 定义repository的实现,也要从基础的repository实现进行继承。

  1. public class MyRepositoryImpl<T,ID extends Serializable> extends SimpleJpaRepository<T,ID> Implements MyRepository<T,ID> {
  2. private EntityManager entityManager;
  3.  
  4. public MyRepositoryImpl(Class<T> domainClass, EntityManager entityManager){
  5. super(domainClass,entityManager);
  6.  
  7. this.entityManager = entityManager;
  8. }
  9.  
  10. public void sharedCustomMethod(ID id){
  11. //implementation goes here
  12. }
  13. }

3. 创建一个repository工厂

  1. public class MyRepositoryFactoryBean<R extends JpaRepository<T, I>, T, I extends Serializable>
  2. extends JpaRepositoryFactoryBean<R, T, I> {
  3.  
  4. protected RepositoryFactorySupport createRepositoryFactory(EntityManager entityManager) {
  5.  
  6. return new MyRepositoryFactory(entityManager);
  7. }
  8.  
  9. private static class MyRepositoryFactory<T, I extends Serializable> extends JpaRepositoryFactory {
  10.  
  11. private EntityManager entityManager;
  12.  
  13. public MyRepositoryFactory(EntityManager entityManager) {
  14. super(entityManager);
  15.  
  16. this.entityManager = entityManager;
  17. }
  18.  
  19. protected Object getTargetRepository(RepositoryMetadata metadata) {
  20.  
  21. return new MyRepositoryImpl<T, I>((Class<T>) metadata.getDomainClass(), entityManager);
  22. }
  23.  
  24. protected Class<?> getRepositoryBaseClass(RepositoryMetadata metadata) {
  25.  
  26. // The RepositoryMetadata can be safely ignored, it is used by the JpaRepositoryFactory
  27. //to check for QueryDslJpaRepository's which is out of scope.
  28. return MyRepository.class;
  29. }
  30. }
  31. }

4. 使用新创建的factory

  1. <repositories base-package="com.acme.repository" factory-class="com.acme.MyRepositoryFactoryBean"/>

[Spring Data Repositories]学习笔记--为repository添加通用的方法的更多相关文章

  1. [Spring Data Repositories]学习笔记--使用现有的repository

    以下内容是在学习Spring-Data-mongoDB中的Spring Data Repositories时做的一些笔记.备忘! 感觉学习还是看官方的资料比较透彻一些. Spring Data Rep ...

  2. [Spring Data Repositories]学习笔记--定义自己的repository

    有时,我们会需要用到自己定义的一些查询方法,可以按照下面几步进行. 1. 定义一个包含该方法的接口 Interface UserRepositoryCustom { public void someC ...

  3. 031 Spring Data Elasticsearch学习笔记---重点掌握第5节高级查询和第6节聚合部分

    Elasticsearch提供的Java客户端有一些不太方便的地方: 很多地方需要拼接Json字符串,在java中拼接字符串有多恐怖你应该懂的 需要自己把对象序列化为json存储 查询到结果也需要自己 ...

  4. Spring data jpa 实现简单动态查询的通用Specification方法

    本篇前提: SpringBoot中使用Spring Data Jpa 实现简单的动态查询的两种方法 这篇文章中的第二种方法 实现Specification 这块的方法 只适用于一个对象针对某一个固定字 ...

  5. spring data jpa 学习笔记

    springboot 集成 springData Jpa 1.在pom.xml添加依赖 <!-- SpringData-Jpa依赖--> <dependency <groupI ...

  6. [Spring Data MongoDB]学习笔记--MongoTemplate查询操作

    查询操作主要用到两个类:Query, Criteria 所有的find方法都需要一个query的object. 1. 直接通过json来查找,不过这种方式在代码中是不推荐的. BasicQuery q ...

  7. [Spring Data MongoDB]学习笔记--MongoTemplate插入修改操作

    插入操作: 直接给个例子 import static org.springframework.data.mongodb.core.query.Criteria.where; import static ...

  8. [Spring Data MongoDB]学习笔记--_id和类型映射

    _id字段的映射: MongoDB要求所有的document都要有一个_id的字段. 如果我们在使用中没有传入_id字段,它会自己创建一个ObjectId. { , "accounts&qu ...

  9. [Spring Data MongoDB]学习笔记--牛逼的MongoTemplate

    MongoTemplate是数据库和代码之间的接口,对数据库的操作都在它里面. 注:MongoTemplate是线程安全的. MongoTemplate实现了interface MongoOperat ...

随机推荐

  1. [Javascript] Conditionally spread entries to a JavaScript object

    In JavaScript, we often end up composing one object out of several other objects. Luckily there's a ...

  2. LeetCode题目:Spiral Matrix II

    原题地址:https://leetcode.com/problems/spiral-matrix-ii/ class Solution { public: vector<vector<in ...

  3. Data truncation: Data too long for column

    是字符集问题引起的,用show full fields from + 表名就可以看出你的列的编码格式把它改成GBK或者GB2312.uTF-8.如果还不行的话,把你表的编码格式也改成上面的编码格式,我 ...

  4. js标准化价钱

    //标准化总价钱 s:总价钱,n:保留几位小数 function fmoney(s, n) { n = n > 0 && n <= 20 ? n : 2; s = pars ...

  5. TIME_WAIT状态及存在原因

    1. 客户端与服务器端建立TCP/IP连接后关闭SOCKET后,服务器端连接的端口状态为TIME_WAIT: 2. 主动关闭的Socket端会进入TIME_WAIT状态,并且持续2MSL时间长度,MS ...

  6. unity, UGUI Image shader

      Image组件的Material成员默认是空,如果想为Image添加shader,只需新建material赋给Material即可. 另外注意,用于UI组件的shader都要包含一句:ZTest  ...

  7. Redis之ziplist数据结构

    0.前言 redis初始创建hash表,有序集合,链表时, 存储结构采用一种ziplist的存储结构, 这种结构内存排列更紧密, 能提高访存性能. 本文介绍ziplist数据结构 1.ziplist存 ...

  8. Codeforces Round #240 (Div. 1)B---Mashmokh and ACM(水dp)

    Mashmokh's boss, Bimokh, didn't like Mashmokh. So he fired him. Mashmokh decided to go to university ...

  9. [ci]gitlab安装配置(含gitlab邮件配置)

    gitlab安装配置 参考: https://www.unixhot.com/article/48 原则:简单维护为准,故yum安装gitlab 1,gitlab安装 2,gitlab邮箱配置 1,g ...

  10. 159. Find Minimum in Rotated Sorted Array 【medium】

    159. Find Minimum in Rotated Sorted Array [medium] Suppose a sorted array is rotated at some pivot u ...