本節主要介紹Repository接口規范,及其子接口
Repository是一個空接口,即標準接口若我們定義的接口繼承了Repository,則該接口會被IOC容器識別為一個Repositoty Bean納入到IOC容器中。進而可以在該接口中定義滿足一定規范的方法。實際上也可以通過注解的方式定義Repository接口
1
2
3
4
5
6
7
8
9
10
11
12
13
|
package com.ntjr.springdata; import org.springframework.data.repository.RepositoryDefinition; /** * * 1、實現Repository接口 2、通過注解的方式@RepositoryDefinition將一個bean定義為Repository接口 */ @RepositoryDefinition (idClass = Integer. class , domainClass = Person. class ) public interface PersonRepsitory { // 根據lastName獲取對應的person Person getByLastName(String lastName); } PersonRepository.java |
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 這樣的接口就具有通用的數據訪問控制層的能力。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:https://www.cnblogs.com/zhaobingqing/p/6854439.html