我正在做一些关于spring-data-cassandra-reactive
的研究,但似乎我找不到或找不到合适的文档。
根据文档,您可以创建一个用@Repository
进行注释的interface
,然后使用注释创建将检索所需数据的自定义查询。代码看起来像这样:
@Repository
public interface UserRepository
extends ReactiveCassandraRepository<UserEntity, UUID>
{
@Query("SELECT u FROM UserEntity u WHERE username = :user ALLOW FILTERING")
Mono<UserEntity> findUserEntityByUsername(@Param("user") String user);
/**
* This is just to illustrate/simulate some custom/advanced logic that cannot be
* done via @Query()
*/
default Mono<UserEntity> saveWithBase64EncodedPassword(UserEntity entity)
{
String encodedPassword = Base64.getEncoder().encodeToString(entity.getPassword().getBytes());
entity.updatePassword(encodedPassword);
return this.save(entity);
}
}
@Table(UserEntity.TABLE_NAME)
public class UserEntity
{
public final static String TABLE_NAME = "users";
@PrimaryKeyColumn(name = "uuid", ordinal = 0, type = PrimaryKeyType.PARTITIONED)
@CassandraType(type = CassandraType.Name.UUID)
@Column
private UUID id;
@Column
@CassandraType(type = CassandraType.Name.VARCHAR)
private String username;
@Column
@CassandraType(type = CassandraType.Name.VARCHAR)
private String password;
public UserEntity()
{
this.id = UUID.randomUUID();
}
public UserEntity(String username, String password)
{
this.id = UUID.randomUUID();
this.username = username;
this.password = password;
}
public UUID getId()
{
return id;
}
public void setId(UUID id)
{
this.id = id;
}
public String getUsername()
{
return username;
}
public void setUsername(String username)
{
this.username = username;
}
public String getPassword()
{
return password;
}
public void setPassword(String password)
{
this.password = password;
}
public void updatePassword(String newPassword)
{
this.password = newPassword;
}
}
依赖项:
plugins {
id("org.springframework.boot") version "2.6.6"
id("io.spring.dependency-management") version "1.0.11.RELEASE"
}
dependencies {
// Embedded Cassandra Server - used for testing.
implementation("com.github.nosan:embedded-cassandra-spring-boot-starter:4.1.0")
// Spring Data Cassandra Dependencies
annotationProcessor("org.springframework.boot:spring-boot-configuration-processor")
implementation("org.springframework.boot:spring-boot-starter-actuator")
implementation("org.springframework.boot:spring-boot-starter-data-cassandra-reactive")
testImplementation("org.springframework.boot:spring-boot-starter-test") {
exclude("org.junit.vintage:junit-vintage-engine")
exclude("com.vaadin.external.google:android-json")
}
testImplementation("io.projectreactor:reactor-test")
// /Spring Data Cassandra Dependencies
}
到目前为止,这一切都很好。然而,我正试图弄清楚如何切换到实现接口,以便能够使用@Autowire
(即,在本例中,代码自动连接org.springframework.security.crypto.password.PasswordEncoder
(
浏览spring-data-cassandra
jar中的代码时,我注意到有一个名为org.springframework.data.cassandra.repository.support.SimpleReactiveCassandraRepository
的类,您可以从中进行扩展。它已经具备了您需要实现的大多数常见功能,这就是为什么它看起来是extend
的完美候选者。
问题来了——它需要CassandraEntityInformation<T, ID> metadata
。我似乎找不到这是从哪里来的,也找不到它应该如何自动连接,我觉得我可能错过了什么,或者可能是一种依赖。
有什么想法吗?
SimpleReactiveCassandraRepository
是一个很好的类,因为它可以让您访问ReactiveCassandraOperations
,也可以访问CqlSession
。这是进行细粒度操作的好方法。(LWT,细菌(
你是对的,不同的类是自动连接的。这是一个示例代码:
@Repository
public class OwnerReactiveCassandraRepository extends SimpleReactiveCassandraRepository<OwnerEntitySpring, UUID> {
protected final CqlSession cqlSession;
protected final ReactiveCassandraOperations reactiveCassandraTemplate;
@SuppressWarnings("unchecked")
public OwnerReactiveCassandraRepository(CqlSession cqlSession, ReactiveCassandraOperations ops) {
super(new MappingCassandraEntityInformation<OwnerEntitySpring, UUID>(
(CassandraPersistentEntity<OwnerEntitySpring>) ops.getConverter().getMappingContext()
.getRequiredPersistentEntity(OwnerEntitySpring.class), ops.getConverter()), ops);
this.cqlSession = cqlSession;
this.reactiveCassandraTemplate = ops;
}
public Flux<OwnerEntitySpring> searchByOwnerName(String ownerLastName) {
return reactiveCassandraTemplate.getReactiveCqlOperations()
.query(SimpleStatement
.builder("SELECT * FROM " + TABLE_NAME + " WHERE " + COLUMN_LASTNAME + "=?")
.addPositionalValues(ownerLastName)
.build(), (row, rownum) -> new OwnerEntitySpring(row));
}
TLDR;我用Spring data Reactive实现了Spring PetClinic,您可以在这里访问完整的代码。这是一个关于代码的逐步研讨会。你可能想特别看看这个文件夹
在该项目中,您会发现与drivers only
、与CassandraRepositories
和与SimpleCassandraRepositories
反应。(检查TEST文件夹(