SpringData
AppleFramework在数据访问控制层采用了Spring Data作为这一层的解决方案
提供了一整套数据访问层(DAO)的解决方案,致力于减少数据访问层(DAO)的开发量。它使用一个叫作Repository的接口类为基础,它被定义为访问底层数据模型的超级接口。而对于某种具体的数据访问操作,则在其子接口中定义。
public interface Repository<T, ID extends Serializable> {
}
所有继承这个接口的interface都被spring所管理,此接口作为标识接口,功能就是用来控制domain模型的。
Spring Data可以让我们只定义接口,只要遵循spring data的规范,就无需写实现类。
Repository
Repository(资源库):通过用来访问领域对象的一个类似集合的接口,在领域与数据映射层之间进行协调。这个叫法就类似于我们通常所说的DAO,在这里,我们就按照这一习惯把数据访问层叫Repository.
Spring Data给我们提供几个Repository,基础的Repository提供了最基本的数据访问功能,其几个子接口则扩展了一些功能。它们的继承关系如下:
Repository: 仅仅是一个标识,表明任何继承它的均为仓库接口类,方便Spring自动扫描识别
CrudRepository: 继承Repository,实现了一组CRUD相关的方法
PagingAndSortingRepository: 继承CrudRepository,实现了一组分页排序相关的方法
JpaRepository: 继承PagingAndSortingRepository,实现一组JPA规范相关的方法
JpaSpecificationExecutor: 比较特殊,不属于Repository体系,实现一组JPA Criteria查询相关的方法
我们自己定义的XxxxRepository需要继承JpaRepository,这样我们的XxxxRepository接口就具备了通用的数据访问控制层的能力。
CrudRepository
/**
* 保存一个实体。
*/
<S extends T> S save(S entity);
/**
* 保存提供的所有实体。
*/
<S extends T> Iterable<S> saveAll(Iterable<S> entities);
/**
* 根据id查询对应的实体。
*/
Optional<T> findById(ID id);
/**
* 根据id查询对应的实体是否存在。
*/
boolean existsById(ID id);
/**
* 查询所有的实体。
*/
Iterable<T> findAll();
/**
* 根据给定的id集合查询所有对应的实体,返回实体集合。
*/
Iterable<T> findAllById(Iterable<ID> ids);
/**
* 统计现存实体的个数。
*/
long count();
/**
* 根据id删除对应的实体。
*/
void deleteById(ID id);
/**
* 删除给定的实体。
*/
void delete(T entity);
/**
* 删除给定的实体集合。
*/
void deleteAll(Iterable<? extends T> entities);
/**
* 删除所有的实体。
*/
void deleteAll();
PagingAndSortingRepository
/**
* 返回所有的实体,根据Sort参数提供的规则排序。
*/
Iterable<T> findAll(Sort sort);
/**
* 返回一页实体,根据Pageable参数提供的规则进行过滤。
*/
Page<T> findAll(Pageable pageable);
JpaRepository
/**
* 将所有未决的更改刷新到数据库。
*/
void flush();
/**
* 保存一个实体并立即将更改刷新到数据库。
*/
<S extends T> S saveAndFlush(S entity);
/**
* 在一个批次中删除给定的实体集合,这意味着将产生一条单独的Query。
*/
void deleteInBatch(Iterable<T> entities);
/**
* 在一个批次中删除所有的实体。
*/
void deleteAllInBatch();
/**
* 根据给定的id标识符,返回对应实体的引用。
*/
T getOne(ID id);
使用JpaRepository查询
基本查询也分为两种,一种是spring data默认已经实现,一种是根据查询的方法来自动解析成SQL。
| Keyword | Sample | JPQL snippet |
|---|---|---|
| And | findByLastnameAndFirstname | … where x.lastname = ?1 and x.firstname = ?2 |
| Or | findByLastnameOrFirstname | … where x.lastname = ?1 or x.firstname = ?2 |
| Is,Equals | findByFirstnameIs,findByFirstnameEquals | … where x.firstname = ?1 |
| Between | findByStartDateBetween | … where x.startDate between ?1 and ?2 |
| LessThan | findByAgeLessThan | … where x.age < ?1 |
| LessThanEqual | findByAgeLessThanEqual | … where x.age ⇐ ?1 |
| GreaterThan | findByAgeGreaterThan | … where x.age > ?1 |
| GreaterThanEqual | findByAgeGreaterThanEqual | … where x.age >= ?1 |
| After | findByStartDateAfter | … where x.startDate > ?1 |
| Before | findByStartDateBefore | … where x.startDate < ?1 |
| IsNull | findByAgeIsNull | … where x.age is null |
| IsNotNull,NotNull | findByAge(Is)NotNull | … where x.age not null |
| Like | findByFirstnameLike | … where x.firstname like ?1 |
| NotLike | findByFirstnameNotLike | … where x.firstname not like ?1 |
| StartingWith | findByFirstnameStartingWith | … where x.firstname like ?1 (parameter bound with appended %) |
| EndingWith | findByFirstnameEndingWith | … where x.firstname like ?1 (parameter bound with prepended %) |
| Containing | findByFirstnameContaining | … where x.firstname like ?1 (parameter bound wrapped in %) |
| OrderBy | findByAgeOrderByLastnameDesc | … where x.age = ?1 order by x.lastname desc |
| Not | findByLastnameNot | … where x.lastname <> ?1 |
| In | findByAgeIn(Collection ages) | … where x.age in ?1 |
| NotIn | findByAgeNotIn(Collection age) | … where x.age not in ?1 |
| TRUE | findByActiveTrue() | … where x.active = true |
| FALSE | findByActiveFalse() | … where x.active = false |
| IgnoreCase | findByFirstnameIgnoreCase | … where UPPER(x.firstame) = UPPER(?1) |
1.按照Spring Data的规范的规范,查询方法以find | read | get 开头,涉及查询条件时,条件的属性用条件关键字连接,
2.要注意的是:条件属性以首字母大写。
参考
spring mvc 的jpa JpaRepository数据层 访问方式汇总
SpringDataJpa——JpaRepository增删改查

本文介绍了Spring Data在数据访问控制层的应用,特别是JpaRepository接口的使用。通过继承JpaRepository,开发者可以获得基本的CRUD及分页排序功能,而无需编写实现类。此外,还探讨了Spring Data如何根据查询方法自动解析成SQL。
940

被折叠的 条评论
为什么被折叠?



