SpringData系列二 Repository接口


  本節主要介紹Repository接口規范,及其子接口

  1. Repository是一個空接口,即標准接口
  2. 若我們定義的接口繼承了Repository,則該接口會被IOC容器識別為一個Repositoty Bean納入到IOC容器中。進而可以在該接口中定義滿足一定規范的方法。
  3. 實際上也可以通過注解的方式定義Repository接口
     1 package com.ntjr.springdata;
     2 
     3 import org.springframework.data.repository.RepositoryDefinition;
     4 
     5 /**
     6  * 
     7  * 1、實現Repository接口 2、通過注解的方式@RepositoryDefinition將一個bean定義為Repository接口
     8  */
     9 @RepositoryDefinition(idClass = Integer.class, domainClass = Person.class)
    10 public interface PersonRepsitory {
    11     // 根據lastName獲取對應的person
    12     Person getByLastName(String lastName);
    13 }
    PersonRepository.java

     

  4. Repository的子接口

    

    org.springframework.data.repository.CrudRepository<T, ID> :實現了一組CRUD的方法

    org.springframework.data.repository.PagingAndSortingRepository<T, ID>:實現了一組分頁排序相關的方法

    org.springframework.data.jpa.repository.JpaRepository<T, ID>:實現了一組JPA相關規范的方法

    自定義的接口繼承JpaRepository 這樣的接口就具有通用的數據訪問控制層的能力。

 

  

 


免責聲明!

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



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