Spring Boot is a lightweight framework that does most of the configuration for Spring-based applications. The purpose of Spring Boot is to provide a set of tools to quickly build easy-to-configure Spring applications without much of the tedious configuration of traditional Spring projects.

MongoDB is a database based on distributed file storage. Written in C++ language. Designed to provide scalable high-performance data storage solutions for WEB applications.

This article describes how to use Spring Boot to operate MongoDB, using Java code to insert data into MongoDB.

Set up the MongoDB environment locally as described in the first article of this tutorial:

One of the easiest tutorials to get started with MongoDB is environment setup.

Create a new Java project with the following contents for pom.xml:


      

<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 from</version>

<parent>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-parent</artifactId>

<version>. The 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 radar echoes captured</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>
Copy the code

This dependency provides the SpringBoot application with MongoDB:

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-data-mongodb</artifactId>

</dependency>
Copy the code

This dependent enables your Spring Boot application to support junit:

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-test</artifactId>

<scope>test</scope>

</dependency>
Copy the code

Create a.java file that ends with Tests in the SRC /main/test folder, in my case applicationTests.java:

Paste in the following code:

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(a) {
        bookRepository.deleteAll();
    }
    @Test
    public void test(a) throws Exception {
        bookRepository.save(new Book("1"."didi"."Jerry")); }}Copy the code

Line 27 creates a new Book object with id 1, name didi, and author Jerry. Then add it to MongoDB via 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);
}
Copy the code

After the JUnit unit test runs successfully,

In MongoDB Compass, you can see this inserted record successfully:

For more of Jerry’s original technical articles, please follow the public account “Wang Zixi” or scan the following QR code: