因文档比较大,有时候findAll 不想返回所有数据.没有找到默认的findAll 能够include 或者 exclude 的方法,所以想办法扩展一下实现类

query.fields().include()
query.fields().exclude()

调试结果 SimpleMongoRepository 为 MongoRepository 的默认实现类,那么继承 SimpleMongoRepository 做一点修改

/**
* Created by laizhenwei on 2017/10/25
*/
public class MySimpleMongoRepository <T, ID extends Serializable> extends SimpleMongoRepository<T, ID> {
private final MongoOperations mongoOperations;
private final MongoEntityInformation<T, ID> entityInformation; public MySimpleMongoRepository(MongoEntityInformation<T, ID> metadata, MongoOperations mongoOperations) {
super(metadata, mongoOperations);
this.entityInformation = metadata;
this.mongoOperations = mongoOperations;
} public <S extends T> Page<S> findAll(final Example<S> example,final Query query, Pageable pageable) {
Assert.notNull(example, "Sample must not be null!");
query.addCriteria((new Criteria()).alike(example)).with(pageable);
List<S> list = this.mongoOperations.find(query, example.getProbeType(), this.entityInformation.getCollectionName());
return PageableExecutionUtils.getPage(list, pageable, () -> mongoOperations.count(query, example.getProbeType(),entityInformation.getCollectionName()));
}
}

OrderInfoRepository

/**
* Created by laizhenwei on 2017/10/25
*/
public interface OrderInfoRepository extends MongoRepository<OrderInfo,ObjectId> { Page<OrderInfo> findAll(final Example<OrderInfo> example, final Query query, Pageable pageable); }

MongoConfig

/**
* Created by laizhenwei on 2017/10/25
*/
@Configuration
@EnableMongoRepositories(basePackages = "com.example.mongo.Repository",repositoryBaseClass = MySimpleMongoRepository.class)
public class MongoConfig {
}

JunitTest

 @Test
public void findPage(){
ExampleMatcher matcher = ExampleMatcher.matching()
.withMatcher("buyer", ExampleMatcher.GenericPropertyMatchers.exact())
.withMatcher("phone",ExampleMatcher.GenericPropertyMatchers.exact()) OrderInfo orderInfo = new OrderInfo();
orderInfo.setBuyer("A君");
orderInfo.setPhone("13422981024"); Example<OrderInfo> example = Example.of(orderInfo,matcher); Query query = new Query();
query.fields().include("orderInfo");
Page<OrderInfo> page = orderInfoRepository.findAll(example,query,new PageRequest(0,10));
List<OrderInfo> orderInfos = page.getContent(); Assert.assertTrue(orderInfos.stream().filter(val->val.getPhone().equals("13422981024")).count()==1); System.out.println(orderInfos.size());
System.out.println(orderInfos.get(0).getOrderDetails());
}

