-
Repository 接口是 Spring Data 的一個核心接口,它不提供任何方法,開發者需要在自己定義的接口中聲明需要的方法 public interface Repository<T, ID extends Serializable> { }
- Spring Data可以讓我們只定義接口,只要遵循 Spring Data的規范,就無需寫實現類。
-
與繼承 Repository 等價的一種方式,就是在持久層接口上使用 @RepositoryDefinition 注解,並為其指定 domainClass 和 idClass 屬性。如下兩種方式是完全等價的
Repository 的子接口
- 基礎的 Repository 提供了最基本的數據訪問功能,其幾個子接口則擴展了一些功能。它們的繼承關系如下:
1.Repository: 僅僅是一個標識,表明任何繼承它的均為倉庫接口類
2.CrudRepository: 繼承 Repository,實現了一組 CRUD 相關的方法
3.PagingAndSortingRepository: 繼承 CrudRepository,實現了一組分頁排序相關的方法
4.JpaRepository: 繼承 PagingAndSortingRepository,實現一組 JPA 規范相關的方法
5.自定義的 XxxxRepository 需要繼承 JpaRepository,這樣的 XxxxRepository 接口就具備了通用的數據訪問控制層的能力。
6.JpaSpecificationExecutor: 不屬於Repository體系,實現一組 JPA Criteria 查詢相關的方法
SpringData 方法定義規范
- 簡單條件查詢:查詢某一個實體類或者集合
- 按照 Spring Data 的規范,查詢方法以 find | read | get 開頭, 涉及條件查詢時,條件的屬性用條件關鍵字連接,要注意的是:條件屬性以首字母大寫。
-
例如:定義一個 Entity 實體類 class User{ private String firstName; private String lastName; } 使用And條件連接時,應這樣寫: findByLastNameAndFirstName(String lastName,String firstName); 條件的屬性名稱與個數要與參數的位置與個數一一對應
支持的關鍵字
| Sample | JPQL snippet | |
|---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| Keyword | Sample | JPQL snippet |
|---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
好了廢話了這么多直接上代碼:
1.首先我們需要定義一個實體Person:
package com.fxr.springdata;
import java.util.Date;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
@Table(name="JPA_PERSONS")
@Entity
public class Person {
private Integer id;
private String lastName;
private String email;
private Date birth;
@GeneratedValue
@Id
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Date getBirth() {
return birth;
}
public void setBirth(Date birth) {
this.birth = birth;
}
@Override
public String toString() {
return "Person [id=" + id + ", lastName=" + lastName + ", email="
+ email + ", birth=" + birth + "]";
}
}
2.我們需要寫一個PersonRepsotory接口來實現JpaRepository<Person,Integer>,傳的兩個泛型第一個是實體類,第二個是主鍵的類型
package com.fxr.springdata;
import org.springframework.data.jpa.repository.JpaRepository;
public interface PersonRepsotory extends JpaRepository<Person,Integer>{
//根據lastName來獲取對應的Person
Person getByLastName(String lastName);
}
3.編寫測試類
package com.fxr.test;
import java.sql.SQLException;
import javax.sql.DataSource;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.fxr.springdata.Person;
import com.fxr.springdata.PersonRepsotory;
public class SpringDataTest {
private ApplicationContext ctx = null;
private PersonRepsotory personRepsotory;
{
ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
personRepsotory = ctx.getBean(PersonRepsotory.class);
}
//測試配置的數據源成功沒有成功
@Test
public void testDataSource() throws SQLException{
DataSource dataSource = ctx.getBean(DataSource.class);
System.out.println(dataSource.getConnection());
}
@Test
public void testHelloWorldSpringData(){
System.out.println(personRepsotory.getClass().getName());
Person person = personRepsotory.getByLastName("sunwukong");
System.out.println(person);
}
}
打印出結果,就說明我們的程序運行成功
