与HibernateRepository类似,通过继承MongoRepository接口,我们可以非常方便地实现对一个对象的增删改查,要使
用Repository的功能,先继承MongoRepository<T, 
TD>接口,其中T为仓库保存的bean类,TD为该bean的唯一标识的类型,一般为ObjectId。之后在service中注入该接口就可以
使用,无需实现里面的方法,spring会根据定义的规则自动生成。

例:

public interface PersonRepository extends

MongoRepository<Person, ObjectId>{ 
//这里可以添加额外的查询方法 

但是MongoRepository实现了的只是最基本的增删改查的功能,要想增加额外的查询方法,可以按照以下规则定义接口的方法。自定义查询方
法,格式为“findBy+字段名+方法后缀”,方法传进的参数即字段的值,此外还支持分页查询,通过传进一个Pageable对象,返回Page集合。

例:

public interface PersonRepository extends

MongoRepository<Person, ObjectId>{ 
//查询大于age的数据 
public Page<Product> findByAgeGreaterThan(int age,Pageable page) ; 

下面是支持的查询类型,每三条数据分别对应:(方法后缀,方法例子,mongodb原生查询语句)

GreaterThan(大于) 
findByAgeGreaterThan(int age) 
{"age" : {"$gt" : age}}

LessThan(小于) 
findByAgeLessThan(int age) 
{"age" : {"$lt" : age}}

Between(在...之间) 
findByAgeBetween(int from, int to) 
{"age" : {"$gt" : from, "$lt" : to}}

IsNotNull, NotNull(是否非空) 
findByFirstnameNotNull() 
{"age" : {"$ne" : null}}

IsNull, Null(是否为空) 
findByFirstnameNull() 
{"age" : null}

Like(模糊查询) 
findByFirstnameLike(String name) 
{"age" : age} ( age as regex)

(No keyword) findByFirstname(String name) 
{"age" : name}

Not(不包含) 
findByFirstnameNot(String name) 
{"age" : {"$ne" : name}}

Near(查询地理位置相近的) 
findByLocationNear(Point point) 
{"location" : {"$near" : [x,y]}}

Within(在地理位置范围内的) 
findByLocationWithin(Circle circle) 
{"location" : {"$within" : {"$center" : [ [x, y], distance]}}}

Within(在地理位置范围内的) 
findByLocationWithin(Box box) 
{"location" : {"$within" : {"$box" : [ [x1, y1], x2, y2]}}}

尽管以上查询功能已经很丰富,但如果还不能满足使用情况的话可以用一下方法---基于mongodb原本查询语句的查询方式。
例:在原接口中加入

@Query("{ 'name':{'$regex':?2,'$options':'i'}, sales': {'$gte':?1,'$lte':?2}}") 
public Page<Product> findByNameAndAgeRange(String name,double ageFrom,double ageTo,Pageable page);

注释Query里面的就是mongodb原来的查询语法,我们可以定义传进来的查询参数,通过坐标定义方法的参数。

还可以在后面指定要返回的数据字段,如上面的例子修改如下,则只通过person表里面的name和age字段构建person对象。

@Query(value="{ 'name':{'$regex':?2,'$options':'i'}, sales':{'$gte':?1,'$lte':?2}}",fields="{ 'name' : 1, 'age' : 1}") 
public Page<Product> findByNameAndAgeRange(String name,double ageFrom,double ageTo,Pageable page);
---------------------
作者:杰克学编程
来源:CSDN
原文:https://blog.csdn.net/codeiswhat/article/details/52129782
版权声明:本文为博主原创文章,转载请附上博文链接!

最新文章

  1. microsoft docx document operation with Java POI library
  2. ionic实现双击返回键退出功能
  3. eclipse color theme 选择一款适合你的代码样式
  4. 给“.Net工资低”争论一个了结吧!
  5. Css选择器的优先级
  6. 14.6.4 Configuring the Memory Allocator for InnoDB 配置InnoDB 内存分配器
  7. Graphics2D 中文乱码
  8. Sticks(Central Europe 1995) (DFS)
  9. easyhadoop:failed to open stream:Permission denied in /var/www/html/index.php
  10. Unix系统的常用信号
  11. Git安装与仓库搭建
  12. 从零开始学安全(三)●黑客常用的windows端口
  13. P3168 [CQOI2015]任务查询系统
  14. 变量安全过滤,防止xss攻击
  15. 清理孤儿文件 clearing up outdated orphans
  16. Deep learnin简介
  17. 做一个完整的Java Web项目需要掌握的技能[转]
  18. linux系统安装 dig和nslookup命令
  19. ASP.NET之旅--深入浅出解读IIS架构
  20. 祝高二学弟学妹AK NOIp2018!!!!!!

热门文章

  1. yolov3应该什么时候停止训练?
  2. redis集群+JedisCluster+lua脚本实现分布式锁(转)
  3. Spring特点与工作原理
  4. ActiveMQ入门操作示例
  5. JavaWeb【三、Web程序编写】
  6. VM 下增加磁盘空间
  7. 关于C++跨平台
  8. 使用remix的solidity在线编译环境部署一个faucet合约
  9. phpstorm激活码
  10. SqlServer获取当前日期