MongoDB Support
Spring 对 MongoDB 的支持具有广泛的功能:
Spring Data support for MongoDB contains a wide range of features:
-
Spring configuration support with Java-based
@Configuration
classes or an XML namespace for a Mongo driver instance and replica sets. -
MongoTemplate
helper class that increases productivity when performing common Mongo operations. Includes integrated object mapping between documents and POJOs. -
Exception translation into Spring’s portable Data Access Exception hierarchy.
-
Feature-rich Object Mapping integrated with Spring’s Conversion Service.
-
Annotation-based mapping metadata that is extensible to support other metadata formats.
-
Automatic implementation of Repository interfaces, including support for custom query methods.
-
QueryDSL integration to support type-safe queries.
对于大多数任务,您应使用 MongoTemplate
或存储库支持,它们均利用丰富的映射功能。MongoTemplate
是用于寻找诸如递增计数器或临时 CRUD 操作之类的功能性的地方。MongoTemplate
还提供回调方法,以便您可以轻松获得低级 API 伪影(例如 com.mongodb.client.MongoDatabase
),以直接与 MongoDB 通信。对各种 API 伪影进行命名约定的目的是将它们复制到基本 MongoDB Java 驱动程序中,以便您可以轻松将现有知识映射到 Spring API。
For most tasks, you should use MongoTemplate
or the Repository support, which both leverage the rich mapping functionality.
MongoTemplate
is the place to look for accessing functionality such as incrementing counters or ad-hoc CRUD operations.
MongoTemplate
also provides callback methods so that it is easy for you to get the low-level API artifacts, such as com.mongodb.client.MongoDatabase
, to communicate directly with MongoDB.
The goal with naming conventions on various API artifacts is to copy those in the base MongoDB Java driver so you can easily map your existing knowledge onto the Spring APIs.