这期内容当中小编将会给大家带来有关Spring Boot中怎么操作mongoDB,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。新建一个Java项目,pom.xml的内容如
这期内容当中小编将会给大家带来有关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了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注编程网数据库频道。
--结束END--
本文标题: Spring Boot中怎么操作MongoDB
本文链接: https://www.lsjlt.com/news/56556.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-04-29
2024-04-29
2024-04-29
2024-04-29
2024-04-29
2024-04-29
2024-04-29
2024-04-29
2024-04-29
2024-04-28
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0