Spring data mongodb 替换 Repository 实现类,findAll 排除 字段的更多相关文章

  1. 使用Spring Data Mongodb的MongoRepository类进行增删改查

    Spring Data Mongodb提供一套快捷操作 mongodb的方法,创建Dao,继承MongoRepository,并指定实体类型和主键类型. public interface CmsPag ...

  2. Spring Data MongoDB 一:入门篇(环境搭建、简单的CRUD操作)

    一.简介 Spring Data  MongoDB 项目提供与MongoDB文档数据库的集成.Spring Data MongoDB POJO的关键功能区域为中心的模型与MongoDB的DBColle ...

  3. Spring Data MongoDB example with Spring MVC 3.2

    Spring Data MongoDB example with Spring MVC 3.2 Here is another example web application built with S ...

  4. Introduction to Spring Data MongoDB

    Introduction to Spring Data MongoDB I just announced the new Spring 5 modules in REST With Spring: & ...

  5. Spring Data MongoDB 五:进阶文档查询(分页、Morphia)(二)

    Spring Data MongoDB 三:基本文档查询(Query.BasicQuery)(一) 学习MongoDB 六: MongoDB查询(游标操作.游标信息)(三) 一.简单介绍 Spring ...

  6. 如何在Spring Data MongoDB 中保存和查询动态字段

    原文: https://stackoverflow.com/questions/46466562/how-to-save-and-query-dynamic-fields-in-spring-data ...

  7. 使用Spring访问Mongodb的方法大全——Spring Data MongoDB查询指南

    1.概述 Spring Data MongoDB 是Spring框架访问mongodb的神器,借助它可以非常方便的读写mongo库.本文介绍使用Spring Data MongoDB来访问mongod ...

  8. Spring data mongodb 聚合,投射,内嵌数组文档分页.

    尽量别直接用 DBObject  ,Spring data mongodb 的api 本来就没什么多大用处,如果还直接用 DBObject 那么还需要自己去解析结果,说动做个对象映射,累不累 Spri ...

  9. Spring Data MongoDB 三:基本文档查询(Query、BasicQuery)(一)

    一.简单介绍 Spring Data  MongoDB提供了org.springframework.data.mongodb.core.MongoTemplate对MongoDB的CRUD的操作,上一 ...

随机推荐

  1. Java并发编程的艺术读书笔记(1)-并发编程的挑战

    title: Java并发编程的艺术读书笔记(1)-并发编程的挑战 date: 2017-05-03 23:28:45 tags: ['多线程','并发'] categories: 读书笔记 --- ...

  2. 远程服务调用(RMI)

    模块概念的引入,是本框架的一大优势,而跨JVM的远程服务调用则是另一个最有价值的功能. <本地服务调用>一文中我们讲解了跨模块间的服务调用可以是这样的: ServiceHelper.inv ...

  3. SQL Server——存储过程

    我想从下面几个方面大概的讲述下存储过程,可能有些知识点是你没有注意的,也可能有些知识点我不知道,欢迎大家指点指点.如有不足,欢迎指教! 存储过程概念 存储过程优点 存储过程的接口 存储过程的解析.编译 ...

  4. 如何用命令将本地项目上传到github

    一.Git终端软件安装 1.下载windows上git终端,类似shell工具,下载地址:http://msysgit.github.io/ 2.安装方法,打开文件,一路点击Next即可 3.安装完成 ...

  5. Selenium+java+idea的安装与配置

    当前操作系统:Windows10pro x64 一.安装JDK 1.到jdk官网下载一个对应当前系统的安装包(Selenium仅支持JDK1.8及以上版本) 网址:http://www.oracle. ...

  6. Win10图片打开方式没有“Windows照片查看器”,如何找回?

    如果你是全新安装的Win10正式版,那么就会发现当在图片上点击右键时,"打开方式"菜单里熟悉的"Windows照片查看器"不见了,换成了Win10全新的&quo ...

  7. WPF之ViewModel

    写之前我先唠叨两句,现在公司要求用wpf写个客户端,我之前玩过winform,没怎么用wpf写过正经项目,我接手这个wpf项目之后就开始研究这个东西,MVVM,自定义控件,等等.可能是winfrom先 ...

  8. 查询集API -- Django从入门到精通系列教程

    该系列教程系个人原创,并完整发布在个人官网刘江的博客和教程 所有转载本文者,需在顶部显著位置注明原作者及www.liujiangblog.com官网地址. Python及Django学习QQ群:453 ...

  9. Flex Grid学习-链接

    这些是我个人在学习这两种布局的时候参考的资料,希望对大家有用-- 1.Flex 阮一峰(flex语法讲解):http://blog.csdn.net/naruto_luoluo/article/det ...

  10. Oracle 12cR1 RAC 在VMware Workstation上安装(下)—静默安装

    Oracle 12cR1 RAC 在VMware Workstation上安装(下)—静默安装 1.1  静默安装 1.1.1  静默安装grid 安装之前使用脚本进行校验,确保所有的failed选项 ...