In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
In this issue, the editor will bring you about how to operate MongoDB in Spring Boot. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.
Create a new Java project. The content of pom.xml is as follows:
4.0.0org.springframeworkgs-rest-service0.1.0org.springframework.bootspring-boot-starter-parent2.0.3.RELEASEorg.springframework.bootspring-boot-starter-weborg.mongodbmongodb-driver3.6.4org.springframework.bootspring-boot-starter-testtestcom.jayway.jsonpathjson-pathtestorg.springframework.bootspring-boot-starter-data-mongodb1.8org.springframework.bootspring-boot-maven-pluginspring-releases https://repo.spring.io/libs-releasespring-releaseshttps://repo.spring.io/libs-release
The function of this dependency is to provide SpringBoot applications with the function of operating MongoDB:
Org.springframework.bootspring-boot-starter-data-mongodb
This dependent enables your Spring Boot application to support junit:
Org.springframework.bootspring-boot-starter-testtest
Create a .java file that ends in Tests under 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");}}
In the 27th line of code, a new Book object is created, the id is 1, the name is didi, and the author is Jerry. And 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, BookRepositoryCustom {public Optional findByName (String name);}
After the JUnit unit test runs successfully
Successfully see this inserted record in MongoDB Compass:
The above is the editor for you to share how to operate MongoDB in the Spring Boot, if you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.