我正在尝试将 Spring Boot 应用程序从 mssql 迁移到 CosmosDb。
我们有以下存储库实现,使用org.springframework.data.jpa.domain.Specification;
它可以根据用户请求的任何(一个或多个)列获取数据。
static Specification<User> isMatching(final Map<String, String> parameters) {
return new Specification<User>() {
private static final long serialVersionUID = 1L;
public Predicate toPredicate(Root<User> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
List<Predicate> predicates = new ArrayList<>();
parameters.entrySet().forEach(e -> {
predicates.add(builder.and(builder.equal(root.get(e.getKey()), e.getValue())));
});
return builder.and(predicates.toArray(new Predicate[predicates.size()]));
}
};
}
然后我们将其称为如下:
repo.findAll(UserRepo.isMatching(parameters));
我不得不从类路径中删除 spring-boot-starter-data-jpa,因为它不支持 CosmosDb。我在类路径中使用下面的 spring 和 spring 数据 cosmos 引用,因为这是唯一对我有用的组合。
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.3.RELEASE</version>
<relativePath />
</parent>
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-spring-data-cosmos</artifactId>
<version>3.0.0</version>
</dependency>
我真的不知道如何为 CosmosDb 实现上述规范逻辑。我非常感谢任何有关此的指导。提前致谢!