对于最新的稳定版本,请使用 Spring Data Couchbase 5.3.1Spring中文文档

对于最新的稳定版本,请使用 Spring Data Couchbase 5.3.1Spring中文文档

本章简要介绍了 4.x 中引入的主要更改,并简要概述了迁移时要考虑的事项。Spring中文文档

请注意,最低 Couchbase Server 版本已隐式提高到 5.5 及更高版本,我们建议至少运行 6.0.x。Spring中文文档

配置

由于主要目标是从 Java SDK 2 迁移到 3,因此配置已更改以适应新的 SDK,并且从长远来看,还可以为范围和集合做好准备(但仍然可以在没有集合支持的情况下使用)。Spring中文文档

XML 配置支持已被删除,因此仅支持基于 java/annotation 的配置。

您的配置仍必须扩展 ,但由于 RBAC(基于角色的访问控制)现在是必需的,因此需要覆盖不同的属性才能进行配置:、 和 。如果要使用非默认作用域(可选),则可以重写该方法。请注意,如果要使用基于证书的身份验证或需要自定义密码身份验证,则可以重写该方法以执行此任务。AbstractCouchbaseConfigurationgetConnectionStringgetUserNamegetPasswordgetBucketNamegetScopeNameauthenticatorSpring中文文档

新 SDK 仍具有用于配置它的环境,因此您可以重写该方法并在需要时提供自定义配置。configureEnvironmentSpring中文文档

有关详细信息,请参阅安装和配置Spring中文文档

Spring Boot 版本兼容性

Spring Boot 2.3.x 或更高版本依赖于 Spring Data Couchbase 4.x。早期版本的 Couchbase 不可用,因为 SDK 2 和 3 不能位于同一类路径上。Spring中文文档

XML 配置支持已被删除,因此仅支持基于 java/annotation 的配置。

实体

处理实体的方式没有改变,尽管由于 SDK 现在不再提供注解,因此仅支持与 Spring-Data 相关的注解。Spring中文文档

具体说来:Spring中文文档

  • com.couchbase.client.java.repository.annotation.Id成为import org.springframework.data.annotation.IdSpring中文文档

  • com.couchbase.client.java.repository.annotation.Field成为import org.springframework.data.couchbase.core.mapping.FieldSpring中文文档

注释保持不变。org.springframework.data.couchbase.core.mapping.DocumentSpring中文文档

有关更多信息,请参见对实体进行建模Spring中文文档

自动索引管理

自动索引管理已重新设计,以允许更灵活的索引。 引入了新的注释,并删除了 等旧注释。@ViewIndexed@N1qlSecondaryIndexed@N1qlPrimaryIndexedSpring中文文档

有关详细信息,请参阅自动索引管理Spring中文文档

Template 和 ReactiveTemplate

由于 Couchbase SDK 3 删除了对 的支持,而是添加了对 的支持,因此 和 都可以直接从 访问。RxJavaReactorcouchbaseTemplatereactiveCouchbaseTemplateAbstractCouchbaseConfigurationSpring中文文档

该模板已经过彻底检修,因此它现在使用流畅的 API 进行配置,而不是许多方法重载。这样做的好处是,将来我们能够扩展功能,而不必引入越来越多的重载,从而使导航变得复杂。Spring中文文档

下表描述了 3.x 中的方法名称,并将它们与 4.x 中的等效项进行了比较:Spring中文文档

表 1.模板方法比较
SDC 3.x SDC 4.x

Spring中文文档

upsertByIdSpring中文文档

插入Spring中文文档

insertByIdSpring中文文档

更新Spring中文文档

替换ByIdSpring中文文档

findByIdSpring中文文档

findByIdSpring中文文档

findByViewSpring中文文档

(已删除)Spring中文文档

findBySpatialViewSpring中文文档

(已删除)Spring中文文档

findByN1QLSpring中文文档

findByQuerySpring中文文档

findByN1QLProjectionSpring中文文档

findByQuerySpring中文文档

查询N1QLSpring中文文档

(直接调用 SDK)Spring中文文档

存在Spring中文文档

existsByIdSpring中文文档

删除Spring中文文档

removeByIdSpring中文文档

执行Spring中文文档

(直接调用 SDK)Spring中文文档

