Spring Data MongoDB 中,MongoTemplate 类可以让你保存(save)、更新(update)和删除(delete)你的领域对象,并将这些领域对象映射到存储在 MongoDB 中的文档。下面就将通过一个简单的例子演示如何使用它。
创建一个 @Configuration 配置类去配置 MongoTemplate,如下:
package com.hxstrive.springdata.mongodb.config;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.core.MongoTemplate;
/**
* 配置 MongoTemplate
* @author hxstrive.com 2022/12/23
*/
@Configuration
public class AppConfig {
@Bean
public MongoClient mongoClient() {
return MongoClients.create("mongodb://localhost:27017");
}
@Bean
public MongoTemplate mongoTemplate(MongoClient mongoClient) {
return new MongoTemplate(mongoClient, "test");
}
}该实体对应 MongoDB 数据库中的对应集合,如下:
package com.hxstrive.springdata.mongodb.entity;
import lombok.Builder;
import lombok.Data;
import lombok.ToString;
/**
* 实体
* @author hxstrive.com 2022/12/23
*/
@Data
@Builder
@ToString
public class Person {
private String id;
private String name;
private int age;
}下面将演示使用两者方式去使用 MongoTemplate,如下:
package com.hxstrive.springdata.mongodb;
import com.hxstrive.springdata.mongodb.entity.Person;
import com.mongodb.client.MongoClients;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.SimpleMongoClientDbFactory;
import java.util.List;
import static org.springframework.data.mongodb.core.query.Criteria.where;
import static org.springframework.data.mongodb.core.query.Query.query;
import static org.springframework.data.mongodb.core.query.Update.update;
/**
* 使用 MongoTemplate 进行 CRUD 操作
* @author hxstrive.com 2022/12/23
*/
public class MongoApp {
public static void main(String[] args) {
// 注意:SimpleMongoClientDbFactory 已经过时,不推荐使用
MongoOperations mongoOps = new MongoTemplate(
new SimpleMongoClientDbFactory(MongoClients.create(), "database"));
Person p = Person.builder().name("Joe").age(34).build();
// 将对象插入到数据库中
mongoOps.insert(p);
System.out.println("Insert: " + p);
// 查找数据
p = mongoOps.findById(p.getId(), Person.class);
System.out.println("Found: " + p);
// 根据条件更新数据
mongoOps.updateFirst(query(where("name").is("Joe")), update("age", 35), Person.class);
p = mongoOps.findOne(query(where("name").is("Joe")), Person.class);
System.out.println("Updated: " + p);
// 删除数据
mongoOps.remove(p);
// 检查删除数据是否成功
List<Person> people = mongoOps.findAll(Person.class);
System.out.println("Number of people = : " + people.size());
// 删除集合
mongoOps.dropCollection(Person.class);
}
}运行代码,输出如下:
Insert: Person(id=63a536dc0f05bb38396c7e05, name=Joe, age=34) Found: Person(id=63a536dc0f05bb38396c7e05, name=Joe, age=34) Updated: Person(id=63a536dc0f05bb38396c7e05, name=Joe, age=35) Number of people = : 0
package com.hxstrive.springdata.mongodb;
import com.hxstrive.springdata.mongodb.entity.Person;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.mongodb.core.MongoTemplate;
import java.util.List;
import static org.springframework.data.mongodb.core.query.Criteria.where;
import static org.springframework.data.mongodb.core.query.Query.query;
import static org.springframework.data.mongodb.core.query.Update.update;
@SpringBootTest
class MongoAppTest {
@Autowired
private MongoTemplate mongoTemplate;
@Test
void contextLoads() {
Person p = Person.builder().name("Joe").age(34).build();
// 将对象插入到数据库中
mongoTemplate.insert(p);
System.out.println("Insert: " + p);
// 查找数据
p = mongoTemplate.findById(p.getId(), Person.class);
System.out.println("Found: " + p);
// 根据条件更新数据
mongoTemplate.updateFirst(query(where("name").is("Joe")), update("age", 35), Person.class);
p = mongoTemplate.findOne(query(where("name").is("Joe")), Person.class);
System.out.println("Updated: " + p);
// 删除数据
mongoTemplate.remove(p);
// 检查删除数据是否成功
List<Person> people = mongoTemplate.findAll(Person.class);
System.out.println("Number of people = : " + people.size());
// 删除集合
mongoTemplate.dropCollection(Person.class);
}
}运行代码,输出如下:
Insert: Person(id=63a538a1333fd62a835a7c58, name=Joe, age=34) Found: Person(id=63a538a1333fd62a835a7c58, name=Joe, age=34) Updated: Person(id=63a538a1333fd62a835a7c58, name=Joe, age=35) Number of people = : 0