一、SpringData简介
https://docs.spring.io/spring-data/jpa/docs/current/reference/html/
  Spring Data是一个用于简化数据库访问,并支持云服务的开源框架。其主要目标是使得对数据的访问变得方便快捷。
  可以极大的简化JPA的写法,可以在几乎不用写实现的情况下,实现对数据的访问和操作。除了CRUD外,还包括如分页、排序等一些常用的功能。
主要来看看Spring Data JPA提供的接口,也是Spring Data JPA的核心概念:
1:Repository:最顶层的接口,是一个空的接口,目的是为了统一所有Repository的类型,且能让组件扫描的时候自动识别。
    2:CrudRepository :是Repository的子接口,提供CRUD的功能
    3:PagingAndSortingRepository:是CrudRepository的子接口,添加分页和排序的功能
    4:JpaRepository:是PagingAndSortingRepository的子接口,增加了一些实用的功能,比如:批量操作等。
    5:JpaSpecificationExecutor:用来做负责查询的接口
    6:Specification:是Spring Data JPA提供的一个查询规范,要做复杂的查询,只需围绕这个规范来设置查询条件即可
特征
1:强大的存储库和自定义对象映射抽象
2:从存储库方法名称中进行动态查询导出
3:实现域基类提供基本属性
4:支持透明审核(创建,最后更改)
5:集成自定义存储库代码的可能性
6:Easy Spring通过JavaConfig和自定义XML命名空间进行集成
7:与Spring MVC控制器进行高级集成
8:跨店存储的实验支持
二、Maven依赖
<dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
三、继承接口
Repository
public interface GatewayRouteRepository extends JpaRepository<GatewayRoute, String> , JpaSpecificationExecutor<GatewayRoute> {
}
四、Spring Data Repository 查询方法定义规范
简单查询条件 :查询某一个实体类或是集合
在Repository  子接口中声明方法:
①、不是随便声明的,而需要符合一定的规范
②、查询方法以 find | read | get 开头
③、涉及条件查询时,条件的属性用条件关键字连接
④、要注意的是:条件属性以首字母大写
⑤、支持属性的级联查询。若当前类有符合条件的属性,则优先使用,而不使用级联属性。若需要使用级联属性,则属性之间使用_连接
spring data支持的关键字
| 关键字 | 方法命名 | sql where字句 | 
|---|---|---|
| And | findByNameAndPwd | where name= ? and pwd =? | 
| Or | findByNameOrSex | where name= ? or sex=? | 
| Is,Equals | findById,findByIdEquals | where id= ? | 
| Between | findByIdBetween | where id between ? and ? | 
| LessThan | findByIdLessThan | where id < ? | 
| LessThanEquals | findByIdLessThanEquals | where id <= ? | 
| GreaterThan | findByIdGreaterThan | where id > ? | 
| GreaterThanEquals | findByIdGreaterThanEquals | where id > = ? | 
| After | findByIdAfter | where id > ? | 
| Before | findByIdBefore | where id < ? | 
| IsNull | findByNameIsNull | where name is null | 
| isNotNull,NotNull | findByNameNotNull | where name is not null | 
| Like | findByNameLike | where name like ? | 
| NotLike | findByNameNotLike | where name not like ? | 
| StartingWith | findByNameStartingWith | where name like '?%' | 
| EndingWith | findByNameEndingWith | where name like '%?' | 
| Containing | findByNameContaining | where name like '%?%' | 
| OrderBy | findByIdOrderByXDesc | where id=? order by x desc | 
| Not | findByNameNot | where name <> ? | 
| In | findByIdIn(Collection<?> c) | where id in (?) | 
| NotIn | findByIdNotIn(Collection<?> c) | where id not in (?) | 
| True | findByAaaTue | where aaa = true | 
| False | findByAaaFalse | where aaa = false | 
| IgnoreCase | findByNameIgnoreCase | where UPPER(name)=UPPER(?) |