Spring Boot中CrudRepository與JpaRepository Dao中JpaRepository和JpaSpecificationExecutor查詢


原文地址  https://blog.csdn.net/xuemengrui12/article/details/80525227?utm_source=blogxgwz0

 https://www.imooc.com/article/16983

 

先看下兩者的接口代碼:
 
  1.  
    @NoRepositoryBean
  2.  
    public interface CrudRepository<T, ID extends Serializable> extends Repository<T, ID> {
  3.  
    <S extends T> S save(S var1);
  4.  
    <S extends T> Iterable<S> save(Iterable<S> var1);
  5.  
    T findOne(ID var1);
  6.  
    boolean exists(ID var1);
  7.  
    Iterable<T> findAll();
  8.  
    Iterable<T> findAll(Iterable<ID> var1);
  9.  
    long count();
  10.  
    void delete(ID var1);
  11.  
    void delete(T var1);
  12.  
    void delete(Iterable<? extends T> var1);
  13.  
    void deleteAll();
  14.  
    }
 
  1.  
    @NoRepositoryBean
  2.  
    public interface JpaRepository<T, ID extends Serializable> extends PagingAndSortingRepository<T, ID>, QueryByExampleExecutor<T> {
  3.  
    List<T> findAll();
  4.  
    List<T> findAll(Sort var1);
  5.  
    List<T> findAll(Iterable<ID> var1);
  6.  
    <S extends T> List<S> save(Iterable<S> var1);
  7.  
    void flush();
  8.  
    <S extends T> S saveAndFlush(S var1);
  9.  
    void deleteInBatch(Iterable<T> var1);
  10.  
    void deleteAllInBatch();
  11.  
    T getOne(ID var1);
  12.  
    <S extends T> List<S> findAll(Example<S> var1);
  13.  
    <S extends T> List<S> findAll(Example<S> var1, Sort var2);
  14.  
    }
 

看一下他們的繼承關系


 

注意下兩者的save方法的不同,JpaRepository 中的save方法實現源碼:
 
  1.  
    @Transactional
  2.  
    public <S extends T> List<S> save(Iterable<S> entities) {
  3.  
    List<S> result = new ArrayList<S>();
  4.  
    if (entities == null) {
  5.  
    return result;
  6.  
    }
  7.  
    for (S entity : entities) {
  8.  
    result.add(save(entity));
  9.  
    }
  10.  
    return result;
  11.  
    }
CrudRepository 中的save方法源代碼
  1.  
    @Transactional
  2.  
    public <S extends T> S save(S entity) {
  3.  
    if (entityInformation.isNew(entity)) {
  4.  
    em.persist(entity); //是新的就插入
  5.  
    return entity;
  6.  
    } else {
  7.  
    return em.merge(entity); //不是新的merge
  8.  
    }
  9.  
    }
 
由源碼可知CrudRepository 中的save方法是相當於merge+save ,它會先判斷記錄是否存在,如果存在則更新,不存在則插入記錄。唉,還是需要多看源碼啊

參考:

 

 
 

一、JpaRepository支持接口規范方法名查詢。意思是如果在接口中定義的查詢方法符合它的命名規則,就可以不用寫實現。使用命名化參數,使用@Param即可,比如:
@Query(value="select o from UserModel o where o.name like %:nn") public List<UserModel> findByUuidOrAge(@Param("nn") String name);
二、spring data jpa 通過創建方法名來做查詢,只能做簡單的查詢,那如果我們要做復雜一些的查詢呢,多條件分頁怎么辦,這里,spring data jpa為我們提供了JpaSpecificationExecutor接口,只要簡單實現toPredicate方法就可以實現復雜的查詢。JpaSpecificationExecutor提供了以下接口

public interface JpaSpecificationExecutor<T> { T findOne(Specification<T> spec); List<T> findAll(Specification<T> spec); Page<T> findAll(Specification<T> spec, Pageable pageable); List<T> findAll(Specification<T> spec, Sort sort); long count(Specification<T> spec); }
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM