[Spring Data Repositories]学习笔记--为repository添加通用的方法
如果想把一个方法加到所有的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添加通用的方法的更多相关文章
- [Spring Data Repositories]学习笔记--使用现有的repository
以下内容是在学习Spring-Data-mongoDB中的Spring Data Repositories时做的一些笔记.备忘! 感觉学习还是看官方的资料比较透彻一些. Spring Data Rep ...
- [Spring Data Repositories]学习笔记--定义自己的repository
有时,我们会需要用到自己定义的一些查询方法,可以按照下面几步进行. 1. 定义一个包含该方法的接口 Interface UserRepositoryCustom { public void someC ...
- 031 Spring Data Elasticsearch学习笔记---重点掌握第5节高级查询和第6节聚合部分
Elasticsearch提供的Java客户端有一些不太方便的地方: 很多地方需要拼接Json字符串,在java中拼接字符串有多恐怖你应该懂的 需要自己把对象序列化为json存储 查询到结果也需要自己 ...
- Spring data jpa 实现简单动态查询的通用Specification方法
本篇前提: SpringBoot中使用Spring Data Jpa 实现简单的动态查询的两种方法 这篇文章中的第二种方法 实现Specification 这块的方法 只适用于一个对象针对某一个固定字 ...
- spring data jpa 学习笔记
springboot 集成 springData Jpa 1.在pom.xml添加依赖 <!-- SpringData-Jpa依赖--> <dependency <groupI ...
- [Spring Data MongoDB]学习笔记--MongoTemplate查询操作
查询操作主要用到两个类:Query, Criteria 所有的find方法都需要一个query的object. 1. 直接通过json来查找,不过这种方式在代码中是不推荐的. BasicQuery q ...
- [Spring Data MongoDB]学习笔记--MongoTemplate插入修改操作
插入操作: 直接给个例子 import static org.springframework.data.mongodb.core.query.Criteria.where; import static ...
- [Spring Data MongoDB]学习笔记--_id和类型映射
_id字段的映射: MongoDB要求所有的document都要有一个_id的字段. 如果我们在使用中没有传入_id字段,它会自己创建一个ObjectId. { , "accounts&qu ...
- [Spring Data MongoDB]学习笔记--牛逼的MongoTemplate
MongoTemplate是数据库和代码之间的接口,对数据库的操作都在它里面. 注:MongoTemplate是线程安全的. MongoTemplate实现了interface MongoOperat ...
随机推荐
- iOS-国家代码选择功能github开源分享
三行代码集成国家区号选择功能 功能执行效果如图: 开源链接: https://github.com/qxuewei/XWCountryCode 用法: 1.导入XWCountryCode类 2.在须要 ...
- Android下的数据存储与訪问 --- 以文件的形式
Android下的数据存储与訪问 --- 以文件的形式 1.1 储存文件存放在手机内存中: // *** 储存数据到 /data/data/包名/files/jxn.txt文件里 String dat ...
- Unity3D教程宝典之Web服务器篇:(第一讲)服务器的架设
转载自风宇冲Unity3D教程学院 引言:本文主要介绍WAMP服务器的架设. 第一部分WAMP介绍;第二部分WAMP安装及使用. 第一部分WAMP介绍 什 ...
- 重新 java 对象的 equals 和 hashCode 方法的建议和示例代码
equals 方法 equals 方法需要满足的规范: 自反性: 对于任意非空引用 x, x.equals(x) 应该返回 true; 对称性: 对于任意引用, 当且仅当 x.equals(y) == ...
- Python实时语音识别控制
代码地址如下:http://www.demodashi.com/demo/12946.html Python实时语音识别控制 概述 本文中的语音识别功能采用 百度语音识别库 ,首先利用 PyAudio ...
- asp.net+mvc+easyui+sqlite 简单用户系统学习之旅(四)—— session传递登录人信息显示+用户注销
前面的学习,已经做好了简单的登录和主页,涉及了三层软件架构,mvc,easyui的layout.tree.tab.window,sqlite,动软代码自动生成. 下面完善一下登录跳转主页后,在sout ...
- 电脑 F键(功能键)的具体作用
F1:如果你处在一个选定的程序中而需要帮助,那么请按下F1.如果现在不是处在任何程序中,而是处在资源管理器或桌面,那么按下F1就会出现Windows的帮助程序.如果你正在对某个程序进行操作,而想得到W ...
- ASP.NET_SessionId 不相同导致验证码出错
问题: 今天碰到一个比较奇怪的问题,每个页面请求的cookie的ASP.NET_SessionId 不相同,导致验证码检验出错.也就是说每个请求都是一个新的会话,我们把验证码保证在Session[ra ...
- .net面试中的一些常见问题与答案
1.页面传值的方式及其优缺点? Session,Application,Cookies,Requst.QueryString,Requst.Form,Server.Transfer(通过Conte ...
- 工作总结 razor 接收datatable