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、结果输出
{ @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
|
>、>= |
LessThanEqual
|
<、<= |
In |
类似sql中的IN |
|
模糊匹配 |
|
字符串包含功能 |
|
集合包含功能 |
无关键字 |
不指定时精确匹配 |
|
Boolean查询 |
7、项目地址
相关推荐
主要介绍了SpringData Repository接口用法解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
在eclipse中配置jpa的时候,jpa标签有可能会报错,可以加入该xsd文件
spring-data-jdbc-repository, Spring Data JDBC泛型DAO实现 Spring Data JDBC泛型DAO实现查看正在积极开发和维护的 jirutka/spring-data-jdbc-repository fork 。 不再支持这里存储库。本项目的目的是为基于 ...
3. 尚硅谷_SpringData_Repository接口 4. 尚硅谷_SpringData_Repository查询方法定义规范 5. 尚硅谷_SpringData_Query注解 6. 尚硅谷_SpringData_Modifying注解 7. 尚硅谷_SpringData_CrudRepository接口 8. 尚硅谷_...
Repository接口及其子接口的具体使用,并实现为单个Repository添加自定义的方法
EFcore Repository 依赖注入方式实现数据库基本操作
NULL 博文链接:https://forlan.iteye.com/blog/2413050
repository-spring4.part1
SpringDataJpa开发--继承JpaRepository实现简单条件查询示例代码
repository-spring4.part2
SpringData_Repository接口 · 4. SpringData_Repository查询方法定义规范 · 5. SpringData_Query注解 · 6. SpringData_Modifying注解 · 7. SpringData_CrudRepository接口 · 8. SpringData_...
Springboot + Spring MVC大大简化了Web应用的RESTful...Spring Data REST支持Spring Data JPA、Spring Data MongoDB、Spring Data Neo4j、Spring Data GenFire、Spring Data Cassandra,这里选择大家比较熟悉的JPA。
maven本地仓库repository中的springframework文件夹,许多repository文件夹中没有springframework文件夹,导致maven项目添加不了jar包。
repository
Spring注解@Component、@Repository、@Service、@Controller区别.doc
第五章 SpringData JPA实现多表操作 1.多表关系分析 2.案例表间关系 3.一对一关系 4.一对多关系 5.多对多关系 第六章 SpringData Redis 1.SpringData Redis简介 2.Redis环境搭建 3.SpringData Redis入门案例 4....
赠送jar包:spring-aop-5.2.0.RELEASE.jar; 赠送原API文档:spring-aop-5.2.0.RELEASE-javadoc.jar; 赠送源代码:spring-aop-5.2.0.RELEASE-sources.jar; 赠送Maven依赖信息文件:spring-aop-5.2.0.RELEASE.pom;...
掌握Repository接口 掌握SpringData的方法定义和规范 掌握@Query注解的使用 @Modifying 注解和事务 掌握Repository接口的子接口
主要介绍了Spring Data Jpa实现自定义repository转DTO,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
此项目介绍了Spring Data JPA的使用,包含3个自定义Repository的实现示例