问题:

  我们都知道 mongodb 有两种添加数据的方式  一种 就是  save 方法   另外一种 insert  方法

  这里两个方法 唯一的区别就是

  

  insert:当主键"_id"在集合中存在时,不做任何处理。 抛异常

  save:当主键"_id"在集合中存在时,进行更新。 数据整体都会更新 ,新数据会替换掉原数据 ID 以外的所有数据。如ID 不存在就新增一条数据

save 方法 需要遍历列表,一个个插入, 而 insert 方法 是直接批量插入

  那么

    Springboot-mongodb   MongoRepository接口   并未提供 insert 方法 ,只提供了 save 方法  。 而  数据比较多 想使用 insert  批量插入 提高速度  怎么办

解决方法:

  第一种  使用 原生 MongoTemplate 类  进行处理  想怎么样就 怎么样 。  比如  ID 自增

    

@Component
public class CountersRepository
{
@Autowired
private MongoTemplate mongoTemplate; /**
* 通过两张表来做的 ID 自增
* @return 返回 最新的ID
*/
public Integer getId()
{
Query query = new Query(Criteria.where("_id").is("productid"));
Update update = new Update().inc("sequence_value", 1);
Counters m = mongoTemplate.findAndModify(query, update, Counters.class);
return m.getSequence_value();
} public void insertList(List<ThothOrder> t)
{
mongoTemplate.insertAll(t);
}
}

第二种   看 MongoRepository  接口 的具体实现类   SimpleMongoRepository<T, ID extends Serializable>   save 方法到底怎么写的。

  

public class SimpleMongoRepository<T, ID extends Serializable> implements MongoRepository<T, ID> {
private final MongoOperations mongoOperations;
private final MongoEntityInformation<T, ID> entityInformation; public SimpleMongoRepository(MongoEntityInformation<T, ID> metadata, MongoOperations mongoOperations) {
Assert.notNull(mongoOperations);
Assert.notNull(metadata);
this.entityInformation = metadata;
this.mongoOperations = mongoOperations;
} public <S extends T> S save(S entity) {
Assert.notNull(entity, "Entity must not be null!");
// 关键在这里 isNow 这个方法的实现类非常不好找
    // 判断一下主键的值是否存在,存在返回false,反正为true.通过 处理类 设置主键Id的,就会走save,而不是insert了
if(this.entityInformation.isNew(entity)) {
this.mongoOperations.insert(entity, this.entityInformation.getCollectionName());
} else {
this.mongoOperations.save(entity, this.entityInformation.getCollectionName());
} return entity;
} public <S extends T> List<S> save(Iterable<S> entities) {
Assert.notNull(entities, "The given Iterable of entities not be null!");
List<S> result = convertIterableToList(entities);
boolean allNew = true;
Iterator var4 = entities.iterator(); Object entity;
while(var4.hasNext()) {
entity = var4.next();
// 关键还是在这里
if(allNew && !this.entityInformation.isNew(entity)) {
allNew = false;
}
}
// 如果集合中 并未有 设置ID 主键的值 那么就 调用 insertAll 做批量插入
if(allNew) {
this.mongoOperations.insertAll(result);
} else {
var4 = result.iterator();
// 否则 就 遍历集合 逐个 插入 或更新
while(var4.hasNext()) {
entity = var4.next();
this.save(entity);
}
} return result;
}
}

最后

  强烈推荐 使用  myeclipse 或者 eclipse  开发的 亲们,  是适合 体验一下   IDEA  2017 了! 跟代码更轻松。

  

