如果想把一个方法加到所有的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进行继承。

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

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

public class MyRepositoryImpl<T,ID extends Serializable> extends SimpleJpaRepository<T,ID> Implements MyRepository<T,ID> {
private EntityManager entityManager; public MyRepositoryImpl(Class<T> domainClass, EntityManager entityManager){
super(domainClass,entityManager); this.entityManager = entityManager;
} public void sharedCustomMethod(ID id){
//implementation goes here
}
}

3. 创建一个repository工厂

public class MyRepositoryFactoryBean<R extends JpaRepository<T, I>, T, I extends Serializable>
extends JpaRepositoryFactoryBean<R, T, I> { protected RepositoryFactorySupport createRepositoryFactory(EntityManager entityManager) { return new MyRepositoryFactory(entityManager);
} private static class MyRepositoryFactory<T, I extends Serializable> extends JpaRepositoryFactory { private EntityManager entityManager; public MyRepositoryFactory(EntityManager entityManager) {
super(entityManager); this.entityManager = entityManager;
} protected Object getTargetRepository(RepositoryMetadata metadata) { return new MyRepositoryImpl<T, I>((Class<T>) metadata.getDomainClass(), entityManager);
} protected Class<?> getRepositoryBaseClass(RepositoryMetadata metadata) { // The RepositoryMetadata can be safely ignored, it is used by the JpaRepositoryFactory
//to check for QueryDslJpaRepository's which is out of scope.
return MyRepository.class;
}
}
}

4. 使用新创建的factory

<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. 基于zookeeper+leveldb搭建activemq集群--转载

    原地址:http://www.open-open.com/lib/view/open1410569018211.html 自从activemq5.9.0开始,activemq的集群实现方式取消了传统的 ...

  2. Unity 导出切片精灵

    http://blog.csdn.net/akof1314/article/details/38845933 设有一张png/tga图集,导入到Unity,放置目录"Assets/Resou ...

  3. 查看FC HBA卡信息的方法

    在配置磁盘阵列或虚拟磁带库时,往往会以FC接口与主机对接,那么就涉及FC HBA卡的查看,本文就这个问题进行了总结与整理. 一.Windows 系统 在Windows系统中,可以使用FC HBA卡厂家 ...

  4. VMware中Nat方式设置静态IP

    一.共享无线连接或本地连接,给VMnet8. 在网络配置中.选着无线连接,右键属性.共享. 这里默认给虚拟网卡VMnet8.分配了IP:192.168.137.1. 二,在VMware中配置VMnet ...

  5. 放大的X 【杭电-2655】 附题

    /* 放大的X Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Su ...

  6. 你被美国监控了,美国监控丑闻——"棱镜项目"事件

    http://www.ittime.com.cn/index.php?m=content&c=index&a=show&catid=29&id=3795 “棱镜”项目所 ...

  7. nginx 404重定向到自定义页面

    在访问时遇到上面这样的404错误页面,我想99%(未经调查,估计数据)的用户会把页面关掉,用户就这样悄悄的流失了.如果此时能有一个漂亮的页面能够引导用户去他想去的地方必然可以留住用户.因此,每一个网站 ...

  8. 在Windows Python3.5 安装LightGBM

    LightGBM是微软旗下DMTK推出的Gradient Boosting框架,因为其快速高效,以后或许会成为数据挖掘竞赛中的又一个大杀器.地址:https://github.com/Microsof ...

  9. window安装rabbitmq

    Rabbit MQ 是建立在强大的Erlang OTP平台上,因此安装Rabbit MQ的前提是安装Erlang.通过下面两个连接可以下载安装最新的版本: 下载并安装Eralng OTP For Wi ...

  10. Atitit.android webview h5运行环境总结

    Atitit.android webview h5运行环境总结 1. WebView 的使用1 2. Js调用java1 3. Js调用java 跟个swt的比较2 3.1. Swt是BrowserF ...