Cassandra Spring mvc - 如何传递值
Cassandra Spring mvc - how to pass value
我试试这个,工作正常。
@Autowired
private CassandraOperations cassandraOperations;
in my method
{
String cqlAll = "select * from customer where cust_id=123";
List<Customer> results = cassandraOperations.select(cqlAll, Customer.class);
for (Customer p : results)
{
System.out.println(p.getCust_id() + "-"+p.getCust_name()+"-"+p.getCust_password());
}
}
但我想将值传递给字符串,这样我就可以单独编写我的查询文件。
像这样,
String cqlAll = "select * from customer where cust_id=?";
objects[] obj = {123};
List<Customer> results = cassandraOperations.select(cqlAll, obj ,Customer.class);
for (Customer p : results)
{
System.out.println(p.getCust_id() + "-"+p.getCust_name()+"-"+
p.getCust_password());
}
CassandraOperation
select 方法也可以用 com.datastax.driver.core.querybuilder.Select
构建,正如我从 spring data cassandra documentation.
中看到的
我对 spring data cassandra 没有太多经验,因为我们直接使用 datastax java 驱动程序,但这应该适用于 select:
final Select select = QueryBuilder.select().all().from("customer")
.where(QueryBuilder.eq("cust_id", 123)).limit(200);
cassandraOperations.select(select ,Customer.class);
123
可以是从对象中提取的任何动态值。
更新 可以使用 CqlOperations:
CqlOperations cqlOperations = new CqlOperations(); //or autowire it
final Statement update = QueryBuilder.update("customer")
.with(QueryBuilder.set("email", "example@example.com"))
.where(QueryBuilder.eq("cust_id", 123));
cqlOperations.execute(update);
可以找到更多 QueryBuilder
个示例 here。
我试试这个,工作正常。
@Autowired
private CassandraOperations cassandraOperations;
in my method
{
String cqlAll = "select * from customer where cust_id=123";
List<Customer> results = cassandraOperations.select(cqlAll, Customer.class);
for (Customer p : results)
{
System.out.println(p.getCust_id() + "-"+p.getCust_name()+"-"+p.getCust_password());
}
}
但我想将值传递给字符串,这样我就可以单独编写我的查询文件。 像这样,
String cqlAll = "select * from customer where cust_id=?";
objects[] obj = {123};
List<Customer> results = cassandraOperations.select(cqlAll, obj ,Customer.class);
for (Customer p : results)
{
System.out.println(p.getCust_id() + "-"+p.getCust_name()+"-"+
p.getCust_password());
}
CassandraOperation
select 方法也可以用 com.datastax.driver.core.querybuilder.Select
构建,正如我从 spring data cassandra documentation.
我对 spring data cassandra 没有太多经验,因为我们直接使用 datastax java 驱动程序,但这应该适用于 select:
final Select select = QueryBuilder.select().all().from("customer")
.where(QueryBuilder.eq("cust_id", 123)).limit(200);
cassandraOperations.select(select ,Customer.class);
123
可以是从对象中提取的任何动态值。
更新 可以使用 CqlOperations:
CqlOperations cqlOperations = new CqlOperations(); //or autowire it
final Statement update = QueryBuilder.update("customer")
.with(QueryBuilder.set("email", "example@example.com"))
.where(QueryBuilder.eq("cust_id", 123));
cqlOperations.execute(update);
可以找到更多 QueryBuilder
个示例 here。