不确定 2.0
版本是否有任何变化,但是当使用 Spring Data Cassandra 2.0.5 时,这段代码将不再适用于 Spring Boot 测试:
@Autowired
CqlTemplate cqlTemplate;
这是在教程中介绍的,如何获得读取 Javadoc 的 CqlOperations
(接口(interface) CqlTemplate
实现)并不是很简单或 Reference doc .
在 SpringBootTest
中使用该注释将导致:
UnsatisfiedDependencyException: Error creating bean with name
'com.alertavert.api.TriggerRestControllerTest': Unsatisfied
dependency expressed through field 'cqlTemplate';
最佳答案
解决方案非常简单,但我将其发布在这里是因为我浪费了一些时间在谷歌上搜索它,但没有产生任何有值(value)的东西(而且很多东西只会让事情变得更糟)。
您可以 Autowiring CassandraTemplate
的 bean,然后使用它来获取 CassandraOperation
对象以执行 CQL 查询:
@Autowired
private CassandraTemplate cassandraTemplate;
在你的测试中:
UUID id = UUID.randomUUID();
Insert insertBuilder = QueryBuilder.insertInto("triggers")
.value("trigger_id", id)
.value("serializedpb", ByteBuffer.wrap("Definitely not a Protobuf".getBytes()));
cassandraTemplate.getCqlOperations().execute(insertBuilder);
希望这可以节省一些人的时间!
关于java - Spring Data Cassandra - 使用 CqlOperations 运行任意 CQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49343123/