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语言的一个优点就是取消了指针的概念,但也导致了许多程序员在编程中常常忽略了对象与引用的区别,本文 ...
随机推荐
- jquery插件网址
各种分布图的插件:http://echarts.baidu.com/demo.html
- LeetCode_Insertion Sort List
题目:Sort a linked list using insertion sort,即仿照插入排序(直接插入排序)对一个链表排序. 插入排序的思想:总共进行n-1趟排序,在排列第i个元素时,前面的i ...
- Supervisor快速上手指南(转)
原文:http://maemual.me/index.php/archives/8/ Supervisor是一个进程控制程序.用于监控管理你需要的程序. 当你有一个程序,需要长期在后台运行,并且希望能 ...
- getApplicationContext()、Activity.this、 getBaseContext区别
getApplicationContext()返回应用的上下文,生命周期是整个应用,应用退出它才被摧毁 Activity.this 返回当前activity的上下文,属于activity ,activ ...
- 常用的自定义Python函数
常用的自定义Python函数 1.时间戳转为日期字串,精确到ms.单位s def timestamp2datems(timestamp): ''' 时间戳转为日期字串,精确到ms.单位s :param ...
- rtsp/rtmp/hls/onvif测试源以及ffmpeg在流媒体方面的应用
一.rtsp/rtmp/hls/onvif测试源 1. rtsp rtsp://184.72.239.149/vod/mp4:BigBuckBunny_175k.mov 2.rtmp rtmp://l ...
- 基于python3.6.6的scrapy环境部署+图像识别插件安装
一.Python3.6.6安装1.安装依赖的二进制软件包yum -y install zlib zlib-devel bzip2 bzip2-devel ncurses ncurses-devel r ...
- PAT 1041 Be Unique[简单]
1041 Be Unique (20 分) Being unique is so important to people on Mars that even their lottery is desi ...
- HTML5笔记——formData
注:formData中的数据在控制台上的console里面是打印不出来的,只能在控制台的network里面查看到具体的发送数据和发送选项 文章出处:梦想天空 XMLHttpRequest Level ...
- Creating an AVI in memory with C++
Creating an AVI in memory with C++ The following example demonstrates how an avi file is comp ...