对于最新稳定版本,请使用 Spring Data Couchbase 6.0.4spring-doc.cadn.net.cn

模板与直接操作

该模板提供了对底层数据库的更低级别访问,同时也是存储库的基础。 当存储库对您来说过于高层时,模板通常能很好地满足您的需求。请注意, 您始终可以通过在AbstractCouchbaseConfiguration上暴露的 Bean 直接访问 SDK。spring-doc.cadn.net.cn

支持的操作

该模板可以通过上下文中的 couchbaseTemplatereactiveCouchbaseTemplate Bean 进行访问。 一旦获取到对该模板的引用,您就可以对其执行各种操作。 除了通过仓库(repository)之外,在模板中您始终需要指定要转换的目标实体类型。spring-doc.cadn.net.cn

这些模板使用流畅风格的 API,允许您根据需要链式调用可选操作符。例如,以下是存储用户并通过其 ID 再次查找它的方法:spring-doc.cadn.net.cn

示例 1. 流畅的模板访问
// 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());

如果您想为 upsert 操作使用自定义的持久性要求(默认情况下将使用来自 @Document 注解的持久性选项),您可以将其链接到:spring-doc.cadn.net.cn

示例 2. 具有持久性的 UPSERT
User modified = couchbaseTemplate
  .upsertById(User.class)
  .withDurability(DurabilityLevel.MAJORITY)
  .one(user);

以类似的方式,您可以执行 N1QL 操作:spring-doc.cadn.net.cn

示例 3. 模板上的 N1QL 查询
final List<User> foundUsers = couchbaseTemplate
  .findByQuery(User.class)
  .consistentWith(QueryScanConsistency.REQUEST_PLUS)
  .all();

子文档操作

Couchbase 支持 子文档操作。本节介绍如何在 Spring Data Couchbase 中使用它。spring-doc.cadn.net.cn

子文档操作可能比完整文档操作(如 upsert 或 replace)更快且网络效率更高,因为它们仅通过网络传输文档中被访问的部分。spring-doc.cadn.net.cn

子文档操作也是原子的,如果其中一个子文档修改失败,则所有修改都会失败,从而允许在具有内置并发控制的文档中进行安全修改。spring-doc.cadn.net.cn

当前 Spring Data Couchbase 仅支持子文档操作(删除、插入更新、替换和插入)。spring-doc.cadn.net.cn

变异操作会修改文档中的一个或多个路径。这些操作中最简单的是 upsert,它与全文档级别的 upsert 类似,要么修改现有路径的值,要么在路径不存在时创建它:spring-doc.cadn.net.cn

以下示例将在用户的地址上更新城市字段,而不会传输任何其他用户文档数据。spring-doc.cadn.net.cn

示例 4. 在模板上执行 MutateIn upsert
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);

执行多个子文档操作

多个子文档操作可以在同一文档上同时执行,允许您一次性修改多个子文档。当多个操作在单个 mutateIn 命令的上下文中提交时,服务器将使用相同版本的文档执行所有操作。spring-doc.cadn.net.cn

要执行多个突变操作,可以使用方法链。spring-doc.cadn.net.cn

示例 5. MutateIn 多操作
couchbaseTemplate.mutateInById(User.class)
    .withInsertPaths("roles", "subuser.firstname")
    .withRemovePaths("address.city")
    .withUpsertPaths("firstname")
    .withReplacePaths("address.street")
    .one(user);

并发修改

在不同部分对并发子文档进行操作时不会发生冲突,因此默认情况下在执行修改操作时将不提供 CAS 值。 如果需要 CAS,可以像这样提供:spring-doc.cadn.net.cn

示例 6. MutateIn 与 CAS
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);

异常转换

Spring Framework 为各种数据库和映射技术提供了异常转换功能。 这传统上适用于 JDBC 和 JPA。 Spring Data Couchbase 通过提供 org.springframework.dao.support.PersistenceExceptionTranslator 接口的实现,将该功能扩展到了 Couchbase。spring-doc.cadn.net.cn

将数据访问映射到 Spring 的一致数据访问异常层次结构背后的动机,是让您能够编写可移植且描述性强的异常处理代码,而无需针对并处理特定的 Couchbase 异常。 Spring 的所有数据访问异常都继承自 DataAccessException类,因此您可以确信能够在单个 try-catch 块中捕获所有与数据库相关的异常。spring-doc.cadn.net.cn

ReactiveCouchbase 尽可能早地传播异常。 在响应式序列处理过程中发生的异常会作为错误信号发出。spring-doc.cadn.net.cn