头图

The previous article of this tutorial: [One of the MongoDB introductory tutorial series: development environment construction and read and write access of Node.js and Java](), we first introduced the construction of the MongoDB local environment, and then gave two specific Example that shows how to access data stored in MongoDB using Node.js and Java.

This tutorial goes on to describe how to use the industry's popular development tools to operate MongoDB.

Spring Boot is a lightweight framework that does most of the configuration work for Spring-based applications. The purpose of Spring Boot is to provide a set of tools to quickly build easily configurable Spring applications, saving application developers the tedious configuration of a lot of traditional Spring projects.

As already introduced in the first article of this tutorial, MongoDB is a database based on distributed file storage, written in C++ language, which aims to provide a scalable and high-performance data storage solution for WEB applications.

This article describes how to use Spring Boot to manipulate MongoDB to insert data in MongoDB through Java code.

First, according to the introduction in the previous article of this tutorial, set up the development environment of MongoDB locally.

Create a new Java project, the content of pom.xml is as follows:

 <?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>

The role of the following dependency is to provide the SpringBoot application with the function of operating MongoDB:

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

And this dependency enables your Spring Boot application to support junit:

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

Create a .java file ending in Tests in the src/main/test folder, in my case ApplicationTests.java:

Paste the following code in:

 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"));
    }
}

The 27th line of code creates a new Book object, the id is 1, the name is didi, and the author is Jerry.

Then add it to MongoDB through bookRepository.

Implementation of 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);
}

In Eclipse, make sure this JUnit unit test runs successfully:

This inserted record is successfully seen in MongoDB Compass:


Summarize

SpringBoot is a Java application development framework based on Spring. Its design purpose is to simplify the initial construction and development process of Spring applications, and can help developers focus on business development tasks themselves. Based on the first article in the series, this article introduces the implementation details of read and write access to MongoDB in SpringBoot applications.


注销
1k 声望1.6k 粉丝

invalid