Springboot-mongodb MongoRepository接口 save方法 详解的更多相关文章

  1. “全栈2019”Java第六十五章:接口与默认方法详解

    难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java第 ...

  2. SpringBoot系列(十二)过滤器配置详解

    SpringBoot(十二)过滤器详解 往期精彩推荐 SpringBoot系列(一)idea新建Springboot项目 SpringBoot系列(二)入门知识 springBoot系列(三)配置文件 ...

  3. HTTP请求方法详解

    HTTP请求方法详解 请求方法:指定了客户端想对指定的资源/服务器作何种操作 下面我们介绍HTTP/1.1中可用的请求方法: [GET:获取资源]     GET方法用来请求已被URI识别的资源.指定 ...

  4. (转)Spring JdbcTemplate 方法详解

    Spring JdbcTemplate方法详解 文章来源:http://blog.csdn.net/dyllove98/article/details/7772463 JdbcTemplate主要提供 ...

  5. JAVA 注解的几大作用及使用方法详解

    JAVA 注解的几大作用及使用方法详解 (2013-01-22 15:13:04) 转载▼ 标签: java 注解 杂谈 分类: Java java 注解,从名字上看是注释,解释.但功能却不仅仅是注释 ...

  6. Java提高篇——equals()与hashCode()方法详解

    java.lang.Object类中有两个非常重要的方法: 1 2 public boolean equals(Object obj) public int hashCode() Object类是类继 ...

  7. Python 字符串方法详解

    Python 字符串方法详解 本文最初发表于赖勇浩(恋花蝶)的博客(http://blog.csdn.net/lanphaday),如蒙转载,敬请保留全文完整,切勿去除本声明和作者信息.        ...

  8. vc中调用Com组件的方法详解

    vc中调用Com组件的方法详解 转载自:网络,来源未知,如有知晓者请告知我.需求:1.创建myCom.dll,该COM只有一个组件,两个接口:   IGetRes--方法Hello(),   IGet ...

  9. Clone使用方法详解【转载】

    博客引用地址:Clone使用方法详解 Clone使用方法详解   java“指针”       Java语言的一个优点就是取消了指针的概念,但也导致了许多程序员在编程中常常忽略了对象与引用的区别,本文 ...

随机推荐

  1. What’s wrong with virtual methods called through an interface

    May 31, 2016 Calling a virtual method through an interface always was a lot slower than calling a st ...

  2. Fang Fang---hud5455(字符串处理)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5455 就是求字符串中含有几个f[i], 输出最小的: 例如fff应该是2,有f[0]和f[1]组成的; ...

  3. Python的一些教程(转)

    原文:http://blog.chinaunix.net/uid-26200547-id-3418038.html Python 安装配置及基本语法篇 Python 语言速成 Python 基本知识  ...

  4. x86架构下的控制寄存器CR0-CR4

    关于这几个寄存器,每次翻看intel手册都很不好找,干脆直接贴在这里吧!

  5. Nginx 之 内存池

    1.基本结构 先来学习一下nginx内存池的几个主要数据结构:[见:./src/core/ngx_palloc.h/.c]     ngx_pool_data_t(内存池数据块结构) 1: typed ...

  6. BBS项目部署之数据库的处理

    一  数据库的处理: 1.1上传bbs.sql(数据库中的数据) 1.2在mysql中创建bbs库,并导入数据库SQL脚本 在mysqld的文件夹中: mysql> create databas ...

  7. 深入浅出地,彻彻底底地理解python中的编码

    python处理文本的功能非常强大,但是如果是初学者,没有搞清楚python中的编码机制,也经常会遇到乱码或者decode error.本文的目的是简明扼要地说明python的编码机制,并给出一些建议 ...

  8. 02-django查询

      目录 (一)查询 1 .基本查询(等于.大于.包含字符.日期.字段比较.逻辑) 2 .关联查询(即为join查询)(一对多.多对多.一对一) 3 .聚合查询(统计查询) (二)关联对象(已知A表的 ...

  9. MySQL 最基本的SQL语法/语句

    DDL—数据定义语言(Create,Alter,Drop,DECLARE) DML—数据操纵语言(Select,Delete,Update,Insert) DCL—数据控制语言(GRANT,REVOK ...

  10. 使用Robomongo连接数据库不成功:没有启动MongoDB

    启动MongoDB ➜ ~ mongod --07T16:: I CONTROL [initandlisten] MongoDB starting : pid= port= dbpath=/data/ ...