Repository架构解析

一、Repository

@Indexed
public interface Repository<T, ID>

被定义为访问底层数据模型的超级接口,所有继承这个接口的interface都被spring所管理,此接口作为标识接口,功能就是用来控制domain模型的

二、CrudRepository

@NoRepositoryBean
interface CrudRepository<T, ID> extends Repository<T, ID>
方法:
  <S extends T> S save(S entity);//保存单个实体
  <S extends T> Iterable<S> saveAll(Iterable<S> entities);;//保存集合
  Optional<T> findById(ID id);//根据id查找实体
  boolean existsById(ID id);//根据id判断实体是否存在
  Iterable<T> findAll();//查询所有实体,不用或慎用!
  Iterable<T> findAllById(Iterable<ID> ids);//根据id列表查询实体
  long count();//查询实体数量
  void deleteById(ID id);//根据Id删除实体
  void delete(T entity);//删除一个实体
  void deleteAll(Iterable<? extends T> entities);//删除一个实体的集合
  void deleteAll();//删除所有实体,不用或慎用!

三、PagingAndSortingRepository

@NoRepositoryBean
public interface PagingAndSortingRepository<T, ID> extends CrudRepository<T, ID> 
方法:
  Iterable<T> findAll(Sort sort);//排序
  Page<T> findAll(Pageable pageable);//分页查询(含排序功能)

四、JpaRepository
@NoRepositoryBean
public interface JpaRepository<T, ID> extends PagingAndSortingRepository<T, ID>, QueryByExampleExecutor<T>
方法:

  1、重写CrudRepository中Iterable<T> findAll()
  @Override
  List<T> findAll();
  2、//重写PagingAndSortingRepository中Iterable<T> findAll(Sort sort)
  @Override
  List<T> findAll(Sort sort);
  3、//重写CrudRepository中Iterable<T> findAllById(Iterable<ID> ids)
  @Override
  List<T> findAllById(Iterable<ID> ids);
  4、//重写CrudRepository中<S extends T> Iterable<S> saveAll(Iterable<S> entities);
  @Override
  <S extends T> List<S> saveAll(Iterable<S> entities);
  5、void flush();//执行缓存与数据库同步
  6、<S extends T> S saveAndFlush(S entity);//强制执行持久化
  7、void deleteInBatch(Iterable<T> entities);//批量删除给定的实体
  8、void deleteAllInBatch();//删除批处理调用中的所有实体
  9、T getOne(ID id);//和findById的区别,没有实体会报错
  10、继承来自QueryByExampleExecutor,详见jpa动态查询
    @Override
<S extends T> List<S> findAll(Example<S> example);
  11、继承来自QueryByExampleExecutor,详见jpa动态查询
    @Override
<S extends T> List<S> findAll(Example<S> example, Sort sort);
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容