zoukankan      html  css  js  c++  java
  • 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 了! 跟代码更轻松。 

      

  • 相关阅读:
    Storm—Storm集群搭建
    fis3工程化中的模块化开发
    rem、px、em(手机端h5页面屏幕适配的几种方法)
    最完整的React+Redux+router兼容ie8 修改!!!!
    移动端常见的一些兼容性问题
    移动端常见问题及解决方案
    EditorConfig 介绍
    当当主页
    JD主页
    react 项目的一个ie8兼容性问题
  • 原文地址:https://www.cnblogs.com/atliwen/p/6644627.html
Copyright © 2011-2022 走看看