如果想把一个方法加到所有的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. C#秘密武器之LINQ to SQL

    LINQ to SQL语句(1)之Where 适用场景:实现过滤,查询等功能. 说明:与SQL命令中的Where作用相似,都是起到范围限定也就是过滤作用的,而判断条件就是它后面所接的子句.Where操 ...

  2. 记录一个奇妙的Bug, -1 &gt;= 2 ?

    直接上代码: #include <iostream> #include <vector> using namespace std; int main() { vector< ...

  3. CSS/JavaScript hacks,browserhacks使用

    1.网址 http://browserhacks.com/ 2.使用 (1)JavaScript Hacks 浏览器js判断 (2)条件注释hack (3)Media Query Hacks 媒体查询 ...

  4. RAID详解[RAID0/RAID1/RAID10/RAID5] (转)

    一.RAID定义RAID(Redundant Array of Independent Disk 独立冗余磁盘阵列)技术是加州大学伯克利分校1987年提出,最初是为了组合小的廉价磁盘来代替大的昂贵磁盘 ...

  5. ROS示例----导航功能包Husky_exploration

    ROS导航功能包示例husky amcl gmapping slam exploration 此功能包包含如下文件: 结构如下: $ tree -L 2 . ├── CMakeLists.txt -& ...

  6. Tomcat中work目录的作用

    今天在修改了某个jsp后发现:tomcat容器启动后,访问该jsp返回的结果依然是修改之前的内容,略感不解,于是乎研究了一下Tomcat中work目录的作用. Tomcat中work目录的作用: js ...

  7. Spark调研笔记第6篇 - Spark编程实战FAQ

    本文主要记录我使用Spark以来遇到的一些典型问题及其解决的方法,希望对遇到相同问题的同学们有所帮助. 1. Spark环境或配置相关 Q: Sparkclient配置文件spark-defaults ...

  8. Python - pandas 数据分析

    pandas: powerful Python data analysis toolkit 官方文档: http://pandas.pydata.org/pandas-docs/stable/ 1. ...

  9. typeof 和 Object.prototype.toString.call 数据类型判断的区别

    使用 typeof 来判断数据类型,只能区分基本类型,即 “number”,”string”,”undefined”,”boolean”,”object” 五种. 但 Object.prototype ...

  10. mysql 应用场景

    一.按时间点来统计 ), date_FORMAT(date_Field,'%Y-%m-%d %H:00:00') as dateStr from table_name group by dateStr