MongoDB是一种面向文档的数据库管理系统,他是一个关系数据库和非关系型数据库之间的产品。
下面开始springboot整合MongoDB。
github:https://github.com/fengqing11/springboot-mongodb
完整项目结构图:
创建项目,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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>xyz.fengqing11</groupId>
<artifactId>springboot-mongodb</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot-mongodb</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
配置MongoDB:
这里需要设置用户:
db.createUser({user:"tyl",pwd:"123",roles:[{role:"readWrite",db:"test"}]})
配置用户可以参考:https://www.jianshu.com/p/62736bff7e2e
spring.data.mongodb.authentication-database=admin
spring.data.mongodb.database=test
spring.data.mongodb.host=localhost
spring.data.mongodb.port=27017
spring.data.mongodb.username=tyl
spring.data.mongodb.password=123
创建实体类:
package xyz.fengqing11.springbootmongodb.pojo;
public class Book {
private Integer id;
private String name;
private String author;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
@Override
public String toString() {
return "Book{" +
"id=" + id +
", name='" + name + '\'' +
", author='" + author + '\'' +
'}';
}
}
拆包关键BookDao:
需要继承MongoRepository。
package xyz.fengqing11.springbootmongodb.dao;
import org.springframework.data.mongodb.repository.MongoRepository;
import xyz.fengqing11.springbootmongodb.pojo.Book;
import java.util.List;
public interface BookDao extends MongoRepository<Book, Integer> {
List<Book> findByAuthorContains(String author);
Book findByNameEquals(String name);
}
创建BookController:
简单起见省略了service层。
package xyz.fengqing11.springbootmongodb.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import xyz.fengqing11.springbootmongodb.dao.BookDao;
import xyz.fengqing11.springbootmongodb.pojo.Book;
import java.util.ArrayList;
import java.util.List;
@RestController
public class BookController {
@Autowired
BookDao bookDao;
@GetMapping("/test")
public void test() {
List<Book> books = new ArrayList<>();
Book b1 = new Book();
b1.setId(1);
b1.setName("奥啦i十九点111asdas");
b1.setAuthor("啊iOS的话111asd");
books.add(b1);
Book b2 = new Book();
b2.setId(2);
b2.setName("奥i十九点222222asdad");
b2.setAuthor("啊iOS的话222222222asdad");
books.add(b2);
bookDao.insert(books);
List<Book> books1 = bookDao.findByAuthorContains("啊");
System.out.println(books1);
Book book = bookDao.findByNameEquals("奥啦i十九点111asdas");
System.out.println(book);
}
}
运行结果:
-end-