对于最新的稳定版本,请使用 Spring Data Couchbase 5.3.1! |
对于最新的稳定版本,请使用 Spring Data Couchbase 5.3.1! |
本章简要介绍了 4.x 中引入的主要更改,并简要概述了迁移时要考虑的事项。
请注意,最低 Couchbase Server 版本已隐式提高到 5.5 及更高版本,我们建议至少运行 6.0.x。
配置
由于主要目标是从 Java SDK 2 迁移到 3,因此配置已更改以适应新的 SDK,并且从长远来看,还可以为范围和集合做好准备(但仍然可以在没有集合支持的情况下使用)。
XML 配置支持已被删除,因此仅支持基于 java/annotation 的配置。 |
您的配置仍必须扩展 ,但由于 RBAC(基于角色的访问控制)现在是必需的,因此需要覆盖不同的属性才能进行配置:、 和 。如果要使用非默认作用域(可选),则可以重写该方法。请注意,如果要使用基于证书的身份验证或需要自定义密码身份验证,则可以重写该方法以执行此任务。AbstractCouchbaseConfiguration
getConnectionString
getUserName
getPassword
getBucketName
getScopeName
authenticator
新 SDK 仍具有用于配置它的环境,因此您可以重写该方法并在需要时提供自定义配置。configureEnvironment
有关详细信息,请参阅安装和配置。
XML 配置支持已被删除,因此仅支持基于 java/annotation 的配置。 |
实体
处理实体的方式没有改变,尽管由于 SDK 现在不再提供注解,因此仅支持与 Spring-Data 相关的注解。
具体说来:
-
com.couchbase.client.java.repository.annotation.Id
成为import org.springframework.data.annotation.Id
-
com.couchbase.client.java.repository.annotation.Field
成为import org.springframework.data.couchbase.core.mapping.Field
注释保持不变。org.springframework.data.couchbase.core.mapping.Document
有关更多信息,请参见对实体进行建模。
自动索引管理
自动索引管理已重新设计,以允许更灵活的索引。
引入了新的注释,并删除了 等旧注释。@ViewIndexed
@N1qlSecondaryIndexed
@N1qlPrimaryIndexed
有关详细信息,请参阅自动索引管理。
Template 和 ReactiveTemplate
由于 Couchbase SDK 3 删除了对 的支持,而是添加了对 的支持,因此 和 都可以直接从 访问。RxJava
Reactor
couchbaseTemplate
reactiveCouchbaseTemplate
AbstractCouchbaseConfiguration
该模板已经过彻底检修,因此它现在使用流畅的 API 进行配置,而不是许多方法重载。这样做的好处是,将来我们能够扩展功能,而不必引入越来越多的重载,从而使导航变得复杂。
下表描述了 3.x 中的方法名称,并将它们与 4.x 中的等效项进行了比较:
SDC 3.x | SDC 4.x |
---|---|
救 |
upsertById |
插入 |
insertById |
更新 |
替换ById |
findById |
findById |
findByView |
(已删除) |
findBySpatialView |
(已删除) |
findByN1QL |
findByQuery |
findByN1QLProjection |
findByQuery |
查询N1QL |
(直接调用 SDK) |
存在 |
existsById |
删除 |
removeById |
执行 |
(直接调用 SDK) |
此外,还添加了以下在 3.x 中不可用的方法:
名字 | 描述 |
---|---|
removeByQuery |
允许通过 N1QL 查询删除实体 |
findByAnalytics(查找分析) |
通过分析服务执行查找 |
findFromReplicasById |
与 findById 类似,但考虑了副本 |
我们尝试将 API 与底层 SDK 语义更紧密地统一和一致,以便它们更易于关联和导航。
有关详细信息,请参阅模板和直接操作。
SDC 3.x | SDC 4.x |
---|---|
救 |
upsertById |
插入 |
insertById |
更新 |
替换ById |
findById |
findById |
findByView |
(已删除) |
findBySpatialView |
(已删除) |
findByN1QL |
findByQuery |
findByN1QLProjection |
findByQuery |
查询N1QL |
(直接调用 SDK) |
存在 |
existsById |
删除 |
removeById |
执行 |
(直接调用 SDK) |
名字 | 描述 |
---|---|
removeByQuery |
允许通过 N1QL 查询删除实体 |
findByAnalytics(查找分析) |
通过分析服务执行查找 |
findFromReplicasById |
与 findById 类似,但考虑了副本 |
存储库和查询
-
org.springframework.data.couchbase.core.query.Query
成为org.springframework.data.couchbase.repository.Query
-
org.springframework.data.couchbase.repository.ReactiveCouchbaseSortingRepository
已被删除。考虑扩展或ReactiveSortingRepository
ReactiveCouchbaseRepository
-
org.springframework.data.couchbase.repository.CouchbasePagingAndSortingRepository
已被删除。考虑扩展或PagingAndSortingRepository
CouchbaseRepository
删除了对视图的支持,默认情况下,N1QL 查询是所有自定义存储库方法以及内置存储库方法的一等公民。 |
与以前的版本相比,查询派生的工作方式的行为本身没有变化。如果您遇到任何过去有效但现在不再有效的查询,请告诉我们。
可以通过新的注释覆盖 N1QL 查询的默认扫描一致性。ScanConsistency
该方法也已被删除。您仍然可以通过以下类访问本机 Java SDK 中的所有方法:getCouchbaseOperations()
CouchbaseTemplate
Cluster
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 存储库。
删除了对视图的支持,默认情况下,N1QL 查询是所有自定义存储库方法以及内置存储库方法的一等公民。 |
全文搜索 (FTS)
FTS API 已简化,现在可以通过以下类访问:Cluster
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();
}
}
}
有关详细信息,请参阅 FTS 文档。