因文档比较大,有时候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. linkin大话数据结构--Set

    Set 集合 Set 集合不允许包含相同的元素,如果试把两个相同的元素加入同一个 Set 集合中,则添加操作失败. Set 判断两个对象是否相同不是使用 == 运算符,而是根据 equals 方法.也 ...

  2. linkin大话面向对象--内部类

    内部类说白了就是类中有类 内部类:嵌套类 外部类:宿主类 内部类主要有以下作用:记住了3个字:多继承... 1,内部类提供了更好的封装,可以把内部类隐藏在外部类中,不允许同一个包中的其他类访问该类. ...

  3. Mybatis集成到spring boot

    1, Mybatis简介 MyBatis 是支持定制化 SQL.存储过程以及高级映射的优秀的持久层框架.MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集.MyBatis 可 ...

  4. Spring 4.x (一)

    1 Spring是什么? Spring是一个开源框架 Spring是为简化企业级应用开发而生的,使用Spring可以使得简单的JavaBean能够实现以前只有EJB才能实现的功能. Spring是一个 ...

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

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

  6. 【转】Shell执行MySql操作

    mysql  -hhostname -Pport -uusername -ppassword  -e  相关mysql的sql语句,不用在mysql的提示符下运行mysql,即可以在shell中操作m ...

  7. 开发JQuery插件(转)

    教你开发jQuery插件(转)   阅读目录 基本方法 支持链式调用 让插件接收参数 面向对象的插件开发 关于命名空间 关于变量定义及命名 压缩的好处 工具 GitHub Service Hook 原 ...

  8. mysql数据库在Linux下安装与配置

      mysql是一种开放源代码的关系型数据库系统(RDBMS),使用最常用的数据库管理语言--结构化查询语句(SQL)进行数据库管理. 由于mysql的开源,因此任何人都可以在General Publ ...

  9. [DeeplearningAI笔记]改善深层神经网络_优化算法2.6_2.9Momentum/RMSprop/Adam优化算法

    Optimization Algorithms优化算法 觉得有用的话,欢迎一起讨论相互学习~Follow Me 2.6 动量梯度下降法(Momentum) 另一种成本函数优化算法,优化速度一般快于标准 ...

  10. 12、SEO工程师指南 - 软件项目角色指南系列文章

    第11章       SEO工程师   SEO工程师是一个比较新兴的职位,在实际的项目管理过程中,SEO工程师的地位相对靠后,只有在项目试运行以及运营期间才能体现出SEO工程师的作用.在项目完成之后, ...