此版本仍在开发中,尚未被视为稳定版本。对于最新的稳定版本,请使用 Spring Data MongoDB 4.4.4! |
开始
引导设置工作环境的一种简单方法是通过 start.spring.io 创建基于 Spring 的项目,或者在 Spring Tools 中创建 Spring 项目。
示例存储库
GitHub spring-data-examples 存储库包含几个示例,您可以下载并试用这些示例,以了解该库的工作原理。
世界您好
首先,您需要设置一个正在运行的 MongoDB 服务器。有关如何启动 MongoDB 实例的说明,请参阅 MongoDB 快速入门指南。
安装后,启动 MongoDB 通常只需运行以下命令即可:/bin/mongod
然后,您可以创建一个Person
类来保留:
package org.springframework.data.mongodb.example;
public class Person {
private String id;
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getId() {
return id;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
@Override
public String toString() {
return "Person [id=" + id + ", name=" + name + ", age=" + age + "]";
}
}
您还需要一个主应用程序来运行:
-
Imperative
-
Reactive
package org.springframework.data.mongodb.example;
import static org.springframework.data.mongodb.core.query.Criteria.*;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.MongoTemplate;
import com.mongodb.client.MongoClients;
public class MongoApplication {
public static void main(String[] args) throws Exception {
MongoOperations mongoOps = new MongoTemplate(MongoClients.create(), "database");
mongoOps.insert(new Person("Joe", 34));
System.out.println(mongoOps.query(Person.class).matching(where("name").is("Joe")).firstValue());
mongoOps.dropCollection("person");
}
}
package org.springframework.data.mongodb.example;
import static org.springframework.data.mongodb.core.query.Criteria.*;
import org.springframework.data.mongodb.core.ReactiveMongoOperations;
import org.springframework.data.mongodb.core.ReactiveMongoTemplate;
import com.mongodb.reactivestreams.client.MongoClients;
public class ReactiveMongoApplication {
public static void main(String[] args) throws Exception {
ReactiveMongoOperations mongoOps = new ReactiveMongoTemplate(MongoClients.create(), "database");
mongoOps.insert(new Person("Joe", 34))
.then(mongoOps.query(Person.class).matching(where("name").is("Joe")).first())
.doOnNext(System.out::println)
.block();
mongoOps.dropCollection("person").block();
}
}
When you run the main program, the preceding examples produce the following output:
10:01:32,265 DEBUG o.s.data.mongodb.core.MongoTemplate - insert Document containing fields: [_class, age, name] in collection: Person
10:01:32,765 DEBUG o.s.data.mongodb.core.MongoTemplate - findOne using query: { "name" : "Joe"} in db.collection: database.Person
Person [id=4ddbba3c0be56b7e1b210166, name=Joe, age=34]
10:01:32,984 DEBUG o.s.data.mongodb.core.MongoTemplate - Dropped collection [database.person]
Even in this simple example, there are few things to notice:
-
You can instantiate the central helper class of Spring Mongo, MongoTemplate
, by using the standard or reactive MongoClient
object and the name of the database to use.
-
The mapper works against standard POJO objects without the need for any additional metadata (though you can optionally provide that information. See here).
-
Conventions are used for handling the id
field, converting it to be an ObjectId
when stored in the database.
-
Mapping conventions can use field access. Notice that the Person
class has only getters.
-
If the constructor argument names match the field names of the stored document, they are used to instantiate the object