Spring repository操作Cassandra

iteye| 阅读:1274 发表时间:2018-12-21 20:31:03 数据库
摘要:1、定义接口  通过继承 CrudRepository实现数据的基本操作public interface PersonRepository extends CrudReposito

1、定义接口

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

public interface PersonRepository extends CrudRepository<Person, String> {    List<Person> findByNameLike(String name);}

 2、Cassandra的配置

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

@Configuration@EnableCassandraRepositoriespublic 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、定义实体类,映射表

@Tablepublic 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、结果输出

 

{ @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

摘自:https://theseus.iteye.com/blog/2435280
如果您觉得好,可以打赏作者:
如果您觉得累了,是否想看点美女养养眼:猛戳>>朋友帮
如果您觉得皮了,是否想来点神吐槽:猛戳>>iPhone查询中

已有0条评论

昵称:
邮箱:

  • 最新评论

iPhone查询中 - bbs.ipcxz.com 朋友帮 - www.pengyb.cn iPhone查询中 - bbs.ipcxz.com
反馈
微信订阅号