22 lines
811 B
Markdown
22 lines
811 B
Markdown
### 29.2.2. MongoDBTemplate
|
||
|
||
Spring Data Mongo提供了一个[MongoTemplate](http://docs.spring.io/spring-data/mongodb/docs/current/api/org/springframework/data/mongodb/core/MongoTemplate.html)类,它的设计和Spring的JdbcTemplate很相似。正如JdbcTemplate一样,Spring Boot会为你自动配置一个bean,你只需简单的注入它即可:
|
||
```java
|
||
import org.springframework.beans.factory.annotation.Autowired;
|
||
import org.springframework.data.mongodb.core.MongoTemplate;
|
||
import org.springframework.stereotype.Component;
|
||
|
||
@Component
|
||
public class MyBean {
|
||
|
||
private final MongoTemplate mongoTemplate;
|
||
|
||
@Autowired
|
||
public MyBean(MongoTemplate mongoTemplate) {
|
||
this.mongoTemplate = mongoTemplate;
|
||
}
|
||
// ...
|
||
}
|
||
```
|
||
具体参考MongoOperations Javadoc。
|