Spring repository操作Cassandra

1、定义接口

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

public interface PersonRepository extends CrudRepository<Person, String> {
    List<Person> 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<Person> 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<Person> 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<Person> personList = repository.findByNameLike("测试%");
        personList.stream().forEach(p -> System.out.println(p.toString()));
    }

}

 5、结果输出

testFind 测试结果:
{ @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 = 7ace2ca0-237f-40ee-bc1f-31bfc3db3b5b, name = 测试5, age = 55 }
{ @type = com.github.theseus.spring.cassandra.domain.Person, id = 929b64ab-b965-4022-99ca-7c48a3506681, name = 测试6, age = 56 }
{ @type = com.github.theseus.spring.cassandra.domain.Person, id = 2c8a7e2b-c91d-4700-b54a-b1d253aaad20, name = 测试2, age = 52 }
{ @type = com.github.theseus.spring.cassandra.domain.Person, id = d7141c64-2d6e-4ea5-8637-1241f9060961, name = 测试7, age = 57 }
{ @type = com.github.theseus.spring.cassandra.domain.Person, id = 5b529f77-2665-45b4-9dc5-358f4b4c7116, name = 测试8, age = 58 }
{ @type = com.github.theseus.spring.cassandra.domain.Person, id = 46d1a84f-a1f3-4704-8a26-debb0348bbcc, name = 测试9, age = 59 }
{ @type = com.github.theseus.spring.cassandra.domain.Person, id = dcf9a832-0718-49cf-8ac1-29da5f844c03, name = 测试0, age = 50 }
{ @type = com.github.theseus.spring.cassandra.domain.Person, id = c4e22b77-07db-4417-85a4-e8f29e0c2c13, name = 测试3, age = 53 }
{ @type = com.github.theseus.spring.cassandra.domain.Person, id = e3f14738-cf8e-47ad-8188-a4e53344b4a2, name = 测试1, age = 51 

猜你喜欢

转载自theseus.iteye.com/blog/2435280