# 30.2.2. MongoDBTemplate

[Spring Data MongoDB](https://projects.spring.io/spring-data-mongodb/)提供了一个[MongoTemplate](https://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](https://docs.spring.io/spring-data/mongodb/docs/current/api/org/springframework/data/mongodb/core/MongoOperations.html)。
