最主要的引入springboot对mongoDB的支org.springframework.boot:spring-boot-starter-data-mongodb
implementation 'org.springframework.boot:spring-boot-starter:2.3.0.RELEASE'
implementation 'org.springframework.boot:spring-boot-starter-web:2.3.0.RELEASE'
implementation 'org.springframework.boot:spring-boot-starter-data-mongodb'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
这里我们使用yml文件的配置方式,具体配置如下
防止8080端口被占用,这里我们用8084
spring:
data:
mongodb:
uri: mongodb://localhost:27017/db_todo
server:
port: 8084
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class TodoListVueMongoApplication {
public static void main(String[] args) {
SpringApplication.run(TodoListVueMongoApplication.class, args);
}
}
import org.springframework.data.mongodb.core.mapping.Document;
import java.io.Serializable;
import java.util.Objects;
@Document("t_todo")
public class Todo implements Serializable {
private Integer todoId;
private String content;
private Boolean status;
public Todo() {
}
public Todo(Integer todoId, String content, Boolean status) {
this.todoId = todoId;
this.content = content;
this.status = status;
}
public Integer getId() {
return todoId;
}
public void setId(Integer id) {
this.todoId = id;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public Boolean getStatus() {
return status;
}
public void setStatus(Boolean status) {
this.status = status;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Todo)) return false;
Todo todo = (Todo) o;
return Objects.equals(getId(), todo.getId()) &&
Objects.equals(getContent(), todo.getContent()) &&
Objects.equals(getStatus(), todo.getStatus());
}
@Override
public int hashCode() {
return Objects.hash(getId(), getContent(), getStatus());
}
}
Spring Data Mongo提供了一个MongoTemplate类,它的设计和Spring的JdbcTemplate很相似。跟JdbcTemplate一样,Spring Boot会为你自动配置一个bean,你只需简单的注入即可
@SpringBootTest
class TodoListVueMongoApplicationTests {
@Autowired
MongoTemplate mongoTemplate;
@Test
void findAllTodos() {
List<Todo> actualTodos = mongoTemplate.findAll(Todo.class);
assertEquals(actualTodos.size(),1);
}
query.addCriteria()方法中通过where…is/in/exists…等操作添加查询条件
@Test
void findTodoById() {
String id = "1";
Query query = new Query();
query.addCriteria(Criteria.where("todo_id").is(id));
Todo actualTodo = mongoTemplate.findOne(query, Todo.class);
assert actualTodo != null;
assertEquals("eat",actualTodo.getContent());
}
@Test
void insertTodo() {
Todo todo = new Todo(2, "sleep", true);
Todo actualTodo = mongoTemplate.save(todo);
assert actualTodo != null;
assertEquals("sleep",actualTodo.getContent());
}
updateFirst方法更新第一条数据,传入一个Query查询的sql,和一个Update更新的操作,最后通过assertEquals判断是否更新成功
@Test
void updateTodo() {
String id = "1";
Query query = new Query();
query.addCriteria(Criteria.where("todo_id").is(id));
Update update = new Update();
update.set("content","game");
mongoTemplate.updateFirst(query,update,Todo.class);
Todo actualTodo = mongoTemplate.findOne(query, Todo.class);
assert actualTodo != null;
assertEquals("game",actualTodo.getContent());
}
普通的更新数据的方法可以使用以下方式:
todo=mongoTemplate.findOne(Query.query(Criteria.where("todo_id").is(id)), Todo.class);
user.setContent("game");
mongoTemplate.save(todo, "todo");
一次更新全部数据可以使用以下方式
query.addCriteria(Criteria.where("status").is(false));
Update update = new Update();
update.set("content","game");
mongoTemplate.updateMulti(query,update,Todo.class);
@Test
void deleteTodo() {
Todo todo = new Todo(2, "sleep", true);
mongoTemplate.remove(todo);
Query query = new Query();
query.addCriteria(Criteria.where("todoId").is("2"));
Todo actualTodo = mongoTemplate.findOne(query, Todo.class);
assertNull(actualTodo);
}
总结
通过以上的配置已经完成springboot与mongoDB集成环境的初步搭建,MongoDB在springboot中的使用不仅于此,还有更多的功能和更优雅的使用方式等待着我们去发掘。