黄能勇 發表於 2019-12-25 15:58:00

springboot2 整合mongodb

<p>在springboot2中使用MongoDB</p>
<p>1、引入依赖</p>
<div class="cnblogs_code">
<pre>&lt;dependency&gt;
    &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
    &lt;artifactId&gt;spring-boot-starter-data-mongodb&lt;/artifactId&gt;
    &lt;version&gt;2.2.2.RELEASE&lt;/version&gt;
&lt;/dependency&gt;</pre>
</div>
<p>2、写入yml文件</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">spring:
data:
    mongodb:
      uri: mongodb:</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"><span style="background-color: rgba(255, 255, 153, 1)"><span style="text-decoration: underline">user</span>:<span style="text-decoration: underline">pwd</span>@</span>localhost:<span style="text-decoration: underline">27017</span>/<span style="text-decoration: underline">database</span></span></pre>
</div>
<p>注意,下划线部分,根据本机设置,自己调整,如果没有设置权限,去掉黄色部分即可。</p>
<p>还有需要注意的是,涉及到权限时,要不就是使用uri的方式指定路径,要不就使用port、host、username等指定,不可混用。</p>
<p>3、上代码(我觉着定义成静态方法更合适,先不调整了,这个可以用)</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">@Service
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> MongoUtils {

    @Autowired
    </span><span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)"> MongoTemplate mongoTemplate;

    </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> insertOne(String collection, Document document) {
      mongoTemplate.getCollection(collection).insertOne(document);
    }

    </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span> insertMany(String collection, List&lt;Document&gt;<span style="color: rgba(0, 0, 0, 1)"> documentList) {
      mongoTemplate.getCollection(collection).insertMany(documentList);
    }

    </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> Document findById(String collection, String id) {
      Document query </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Document();
      query.put(</span>"_id"<span style="color: rgba(0, 0, 0, 1)">, id);
      </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> findOne(collection, query);
    }

    </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> Document findOne(String collection, Bson filter) {
      </span><span style="color: rgba(0, 0, 255, 1)">return</span> findMany(collection, filter).get(0<span style="color: rgba(0, 0, 0, 1)">);
    }

    </span><span style="color: rgba(0, 0, 255, 1)">public</span> List&lt;Document&gt;<span style="color: rgba(0, 0, 0, 1)"> findAll(String collection) {
      </span><span style="color: rgba(0, 0, 255, 1)">return</span> findMany(collection, <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">);
    }

    </span><span style="color: rgba(0, 0, 255, 1)">public</span> List&lt;Document&gt;<span style="color: rgba(0, 0, 0, 1)"> findMany(String collection, Bson filter) {
      filter </span>=<span style="color: rgba(0, 0, 0, 1)"> handelId(filter);
      MongoCollection</span>&lt;Document&gt; mongoCollection =<span style="color: rgba(0, 0, 0, 1)"> mongoTemplate.getCollection(collection);
      FindIterable</span>&lt;Document&gt; findIterable =<span style="color: rgba(0, 0, 0, 1)"> mongoCollection.find();
      MongoCursor</span>&lt;Document&gt; mongoCursor =<span style="color: rgba(0, 0, 0, 1)"> findIterable.filter(filter).iterator();
      List</span>&lt;Document&gt; documentList = <span style="color: rgba(0, 0, 255, 1)">new</span> ArrayList&lt;Document&gt;<span style="color: rgba(0, 0, 0, 1)">();
      </span><span style="color: rgba(0, 0, 255, 1)">while</span><span style="color: rgba(0, 0, 0, 1)"> (mongoCursor.hasNext()) {
            Document document </span>=<span style="color: rgba(0, 0, 0, 1)"> mongoCursor.next();
            documentList.add(document);
      }
      </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> documentList;
    }

    </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> updateOne(String collection, Document document) {
      Document query </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Document();
      query.put(</span>"_id", <span style="color: rgba(0, 0, 255, 1)">new</span> ObjectId(document.get("_id"<span style="color: rgba(0, 0, 0, 1)">).toString()));
      document.remove(</span>"_id"<span style="color: rgba(0, 0, 0, 1)">);
      Document updateDoc </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Document();
      updateDoc.put(</span>"$set"<span style="color: rgba(0, 0, 0, 1)">, document);
      mongoTemplate.getCollection(collection).updateOne(query, updateDoc);
    }

    </span><span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)"> Document handelId(Bson bson) {
      </span><span style="color: rgba(0, 0, 255, 1)">if</span> (bson != <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">) {
            Document document </span>=<span style="color: rgba(0, 0, 0, 1)"> (Document) bson;
            Object id </span>= <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">;
            </span><span style="color: rgba(0, 0, 255, 1)">if</span> (document.containsKey("id"<span style="color: rgba(0, 0, 0, 1)">)) {
                id </span>= document.get("id"<span style="color: rgba(0, 0, 0, 1)">);
                document.remove(</span>"id"<span style="color: rgba(0, 0, 0, 1)">);
            } </span><span style="color: rgba(0, 0, 255, 1)">else</span> <span style="color: rgba(0, 0, 255, 1)">if</span> (document.containsKey("_id"<span style="color: rgba(0, 0, 0, 1)">)) {
                id </span>= document.get("_id"<span style="color: rgba(0, 0, 0, 1)">);
            }
            </span><span style="color: rgba(0, 0, 255, 1)">if</span> (id != <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">) {
                ObjectId objId </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ObjectId(id.toString());
                document.put(</span>"_id"<span style="color: rgba(0, 0, 0, 1)">, objId);
            }
            </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> document;
      }
      </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Document();
    }
}</span></pre>
</div>
<p>4、测试类(用法参考)</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">@SpringBootTest(webEnvironment </span>=<span style="color: rgba(0, 0, 0, 1)"> SpringBootTest.WebEnvironment.RANDOM_PORT)
@Transactional    // 注释后回滚
</span><span style="color: rgba(0, 0, 255, 1)">class</span> MongoDBTest<span style="color: rgba(0, 0, 0, 1)"> {
    @Autowired
    </span><span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)"> MongoUtils mongoUtils;

    @BeforeEach
    </span><span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> setUp() {
    }

    @AfterEach
    </span><span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> tearDown() {
    }

    @Test
    </span><span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> insertOne() {
      Document doc </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Document();
      doc.put(</span>"name", "world"<span style="color: rgba(0, 0, 0, 1)">);
      doc.put(</span>"age", 16<span style="color: rgba(0, 0, 0, 1)">);
      mongoUtils.insertOne(</span>"collection"<span style="color: rgba(0, 0, 0, 1)">, doc);
    }

    @Test
    </span><span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> insertMany() {
      List</span>&lt;Document&gt; documentList = <span style="color: rgba(0, 0, 255, 1)">new</span> ArrayList&lt;Document&gt;<span style="color: rgba(0, 0, 0, 1)">();
      Document doc </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Document();
      doc.put(</span>"name", "sam"<span style="color: rgba(0, 0, 0, 1)">);
      doc.put(</span>"age", 30<span style="color: rgba(0, 0, 0, 1)">);
      documentList.add(doc);
      doc </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Document();
      doc.put(</span>"name", "nicole"<span style="color: rgba(0, 0, 0, 1)">);
      doc.put(</span>"age", 30<span style="color: rgba(0, 0, 0, 1)">);
      documentList.add(doc);
      mongoUtils.insertMany(</span>"collection"<span style="color: rgba(0, 0, 0, 1)">, documentList);
    }

    @Test
    </span><span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> findById() {
      System.out.println(mongoUtils.findById(</span>"collection", "5db7a5e66957f95f2a7459a6"<span style="color: rgba(0, 0, 0, 1)">));
    }

    @Test
    </span><span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> updateOne() {
      Document doc </span>= mongoUtils.findById("collection", "5db7a5e66957f95f2a7459a6"<span style="color: rgba(0, 0, 0, 1)">);
      doc.replace(</span>"name", "Jully"<span style="color: rgba(0, 0, 0, 1)">);
      mongoUtils.updateOne(</span>"collection"<span style="color: rgba(0, 0, 0, 1)">, doc);
    }

    @Test
    </span><span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> findOne() {
      Document doc </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Document();
      doc.put(</span>"name", "hello"<span style="color: rgba(0, 0, 0, 1)">);
      System.out.println(mongoUtils.findOne(</span>"collection"<span style="color: rgba(0, 0, 0, 1)">, doc));
    }

    @Test
    </span><span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> findMany() {
      Document doc </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Document();
      doc.put(</span>"id", "5db7a5e66957f95f2a7459a6"<span style="color: rgba(0, 0, 0, 1)">);
      System.out.println(mongoUtils.findMany(</span>"collection"<span style="color: rgba(0, 0, 0, 1)">, doc));
    }

    @Test
    </span><span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> findAll() {
      System.out.println(mongoUtils.findAll(</span>"collection"<span style="color: rgba(0, 0, 0, 1)">));
    }
}</span></pre>
</div>
<p>如果有问题,欢迎反馈。</p>


</div>
<div id="MySignature" role="contentinfo">
    知止而后有定;定而后能静;静而后能安;安而后能虑;虑而后能得。<br><br>
来源:https://www.cnblogs.com/SamNicole1809/p/12097182.html
頁: [1]
查看完整版本: springboot2 整合mongodb