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 了! 跟代碼更輕松。 

 

 

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM