有时,我们会需要用到自己定义的一些查询方法,可以按照下面几步进行。

1. 定义一个包含该方法的接口

Interface UserRepositoryCustom {
public void someCustomMethod(User user);
}

2. 定义实现

class UserRepositoryImpl implements UserRepositoryCustom {
public void someCustomMethod(User user){
//Your custom implementation
}
}

3. 定义你要用的repository

public interface UserRepository extends CrudRepository<User,Long>, UserRepositoryCustom {
//Declare query methods here.
}

4. 有两种方式可以对repository进行配置

4.1 通过统一的后缀名(限制是自己的implementation必须进行annotation配置,而且仅用于autowired)

<repositories base-package="com.acme.repository"/>//默认后缀为Impl
<repositories base-package="com.acme.repository" repository-impl-postfix="FooBar"/>

如果没有配置后缀,默认会找UserRepositoryImpl这个类。

4.2 自己定义bean

<repositories base-package="com.acme.repository"/>
<beans:bean id="userRepositoryImpl" class="...">
<!--further configuration-->
</beans:bean>

上面这种方法只能针对单一的repository,如果想所有的repository都包含这个方法,请看下一篇。

[Spring Data Repositories]学习笔记--定义自己的repository的更多相关文章

  1. [Spring Data Repositories]学习笔记--为repository添加通用的方法

    如果想把一个方法加到所有的repository中,用前一篇提到的方法就不合适了. 英文原版,请看 http://docs.spring.io/spring-data/data-mongo/docs/1 ...

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

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

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

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

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

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

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

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

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

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

  7. spring data jpa 学习笔记

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

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

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

  9. [Spring Data MongoDB]学习笔记--建立数据库的连接

    1. 有了上一篇的Mongo后,连接数据库我们还需要更多的信息,比如数据库名字,用户名和密码等. 我们可以继续来配置MongoDbFactory的实例. public interface MongoD ...

随机推荐

  1. JS里取前天,昨天和今天

    var today=new Date(); var yesterday=new Date(today.getTime()-1000*60*60*24); var thedaybeforeyesterd ...

  2. [BestCoder Round #3] hdu 4908 BestCoder Sequence (计数)

    BestCoder Sequence Problem Description Mr Potato is a coder. Mr Potato is the BestCoder. One night, ...

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

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

  4. Quartz的cron表达式 (spring定时器 crontab)

    http://tangshuo.iteye.com/blog/184824 表达式位数最少六位,如每天凌晨一点启动:"0 0 1 * * ?" 顺序按   秒 分 时 日期 月份 ...

  5. paho-mqtt 学习笔记

    Installation The latest stable version is available in the Python Package Index (PyPi) and can be in ...

  6. tcp/ip--IP 协议首部格式与其配套使用的四个协议(ARP,RARP,ICMP,IGMP)

    IP 数据报首部 最高位在左边,记为0 bit:最低位在右边,记为31 bit 版本: 占 4 位,指 IP 协议的版本目前的 IP 协议版本号为 4 (即 IPv4) 首部长度: 占4位,可表示的最 ...

  7. CSS3怎样实现超出指定文本以省略号显示效果

    作者:zhanhailiang 日期:2014-10-24 不做前端非常久了,今天从重构师那里了解到CSS3已经能够实现非常多以往必须通过JS才干实现的效果,如渐变,阴影,自己主动截断文本展示省略号等 ...

  8. Error: [vuex] vuex requires a Promise polyfill in this browser. 与 babel-polyfill 的问题

    Error: [vuex] vuex requires a Promise polyfill in this browser. 与 babel-polyfill 的问题 采用最笨重的解决方案就是npm ...

  9. PE下挂载注册表文件然后清除系统托盘空白图标缓存

    清除了右下角通知栏图标缓存TrayNotify(否则会出现一堆空白图标)清除缓存批处理脚本.bat如何在PE系统环境下清除宿主系统的托盘图标缓存? 清除了右下角通知栏图标缓存TrayNotify(否则 ...

  10. 160. Intersection of Two Linked Lists【easy】

    160. Intersection of Two Linked Lists[easy] Write a program to find the node at which the intersecti ...