博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
五分钟学会使用spring-data-cassandra快速实现数据的访问
阅读量:2437 次
发布时间:2019-05-10

本文共 2863 字,大约阅读时间需要 9 分钟。

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

当然了,官方的手册是一定要看的,。准备一下基础使用的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. 的确很简单。

转载地址:http://xpgmb.baihongyu.com/

你可能感兴趣的文章
解决无盘多机启动慢的方法(转)
查看>>
检查字符串strSource是否为big或big5码(转)
查看>>
EXCEL读取与写入数据的最佳方案(转)
查看>>
windows运行命令详解(转)
查看>>
sql语句插入的数据中含有单引号怎么办(转)
查看>>
RJ45接头接法(转)
查看>>
将数据库的内容放到下拉列表中(转)
查看>>
突破网吧及机房管理限制的方法(转)
查看>>
WAP 2.0--XHTML mobile profile(转)
查看>>
Platform Builder之旅(二)(转)
查看>>
GFP:新一代多业务传输技术(转)
查看>>
安全至上:7月11日值得注意病毒列表(转)
查看>>
How to Use DBMS_SUPPORT Package(转)
查看>>
在Win2003中配置SNMP服务的网络安全(转)
查看>>
如何彻底保护你的网站不受RDS攻击的威胁(转)
查看>>
提高网站在Google中的排名——面向搜索引擎的网站设计(转)
查看>>
SQL Server 存储过程的经典分页(转)
查看>>
SMS基本概念和移动通信系统介绍(转)
查看>>
匿名FTP的安全设定(转)
查看>>
学习J2ME编程需要掌握的七种技术(转)
查看>>