Spring boot 操作 mongoDB

  仔细算下来感觉大家喜欢看实用的博客,是人心态浮躁么?本人看thinking in java两边做的笔记算是心血之作,却是无人问津啊。现在就想写几篇实用性特别强的博客。
  我在之前写过一篇博客,关于Spring连接MongoDB数据库,并且做简单的增删检查,刚才看了上篇博客,有个思想很好,就是封装,不过在这里更加的简单粗暴。如果你只想了解其增删改查建议 只看 第四部分,其余的略过。
 
一、连接mongoDB工具
  其实最好的操作mongoDB的软件应该是Studio3T,本人找了半天找不破解版,若有道友有破解版,感谢告知。我使用的是RoBo3T ,因为使用了Studio3T一个月,然后再使用Robo3T感觉差距还是挺大的。这里以Robo3T连接为例:
  相关数据如下,填好即可连接成功。
  这里写图片描述
  这里写图片描述

二、创建SpringBoot及其准备工作
  1.新建一个Spring Boot,添加mongoDb数据库。或者自己百度添加依赖。(不懂的可以看我上篇关于MongoDB的博客)
  2.因为我们做这种简单的尝试,我们还是可以添加一个热部署的,添加依赖

<!-- Spring boot 热部署--> 
<dependency>  
    <groupId>org.springframework.boot</groupId>  
    <artifactId>spring-boot-devtools</artifactId>  
    <optional>true</optional>  
    <scope>true</scope>  
</dependency> 

  至此差不多没有任何问题。

三、操作MongoDB
  增:
  进入MongoOperation的实现类MongoTemplate里边查看增的方法

public void insert(Object objectToSave) {
    ensureNotIterable(objectToSave);
    insert(objectToSave, determineEntityCollectionName(objectToSave));
}

  会将objectToSave数据插入数据库中,数据库集合名默认是objectToSave。

public void insert(Object objectToSave, String collectionName) {
        ensureNotIterable(objectToSave);
        doInsert(collectionName, objectToSave, this.mongoConverter);
    }

  插入集合:

public void insert(Collection<? extends Object> batchToSave, String collectionName) {
    doInsertBatch(collectionName, batchToSave, this.mongoConverter);
}

  删:
  利用remove()

public WriteResult remove(Object object) {
    if (object == null) {
        return null;
    }

    return remove(getIdQueryFor(object), object.getClass());
}

public WriteResult remove(Object object, String collection) {
    Assert.hasText(collection, "Collection name must not be null or empty!");

    if (object == null) {
        return null;
    }

    return doRemove(collection, getIdQueryFor(object), object.getClass());
}

  改:

    public WriteResult upsert(Query query, Update update, Class<?> entityClass) {
        return doUpdate(determineCollectionName(entityClass), query, update, entityClass, true, false);
    }

    public WriteResult upsert(Query query, Update update, String collectionName) {
        return doUpdate(collectionName, query, update, null, true, false);
    }

    public WriteResult upsert(Query query, Update update, Class<?> entityClass, String collectionName) {
        return doUpdate(collectionName, query, update, entityClass, true, false);
    }

    public WriteResult updateFirst(Query query, Update update, Class<?> entityClass) {
        return doUpdate(determineCollectionName(entityClass), query, update, entityClass, false, false);
    }

    public WriteResult updateFirst(final Query query, final Update update, final String collectionName) {
        return doUpdate(collectionName, query, update, null, false, false);
    }

    public WriteResult updateFirst(Query query, Update update, Class<?> entityClass, String collectionName) {
        return doUpdate(collectionName, query, update, entityClass, false, false);
    }

    public WriteResult updateMulti(Query query, Update update, Class<?> entityClass) {
        return doUpdate(determineCollectionName(entityClass), query, update, entityClass, false, true);
    }

  可以看出
  查:

猜你喜欢

转载自blog.csdn.net/u010986518/article/details/82146613