此外,还添加了以下在 3.x 中不可用的方法:Spring中文文档

表 2.4.x 中的模板添加
名字 描述

removeByQuerySpring中文文档

允许通过 N1QL 查询删除实体Spring中文文档

findByAnalytics(查找分析)Spring中文文档

通过分析服务执行查找Spring中文文档

findFromReplicasByIdSpring中文文档

与 findById 类似,但考虑了副本Spring中文文档

我们尝试将 API 与底层 SDK 语义更紧密地统一和一致,以便它们更易于关联和导航。Spring中文文档

有关详细信息,请参阅模板和直接操作Spring中文文档

表 1.模板方法比较
SDC 3.x SDC 4.x

Spring中文文档

upsertByIdSpring中文文档

插入Spring中文文档

insertByIdSpring中文文档

更新Spring中文文档

替换ByIdSpring中文文档

findByIdSpring中文文档

findByIdSpring中文文档

findByViewSpring中文文档

(已删除)Spring中文文档

findBySpatialViewSpring中文文档

(已删除)Spring中文文档

findByN1QLSpring中文文档

findByQuerySpring中文文档

findByN1QLProjectionSpring中文文档

findByQuerySpring中文文档

查询N1QLSpring中文文档

(直接调用 SDK)Spring中文文档

存在Spring中文文档

existsByIdSpring中文文档

删除Spring中文文档

removeByIdSpring中文文档

执行Spring中文文档

(直接调用 SDK)Spring中文文档

表 2.4.x 中的模板添加
名字 描述

removeByQuerySpring中文文档

允许通过 N1QL 查询删除实体Spring中文文档

findByAnalytics(查找分析)Spring中文文档

通过分析服务执行查找Spring中文文档

findFromReplicasByIdSpring中文文档

与 findById 类似,但考虑了副本Spring中文文档

存储库和查询

  • org.springframework.data.couchbase.core.query.Query成为org.springframework.data.couchbase.repository.QuerySpring中文文档

  • org.springframework.data.couchbase.repository.ReactiveCouchbaseSortingRepository已被删除。考虑扩展或ReactiveSortingRepositoryReactiveCouchbaseRepositorySpring中文文档

  • org.springframework.data.couchbase.repository.CouchbasePagingAndSortingRepository已被删除。考虑扩展或PagingAndSortingRepositoryCouchbaseRepositorySpring中文文档

删除了对视图的支持,默认情况下,N1QL 查询是所有自定义存储库方法以及内置存储库方法的一等公民。

与以前的版本相比,查询派生的工作方式的行为本身没有变化。如果您遇到任何过去有效但现在不再有效的查询,请告诉我们。Spring中文文档

可以通过新的注释覆盖 N1QL 查询的默认扫描一致性。ScanConsistencySpring中文文档

该方法也已被删除。您仍然可以通过以下类访问本机 Java SDK 中的所有方法:getCouchbaseOperations()CouchbaseTemplateClusterSpring中文文档

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.couchbase.core.CouchbaseTemplate;
import org.springframework.stereotype.Service;
import com.couchbase.client.java.Cluster;

@Service
public class MyService {

    @Autowired
    private CouchbaseTemplate couchbaseTemplate;

    @Autowired
    private Cluster cluster;
}

有关详细信息,请参阅 Couchbase 存储库Spring中文文档

删除了对视图的支持,默认情况下,N1QL 查询是所有自定义存储库方法以及内置存储库方法的一等公民。

全文搜索 (FTS)

FTS API 已简化,现在可以通过以下类访问:ClusterSpring中文文档

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.couchbase.client.java.Cluster;
import com.couchbase.client.java.search.result.SearchResult;
import com.couchbase.client.java.search.result.SearchRow;
import com.couchbase.client.core.error.CouchbaseException;

@Service
public class MyService {

    @Autowired
    private Cluster cluster;

    public void myMethod() {
        try {
          final SearchResult result = cluster
            .searchQuery("index", SearchQuery.queryString("query"));

          for (SearchRow row : result.rows()) {
            System.out.println("Found row: " + row);
          }

          System.out.println("Reported total rows: "
            + result.metaData().metrics().totalRows());
        } catch (CouchbaseException ex) {
          ex.printStackTrace();
        }
    }
}