Springboot-mongodb MongoRepository接口 save方法 详解
问题:
我们都知道 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方法 详解的更多相关文章
- “全栈2019”Java第六十五章:接口与默认方法详解
难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java第 ...
- SpringBoot系列(十二)过滤器配置详解
SpringBoot(十二)过滤器详解 往期精彩推荐 SpringBoot系列(一)idea新建Springboot项目 SpringBoot系列(二)入门知识 springBoot系列(三)配置文件 ...
- HTTP请求方法详解
HTTP请求方法详解 请求方法:指定了客户端想对指定的资源/服务器作何种操作 下面我们介绍HTTP/1.1中可用的请求方法: [GET:获取资源] GET方法用来请求已被URI识别的资源.指定 ...
- (转)Spring JdbcTemplate 方法详解
Spring JdbcTemplate方法详解 文章来源:http://blog.csdn.net/dyllove98/article/details/7772463 JdbcTemplate主要提供 ...
- JAVA 注解的几大作用及使用方法详解
JAVA 注解的几大作用及使用方法详解 (2013-01-22 15:13:04) 转载▼ 标签: java 注解 杂谈 分类: Java java 注解,从名字上看是注释,解释.但功能却不仅仅是注释 ...
- Java提高篇——equals()与hashCode()方法详解
java.lang.Object类中有两个非常重要的方法: 1 2 public boolean equals(Object obj) public int hashCode() Object类是类继 ...
- Python 字符串方法详解
Python 字符串方法详解 本文最初发表于赖勇浩(恋花蝶)的博客(http://blog.csdn.net/lanphaday),如蒙转载,敬请保留全文完整,切勿去除本声明和作者信息. ...
- vc中调用Com组件的方法详解
vc中调用Com组件的方法详解 转载自:网络,来源未知,如有知晓者请告知我.需求:1.创建myCom.dll,该COM只有一个组件,两个接口: IGetRes--方法Hello(), IGet ...
- Clone使用方法详解【转载】
博客引用地址:Clone使用方法详解 Clone使用方法详解 java“指针” Java语言的一个优点就是取消了指针的概念,但也导致了许多程序员在编程中常常忽略了对象与引用的区别,本文 ...
随机推荐
- Python 之网络编程
# 流程描述: # # 1. 服务器根据地址类型(ipv4, ipv6), socket类型, 协议创建socket; # # 2. 服务器为socket绑定ip地址和端口号; # # 3. 服务器s ...
- https://zh.cppreference.com 和 https://en.cppreference.com 和 https://cppcon.org/
https://zh.cppreference.comhttps://en.cppreference.com/w/ https://cppcon.org/
- 国产服务器离线安装gm
离线安装过程: 1.安装JPEGlib cd /opt/ ls tar -zxvf jpegsrc.v9b.tar.gz cd jpeg-9b/ ./configure make make insta ...
- LeetCode_Two Sum
Given an array of integers, find two numbers such that they add up to a specific target number. The ...
- java-mybaits-00502-案例-映射分析-一对一、一对多、多对多
1.一对一查询[类属性即可,association ] 案例:查询所有订单信息,关联查询下单用户信息. 注意:因为一个订单信息只会是一个人下的订单,所以从查询订单信息出发关联查询用户信息为一对一查 ...
- windows平台tensorboard的配置及使用
由于官网和其他教程里面都是以Linux为平台演示tensorboard使用的,而在Windows上与Linux上会有一些差别,因此我将学习的过程记录下来与大家分享(基于tensorflow1.2.1版 ...
- HDU1160:FatMouse's Speed(最长上升子序列,不错的题)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1160 学的东西还是不深入啊,明明会最长上升子序列,可是还是没有A出这题,反而做的一点思路没有,题意就不多说 ...
- ITL(Interested Transaction List)理解
一.ITL描述: ITL(Interested Transaction List)是Oracle数据块内部的一个组成部分,位于数据块头(block header),itl由xid,uba,flag,l ...
- 关于cgi、FastCGI、php-fpm、php-cgi(复制)
首先,CGI是干嘛的?CGI是为了保证web server传递过来的数据是标准格式的,方便CGI程序的编写者. web server(比如说nginx)只是内容的分发者.比如,如果请求/index.h ...
- PKU 1655 Balancing Act(树+树的重心)
#include<cstdio> #include<cstring> #include<algorithm> #define maxn 20005 using na ...