cassandra的java api_Spring repository操作Cassandra

1、定义接口

通过继承 CrudRepository实现数据的基本操作

public interface PersonRepository extends CrudRepository {

List findByNameLike(String name);

}

2、Cassandra的配置

继承AbstractCassandraConfiguration,指定默认的库,并添加EnableCassandraRepositories注解

@Configuration

@EnableCassandraRepositories

public class ApplicatonConfig extends AbstractCassandraConfiguration {

/**

* 指定Cassandra数据库

* @return

*/

@Override

protected String getKeyspaceName() {

return "cycling";

}

/**

* 配置实体bean的扫描路径

* @return

*/

@Override

public String[] getEntityBasePackages() {

return new String[] { "com.github.theseus.spring.cassandra.domain" };

}

}

3、定义实体类,映射表

@Table

public class Person {

@PrimaryKey

private String id;

private String name;

private Integer age;

public Person(String id, String name, int age) {

this.id = id;

this.name = name;

this.age = age;

}

// get set方法……

@Override

public String toString() {

return String.format("{ @type = %1$s, id = %2$s, name = %3$s, age = %4$d }",

getClass().getName(), getId(), getName(), getAge());

}

}

4、单元测试

@RunWith(SpringJUnit4ClassRunner.class)

@ContextConfiguration(classes = ApplicatonConfig.class)

public class ReposityTest {

@Autowired

PersonRepository repository;

@Test //查询所有数据

public void testReadAll() {

Iterable personpIterable = repository.findAll();

personpIterable.forEach(p -> System.out.println(p.toString()));

}

@Test //新增

public void testCreate() {

Person p = new Person(UUID.randomUUID().toString(), "theseus", 21);

repository.save(p);

}

@Test //修改,没有的话新增

public void testUpdate() {

Person p = new Person("5931583b-39b2-48ac-ba5d-e7b63523a97f", "Jon Doe", 40);

repository.save(p);

}

@Test //批量创建

public void testBatchCreate() {

List personList = new ArrayList<>();

for (int i=0;i<10;i++) {

personList.add(new Person(UUID.randomUUID().toString(), "测试" + i, 50 + i));

}

repository.saveAll(personList);

}

/**

* 创建SASIIndex索引,以支持模糊查询

*/

@Test //自定义方法模糊查询

public void testFind() {

List personList = repository.findByNameLike("测试%");

personList.stream().forEach(p -> System.out.println(p.toString()));

}

}

5、结果输出

{ @type = com.github.theseus.spring.cassandra.domain.Person, id = 6c05f079-5f2a-4ec0-bf97-7266c7361b87, name = 测试4, age = 54 }

……

……

{ @type = com.github.theseus.spring.cassandra.domain.Person, id = e3f14738-cf8e-47ad-8188-a4e53344b4a2, name = 测试1, age = 51 }

6、自定义方法说明

findBy+"属性"+操作关键字

关键字

说明

After/Before

日期比较,大于、小于参数值

GreaterThan/GreaterThanEqual

>、>=

LessThan/LessThanEqual

In

类似sql中的IN

Like, StartingWith, EndingWith

模糊匹配

Containing on String

字符串包含功能

Containing on Collection

集合包含功能

无关键字

不指定时精确匹配

IsTrue, True/IsFalse, False

Boolean查询

7、项目地址

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值