关于redis:MongoDB-入门教程系列之二使用-Spring-Boot-操作-MongoDB

6次阅读

共计 3683 个字符,预计需要花费 10 分钟才能阅读完成。

本教程的前一篇文章:[MongoDB 入门教程系列之一:开发环境搭建以及 Node.js 和 Java 的读写访问](),咱们首先介绍了 MongoDB 本地环境的搭建,接着举了两个具体的例子,展现了如何应用 Node.js 和 Java 拜访 MongoDB 存储的数据。

本教程持续介绍如何应用业界风行的开发工具来操作 MongoDB.

Spring Boot 是一个轻量级框架,能够实现基于 Spring 的应用程序的大部分配置工作。Spring Boot 的目标是提供一组工具,以便疾速构建容易配置的 Spring 应用程序,为利用开发人员省去大量传统 Spring 我的项目的繁琐配置。

本教程第一篇文章曾经介绍过,MongoDB 是一个基于分布式文件存储的数据库,由 C++ 语言编写,旨在为 WEB 利用提供可扩大的高性能数据存储解决方案。

本文介绍如何应用 Spring Boot 操作 MongoDB,从而通过 Java 代码在 MongoDB 里插入数据。

首先依照本教程前一篇文章的介绍,在本地搭建好 MongoDB 的开发环境。

新建一个 Java 我的项目,pom.xml 的内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.springframework</groupId>
<artifactId>gs-rest-service</artifactId>
<version>0.1.0</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongodb-driver</artifactId>
<version>3.6.4</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
</dependencies>
<properties>
<java.version>1.8</java.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-releases</id>
<url>https://repo.spring.io/libs-release</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-releases</id>
<url>https://repo.spring.io/libs-release</url>
</pluginRepository>
</pluginRepositories>
</project>

其中上面这个 dependency 的作用是为 SpringBoot 利用提供操作 MongoDB 的性能:

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>

而这个 dependency 能让您的 Spring Boot 利用反对 junit:

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

src/main/test 文件夹下创立一个以 Tests 结尾的 .java 文件,我的例子里是 ApplicationTests.java:

将如下代码粘贴进去:

package main.test;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import main.java.library.Application;
import main.java.library.Book;
import main.java.library.BookRepository;
@RunWith(SpringRunner.class)
@SpringBootTest(classes=Application.class)
public class ApplicationTests {
    @Autowired
    private BookRepository bookRepository;
    @Before
    public void setUp() {bookRepository.deleteAll();
    }
    @Test
    public void test() throws Exception {bookRepository.save(new Book("1", "didi", "Jerry"));
    }
}

第 27 行代码,新建了一个 Book 对象,id 为 1,name 为 didi,作者为 Jerry.

而后通过 bookRepository 退出到 MongoDB 里。

BookRepository 的实现:

import java.util.Optional;
import org.springframework.data.mongodb.repository.MongoRepository;
public interface BookRepository extends MongoRepository<Book, String>, BookRepositoryCustom {public Optional<Book> findByName(String name);
}

在 Eclipse 里,确保这个 JUnit 单元测试运行胜利:

在 MongoDB Compass 里胜利看到这条插入的记录:


总结

SpringBoot 是一个基于 Spring 的 Java 利用开发框架,其设计目标是用来简化 Spring 利用的初始搭建以及开发过程,可能帮忙开发人员专一于业务开发工作自身下来。本文在系列第一篇文章的根底上,介绍了在 SpringBoot 利用里对 MongoDB 的读写访问实现细节。

正文完
 0