这期内容当中小编将会给大家带来有关Spring Boot中怎么操作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>
这个dependent能让您的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); }
这个JUnit单元测试运行成功后,
在MongoDB Compass里成功看到这条插入的记录:
上述就是小编为大家分享的Spring Boot中怎么操作MongoDB了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注亿速云行业资讯频道。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。