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、项目地址

  https://github.com/hjguang/spring-cassandra

你可能感兴趣的:(spring,cassandra)