Spring Data JPA 中的七大接口和两大实现类
接口
*PS:都是在org.springframework.data.包中的。
Repository接口
CrudRepository接口
PagingAndSortingRepository接口
JpaRepository接口
QueryByExampleExecutor接口
JpaSpecificationExecutor接口
QueryDslPredicateExecutor接口
实现类
PS:都是在org.springframework.data.jpa.repository.support中
SimpleJpaRepository类
QueryDslJpaRepository类
结构关系图
接口源码部分
- Repository接口 这是一个最顶层的接口 org.springframework.data.repository
@Indexed //Spring 提供的注解 加快启动速度 org.springframework.stereotype.Indexed public interface Repository<T, ID> { }
- CrudRepository接口 org.springframework.data.repository
@NoRepositoryBean //确保将该注释添加到所有存储库接口中,Spring数据不应在运行时为其创建实例。 public interface CrudRepository<T, ID> extends Repository<T, ID> { /** * 保存,没有Id的情况下是插入,有id的情况下是更新 */. <S extends T> S save(S entity); /** * 批量保存 */ <S extends T> Iterable<S> saveAll(Iterable<S> entities); /** * 根据Id查找对象 */ Optional<T> findById(ID id); /** * 根据id判断对象是否存在,不存在时返回null */ 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); /** * 批量删除 集合对象 实际上是一条一条的删除 调用delete */ void deleteAll(Iterable<? extends T> entities); /** * 删除所有 实际上也是一条一条的删除 */ void deleteAll(); }
- PagingAndSortingRepository接口 org.springframework.data.repository
@NoRepositoryBean public interface PagingAndSortingRepository<T, ID> extends CrudRepository<T, ID> { /** * 增加了一个排序的功能 * org.springframework.data.domain.Sort * Sort.Direction.DESC|ASC 降序/升序 * 这个排序的类 比较常用的一个方法是: * public Sort(Direction direction, String... properties) { * this(direction, properties == null ? new ArrayList<>() : Arrays.asList(properties)); * } * 第一个参数指定排序的方向,第二个参数是排序的属性字段 */ Iterable<T> findAll(Sort sort); /** * 增加了排序的功能和分页的功能 * import org.springframework.data.domain.Page * import org.springframework.data.domain.Pageable */ Page<T> findAll(Pageable pageable); }
- QueryByExampleExecutor接口 org.springframework.data.repository.query
public interface QueryByExampleExecutor<T> { /** * 查找一个对象 * Optional类Java8的新特性 是一个可以为null的容器对象 java.util.Optional * Optional 是个容器:它可以保存类型T的值,或者仅仅保存null * Example类 org.springframework.data.domain.Example 这个也是可以存储null值的 * 用于条件查询 */ <S extends T> Optional<S> findOne(Example<S> example); /** * 查找所有的对象 */ <S extends T> Iterable<S> findAll(Example<S> example); /** * 查找所有的对象,并且有排序功能 */ <S extends T> Iterable<S> findAll(Example<S> example, Sort sort); /** * 查找所有的对象,并且有排序和分页的功能 */ <S extends T> Page<S> findAll(Example<S> example, Pageable pageable); /** * 统计对象的数目 */ <S extends T> long count(Example<S> example); /** * 判断对象是否存在 */ <S extends T> boolean exists(Example<S> example); }
- JpaRepository接口 org.springframework.data.jpa.repository
@NoRepositoryBean public interface JpaRepository<T, ID> extends PagingAndSortingRepository<T, ID>, QueryByExampleExecutor<T> { /* * 查询所有对象 对原先返回的迭代器进行了一次封装,可以返回List */ List<T> findAll(); /* * 查询所有对象,带有排序功能 */ List<T> findAll(Sort sort); /* * 根据id的列表,查询所有符合的对象 */ List<T> findAllById(Iterable<ID> ids); /* * 批量保存 */ <S extends T> List<S> saveAll(Iterable<S> entities); /** * 强制缓存与数据库同步 也是刷新 */ void flush(); /** * 保存并且同步 */ <S extends T> S saveAndFlush(S entity); /** * 批量删除 集合对象 后台执行时,生成一条语句执行,用多个or条件 */ void deleteInBatch(Iterable<T> entities); /** * 删除所有对象 直接一条语句解决 delete from table_name */ void deleteAllInBatch(); /** * 根据Id查询,返回对象的引用,对象的引用,对象的引用 重要的事情说三遍 */ T getOne(ID id); /* * 根据实例查询所有对象 */ @Override <S extends T> List<S> findAll(Example<S> example); /* * 根据实例查询所有对象,并且提供排序功能 */ @Override <S extends T> List<S> findAll(Example<S> example, Sort sort); }
- JpaSpecificationExecutor接口 org.springframework.data.jpa.repository
public interface JpaSpecificationExecutor<T> { /** * @Nullable 表示可为空 @NotNull表示不可为空 * 提供动态查询的支持 * 返回与给定动态规范匹配的单个实体,如果未找到,则返回Optional为null的对象。 */ Optional<T> findOne(@Nullable Specification<T> spec); /** * 返回与给定动态规范匹配的所有实体 */ List<T> findAll(@Nullable Specification<T> spec); /** * 返回与给定动态规范匹配的所有实体,并提供分页功能 */ Page<T> findAll(@Nullable Specification<T> spec, Pageable pageable); /** * 返回与给定动态规范匹配的所有实体,并提供排序功能 */ List<T> findAll(@Nullable Specification<T> spec, Sort sort); /** * 返回与给定动态规范匹配的所有对象的总数 */ long count(@Nullable Specification<T> spec); }
- QueryDslPredicateExecutor接口 org.springframework.data.querydsl
public interface QuerydslPredicateExecutor<T> { /** * 返回与给定条件匹配单个对象 */ Optional<T> findOne(Predicate predicate); /** * 返回与给定条件匹配的所有对象 */ Iterable<T> findAll(Predicate predicate); /** * 返回与给定条件匹配的所有对象,并提供排序功能 */ Iterable<T> findAll(Predicate predicate, Sort sort); /** * 返回与给定条件匹配的所有对象,并按照一定的顺序 */ Iterable<T> findAll(Predicate predicate, OrderSpecifier<?>... orders); /** * 返回给定顺序的所有对象 */ Iterable<T> findAll(OrderSpecifier<?>... orders); /** * 返回与给定条件匹配的所有对象,并提供分页功能 */ Page<T> findAll(Predicate predicate, Pageable pageable); /** * 返回与给定条件匹配的所有对象的总数 */ long count(Predicate predicate); /** * 判断与给定条件匹配的对象是否存在 */ boolean exists(Predicate predicate); }
实现类
/**
* 对于只实现了接口,并且只定义了XXXRepository接口,而没有具体的实现类XXXRepositoryImpl的时候。
* Spring Data JPA 的后台通过CGLIB的动态代理,其实@Repository 标注的DAO类,就是使用的这个类
*/
@Repository
@Transactional(readOnly = true)
public class SimpleJpaRepository<T, ID> implements JpaRepositoryImplementation<T, ID>{
....
/**
* 这个已经被淘汰了....
*/
@Deprecated
public class QuerydslJpaRepository <T, ID extends Serializable> extends SimpleJpaRepository<T, ID>
implements QuerydslPredicateExecutor<T> {
也就是说这样:
public class QuerydslJpaRepository …
对于接口中一些的方法定义规则,简化各种SQL代码的书写
KeyWord | Meaning | Sample | SQL |
---|---|---|---|
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 | 之前(时间)< | fingByStartDateBefore | where x.startDate < ?1 |
IsNull | 等于null | findByAgeIsNull | where x.age is null |
IsNotNull,NotNull | 不等于null | findByAge(Is)NotNull | where x.age not null |
Like | 模糊查询,查询中需要自己加% | findByFirstnameLike | where x.firstname like ?1 |
NotLike | 不再模糊范围内,查询中需要自己jia% | 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 | 包含某 | fingByFirstnameContaining | where x.firstname like ?1(parameter bound wrapped in %) |
OrderBy | 排序 | fingByAgeOrderByLastNameDesc | 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.firstname)=UPPER(?1) |