对于以下代码,存储库中的获取和更新操作可以正常工作。但保存操作不会将数据持久化到表中。如果我自己实现存储库,它工作正常。将其替换为扩展 ReactiveCrudRepository 的接口后,开始出现此问题。我错过了什么吗?
@SpringBootApplication
public class ReactiveSqlApplication {
public static void main(String[] args) {
SpringApplication.run(ReactiveSqlApplication.class, args);
}
}
@Data
@AllArgsConstructor
@NoArgsConstructor
@Table("store")
class Store {
@Id
private String id;
private String name;
private String description;
}
interface StoreRepository extends ReactiveCrudRepository<Store, String> {
}
@Configuration
@EnableR2dbcRepositories
class R2dbcConfiguration extends AbstractR2dbcConfiguration {
private final ConnectionFactory connectionFactory;
R2dbcConfiguration(ConnectionFactory connectionFactory) {
this.connectionFactory = connectionFactory;
}
@Override
public ConnectionFactory connectionFactory() {
return this.connectionFactory;
}
}
@Configuration
class ConnectionFactoryConfiguration {
@Bean
ConnectionFactory connectionFactory() {
PostgresqlConnectionConfiguration config = PostgresqlConnectionConfiguration.builder()
.host("localhost")
.port(5433)
.database("testdb")
.username("postgres")
.password("root")
.build();
return new PostgresqlConnectionFactory(config);
}
}
代码测试如下:
@SpringBootTest
@RunWith(SpringRunner.class)
public class StoreRepositoryTest {
@Autowired
private StoreRepository repository;
@Test
public void all() {
Flux<Store> storeFlux = Flux.just(new Store("1", "a1", "a1"), new Store("2", "a2", "a2"))
.flatMap(store -> repository.save(store));
StepVerifier
.create(storeFlux)
.expectNextCount(2)
.verifyComplete();
Flux<Store> all = repository.findAll();
StepVerifier
.create(all)
.expectNextCount(2)
.verifyComplete();
}
}