您的位置:首页 > 编程语言 > Java开发

五分钟学会使用spring-data-cassandra快速实现数据的访问

2017-10-06 20:34 591 查看
Spring Data给我们带来了访问数据的很多便利,接下来我们结合spring-data-cassandra来看一下如何快速实现对Cassandra数据的访问。

当然了,官方的手册是一定要看的,官方1.2.0RELEASE文档。准备一下基础使用的dependency:

<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-cassandra</artifactId>
<version>1.2.0.RELEASE</version>
</dependency>

Setp1:定义域模型(在JPA里称之为实体),例如Person:

import org.springframework.data.cassandra.mapping.PrimaryKey;
import org.springframework.data.cassandra.mapping.Table;

@Table
public class Person {
@PrimaryKey
private String id;
private String name;
private int age;

public Person(String id,String name,int age){
this.id = id;
this.name = name;
this.age=age;
}

}

熟悉JPA的朋友不难发现,这真的和Entity定义很像。但它是有所不同的,JPA中是不允许有构造函数的,但spring-data-cassandra里不存在这样的限制。毕竟spring-data-cassandra的映射关系与JPA有所不同。看到了么,不再是@Id而是使用了@PrimaryKey。

Step2:定义相关的Repository,例如PersonRepository:

import org.springframework.data.repository.CrudRepository;
import demo.domain.Person;
public interface PersonRepository extends CrudRepository<Person, String>{

}

用过spring-data-jpa的朋友应该很明白这个如何使用,当然了,对于复合主键还是要加些许注意的。

Step3:关键的配置文件,其实以上内容对Spring data有了解的人基本都会弄。唯独需要注意的一点就是这配置文件,绝对有着自己的特点:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:cassandra="http://www.springframework.org/schema/data/cassandra"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/cql http://www.springframework.org/schema/cql/spring-cql-1.0.xsd http://www.springframework.org/schema/data/cassandra http://www.springframework.org/schema/data/cassandra/spring-cassandra-1.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<context:property-placeholder location="classpath:cassandra.properties"/>
<cassandra:cluster contact-points="${cassandra.contactpoints}" port="${cassandra.port}"/>
<cassandra:session keyspace-name="${cassandra.keyspace}" />
<cassandra:mapping />
<cassandra:converter/>
<cassandra:template id="cqlTemplate"/>
<cassandra:repositories base-package="demo" />
</beans>

官方资料里<cassandra:template id="cqlTemplate"/>的id定义有一些问题,经过实践只需要调整为上面的名称即可。当然,这里使用了属性文件,cassandra.properties:

cassandra.contactpoints=127.0.0.1

cassandra.port=9042
cassandra.keyspace=mykeyspace

Step4:穿插起来运行:

public class Demo {
public static void main(String[] args) {
ConfigurableApplicationContext ct = new ClassPathXmlApplicationContext(
"beans.xml");
PersonRepository repository = ct.getBean(PersonRepository.class);

List<Person> persons = new ArrayList<Person>();
for(int i=0;i<10000;i++){
Person person = new Person(Integer.toString(i),"name",i);
persons.add(person);
}
repository.save(persons);
ct.close();
}
}

OK,everything is OK. 的确很简单。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