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 了! 跟代码更轻松。 

 

 

  

转载于:https://www.cnblogs.com/atliwen/p/6644627.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值