此版本仍在开发中,尚未被视为稳定版本。对于最新的稳定版本,请使用 Spring Data Couchbase 5.3.4! |
此版本仍在开发中,尚未被视为稳定版本。对于最新的稳定版本,请使用 Spring Data Couchbase 5.3.4! |
该模板提供对底层数据库的较低级别访问,还用作存储库的基础。
任何时候,当仓库对你的需求来说太高级时,模板很有可能为你提供很好的服务。请注意,
您始终可以通过 .AbstractCouchbaseConfiguration
支持的操作
可以通过上下文之外的 和 bean 访问该模板。
一旦你有了对它的引用,你就可以对它运行各种操作。
除了通过存储库之外,在模板中,您还需要始终指定要转换的目标实体类型。couchbaseTemplate
reactiveCouchbaseTemplate
这些模板使用 Fluent 样式的 API,允许您根据需要链接可选运算符。例如,这里是 如何存储用户,然后按其 ID 再次找到它:
// Create an Entity
User user = new User(UUID.randomUUID().toString(), "firstname", "lastname");
// Upsert it
couchbaseTemplate.upsertById(User.class).one(user);
// Retrieve it again
User found = couchbaseTemplate.findById(User.class).one(user.getId());
如果您想为操作使用自定义(默认情况下将使用注释中的持久性选项)持久性要求,您可以将其链接进来:@Document
upsert
User modified = couchbaseTemplate
.upsertById(User.class)
.withDurability(DurabilityLevel.MAJORITY)
.one(user);
以类似的方式,您可以执行 N1QL 操作:
final List<User> foundUsers = couchbaseTemplate
.findByQuery(User.class)
.consistentWith(QueryScanConsistency.REQUEST_PLUS)
.all();
子文档操作
Couchbase 支持子文档操作。本节记录了如何将其与 Spring Data Couchbase 一起使用。
子文档操作可能比完整文档操作(如 upsert 或 replace)更快、更高效,因为它们仅通过网络传输文档的访问部分。
Sub-Document 操作也是原子的,因为如果一个 Sub-Document 更改失败,那么所有 Sub-Document 更改都会失败,从而允许使用内置的并发控制对文档进行安全修改。
目前 Spring Data Couchbase 仅支持子文档更改(remove、upsert、replace 和 insert)。
更改操作会修改文档中的一个或多个路径。这些操作中最简单的是 upsert,它与 fulldoc 级别的 upsert 类似,将修改现有路径的值,或者在不存在时创建它:
以下示例将更新用户地址上的 city 字段,而不会传输任何其他用户文档数据。
User user = new User();
// id field on the base document id required
user.setId(ID);
user.setAddress(address);
couchbaseTemplate.mutateInById(User.class)
.withUpsertPaths("address.city")
.one(user);
执行多个子文档操作
可以同时对同一文档执行多个 Sub-Document 操作,从而允许您一次修改多个 Sub-Documents。当在单个 mutateIn 命令的上下文中提交多个操作时,服务器将使用相同版本的文档执行所有操作。
要执行多个 mutation 操作,可以使用方法链接。
couchbaseTemplate.mutateInById(User.class)
.withInsertPaths("roles", "subuser.firstname")
.withRemovePaths("address.city")
.withUpsertPaths("firstname")
.withReplacePaths("address.street")
.one(user);
Concurrent Modifications
Concurrent Sub-Document operations on different parts of a document will not conflict so by default the CAS value will be not be supplied when executing the mutations. If CAS is required then it can be provided like this:
User user = new User();
// id field on the base document id required
user.setId(ID);
// @Version field should have a value for CAS to be supplied
user.setVersion(cas);
user.setAddress(address);
couchbaseTemplate.mutateInById(User.class)
.withUpsertPaths("address.city")
.withCasProvided()
.one(user);
Exception Translation
The Spring Framework provides exception translation for a wide variety of database and mapping technologies.
This has traditionally been for JDBC and JPA.
Spring Data Couchbase extends this feature to Couchbase by providing an implementation of the interface.org.springframework.dao.support.PersistenceExceptionTranslator
The motivation behind mapping to Spring’s consistent data access exception hierarchy
is to let you write portable and descriptive exception handling code without resorting to coding against and handling specific Couchbase exceptions.
All of Spring’s data access exceptions are inherited from the
class, so you can be sure that you can catch all database-related exceptions within a single try-catch block.DataAccessException
ReactiveCouchbase
propagates exceptions as early as possible.
Exceptions that occur during the processing of the reactive sequence are emitted as error signals.