五分钟学会使用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. 的确很简单。
当然了,官方的手册是一定要看的,官方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. 的确很简单。
相关文章推荐
- 五分钟学会使用spring-data-cassandra快速实现数据的访问
- Chapter 1. 使用Spring进行数据访问(Data Access With Spring)
- Spring Boot(7)— Spring Boot中使用Spring-data-jpa让数据访问更简单、更优雅
- SpringCloud SpringBoot mybatis 分布式微服务(九)Spring Boot中使用Spring-data-jpa让数据访问更简单
- springdata jpa使用Example快速实现动态查询
- Spring Boot中使用Spring-data-jpa让数据访问更简单、更优雅
- Spring Data MongoDB 使用group和unwind实现对数组数据进行简单统计
- Spring Boot中使用Spring-data-jpa让数据访问更简单、更优雅
- Spring Boot中使用Spring-data-jpa让数据访问更简单、更优雅
- MOSS 2010:Visual Studio 2010开发体验(20)——使用ADO.NET Data Service快速访问SharePoint列表数据
- MOSS 2010:Visual Studio 2010开发体验(20)——使用ADO.NET Data Service快速访问SharePoint列表数据
- Spring Boot中使用Spring-data-jpa让数据访问更简单、更优雅
- 使用Spring进行数据访问(Data Access With Spring)
- Spring Boot中使用Spring-data-jpa让数据访问更简单、更优雅
- MOSS 2010:Visual Studio 2010开发体验(20)——使用ADO.NET Data Service快速访问SharePoint列表数据
- 转载:使用Spring进行数据访问(Data Access With Spring)
- Spring Boot中使用Spring-data-jpa让数据访问更简单、更优雅
- 企业分布式微服务云SpringCloud SpringBoot mybatis (八)Spring Boot中使用Spring-data-jpa让数据访问更简单
- Spring Boot中使用Spring-data-jpa让数据访问更简单、更优雅
- Java Web学习系列——Maven Web项目中集成使用Spring、MyBatis实现对MySQL的数据访问