溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

如何使用Spring?Data?repository進行數(shù)據(jù)層的訪問

發(fā)布時間:2022-06-14 09:54:43 來源:億速云 閱讀:150 作者:iii 欄目:開發(fā)技術(shù)

本篇內(nèi)容主要講解“如何使用Spring Data repository進行數(shù)據(jù)層的訪問”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學(xué)習“如何使用Spring Data repository進行數(shù)據(jù)層的訪問”吧!

使用Spring Data repository進行數(shù)據(jù)層的訪問

抽象出Spring Data repository是因為在開發(fā)過程中,常常會為了實現(xiàn)不同持久化存儲的數(shù)據(jù)訪問層而寫大量的大同小異的代碼。

Spring Data repository的目的就是要大幅減少這些重復(fù)的代碼。 Spring Data Elasticsearch為文檔的存儲,查詢,排序和統(tǒng)計提供了一個高度抽象的模板。

核心概念

Spring Data repository抽象中最核心的接口就是Repository。該接口使用了泛型,需要提供兩個類型參數(shù),

  • 第一個是接口處理的域?qū)ο箢愋?/p>

  • 第二個是域?qū)ο蟮闹麈I類型。

這個接口常被看做是一個標記型接口,用來獲取要操作的域?qū)ο箢愋秃蛶椭_發(fā)者識別繼承這個類的接口。在Repository的基礎(chǔ)上,CrudRepository接口提供了針對實體類的復(fù)雜的CRUD(增刪改查)操作。

public interface CrudRepository<T, ID extends Serializable>
    extends Repository<T, ID> {
    <S extends T> S save(S entity); 
    T findOne(ID primaryKey);       
    Iterable<T> findAll();          
    Long count();                   
    void delete(T entity);          
    boolean exists(ID primaryKey);  
    // … more functionality omitted.
}

PagingAndSortingRepository接口在CrudRepository的基礎(chǔ)上增加了一些方法,使開發(fā)者可以方便的對實體類進行分頁和排序。

public interface PagingAndSortingRepository<T, ID extends Serializable>
  extends CrudRepository<T, ID> {
  Iterable<T> findAll(Sort sort);
  Page<T> findAll(Pageable pageable);
}

在分頁長度為20的基礎(chǔ)上,想要獲取第二頁的User數(shù)據(jù),代碼如下

PagingAndSortingRepository<User, Long> repository = // … get access to a bean
Page<User> users = repository.findAll(new PageRequest(1, 20));

查詢方法

標準的CRUD(增刪改查)功能都要使用查詢語句來查詢數(shù)據(jù)庫。但通過使用Spring Data,只要五個步驟就可以實現(xiàn)。

  • 創(chuàng)建一個Domain類

@Entity
@Document
public class Person {
  …
}
  • 聲明一個繼承Repository接口或其子接口的持久層接口。并標明要處理的域?qū)ο箢愋图捌渲麈I的類型(在下面的例子中,要處理的域?qū)ο笫荘erson,其主鍵類型是Long)

interface PersonRepository extends Repository<Person, Long> { … }
  • 在接口中聲明查詢方法(spring會為其生成實現(xiàn)代碼)

interface PersonRepository extends Repository<Person, Long> {
  List<Person> findByLastname(String lastname);
}
  • 讓Spring創(chuàng)建對這些接口的代理實例。

使用JavaConfig的方式

import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
@EnableJpaRepositories
class Config {}

使用xml配置的方式

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:jpa="http://www.springframework.org/schema/data/jpa"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
     http://www.springframework.org/schema/beans/spring-beans.xsd
     http://www.springframework.org/schema/data/jpa
     http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">
   <jpa:repositories base-package="com.acme.repositories"/>
</beans>

注入repository實例,并使用

public class SomeClient {
  @Autowired
  private PersonRepository repository;
  public void doSomething() {
    List<Person> persons = repository.findByLastname("Matthews");
  }
}

定義查詢方法

CREATE

Spring Data repository自帶了一個非常有用的查詢構(gòu)造器。它會從方法名中去掉類似find..By,read...By,query...By,count...By之類的前綴,然后解析剩余的名字。我們也可以在方法名中加入更多的表達式,比如查詢時需要distinct約束,那么在方法名中加入Distinct即可。方法名中的第一個By是一個分解符,代表著查詢語句的開始,我們可以用And或Or來將多個查詢條件關(guān)聯(lián)起來。

public interface PersonRepository extends Repository<User, Long> {
  List<Person> findByEmailAddressAndLastname(EmailAddress emailAddress, String lastname);
  // Enables the distinct flag for the query
  List<Person> findDistinctPeopleByLastnameOrFirstname(String lastname, String firstname);
  List<Person> findPeopleDistinctByLastnameOrFirstname(String lastname, String firstname);
  // Enabling ignoring case for an individual property
  List<Person> findByLastnameIgnoreCase(String lastname);
  // Enabling ignoring case for all suitable properties
  List<Person> findByLastnameAndFirstnameAllIgnoreCase(String lastname, String firstname);
  // Enabling static ORDER BY for a query
  List<Person> findByLastnameOrderByFirstnameAsc(String lastname);
  List<Person> findByLastnameOrderByFirstnameDesc(String lastname);
}

除此之外,我們還可以為方法添加某些特定類型的參數(shù)(如:Pageable和Sort)來動態(tài)的在查詢中添加分頁和排序。

Page<User> findByLastname(String lastname, Pageable pageable);
Slice<User> findByLastname(String lastname, Pageable pageable);
List<User> findByLastname(String lastname, Sort sort);
List<User> findByLastname(String lastname, Pageable pageable);

USE_DECLARED_QUERY

如果方法通過 @Query 指定了查詢語句,則使用該語句創(chuàng)建Query;如果沒有,則查找是否定義了符合條件的Named Query,如果找到,則使用該命名查詢;如果兩者都沒有找到,則拋出異常。使用@Query聲明查詢語句的例子如下:

//使用Query注解
@Query("select a from AccountInfo a where a.accountId = ?1")
public AccountInfo findByAccountId(Long accountId);

CREATE_IF_NOT_FOUND

結(jié)合了CREATE和USE_DECLARED_QUERY 兩種策略,會先嘗試查找聲明好的查詢,如果沒有找到,就按照解析方法名的方式構(gòu)建查詢。這是默認的查詢策略,如果不更改配置,會一直使用這種策略構(gòu)建查詢。這種策略支持通過方法名快速定義一個查詢,也允許引入聲明好的查詢。

WEB支持

DomainClassConverter 允許開發(fā)者在SpringMVC控制層的方法中直接使用域?qū)ο箢愋?Domain types),而無需通過repository手動查找這個實例。

@Controller
@RequestMapping("/users")
public class UserController {
  @RequestMapping("/{id}")
  public String showUserForm(@PathVariable("id") User user, Model model) {
    model.addAttribute("user", user);
    return "userForm";
  }
}

上面的方法直接接收了一個User對象,開發(fā)者不需要做任何的搜索操作,轉(zhuǎn)換器會自動將路徑變量id轉(zhuǎn)為User對象的id,并且調(diào)用了findOne()方法查詢出User實體。 注意:當前的Repository 必須實現(xiàn)CrudRepository

HandlerMethodArgumentResolver使開發(fā)者可以在controller的方法中使用Pageable和Sort作為參數(shù)。

@Controller
@RequestMapping("/users")
public class UserController {
  @Autowired UserRepository repository;
  @RequestMapping
  public String showUsers(Model model, Pageable pageable) {
    model.addAttribute("users", repository.findAll(pageable));
    return "users";
  }
}

通過上面的方法定義,Spring MVC會使用下面的默認配置嘗試從請求參數(shù)中得到一個Pageable的實例。

參數(shù)名作用
page想要獲取的頁數(shù),默認為0
size獲取頁的大小,默認為20
page需要排序的屬性,格式為property,property(,ASC/DESC),默認升序排序。支持多個字段排序,比如?sort=firstname&sort=lastname,asc

開發(fā)者也可以針對多個表定義多個Pageable或Sort實例,需要使用Spring的@Qualifier注解來區(qū)分它們。并且請求參數(shù)名要帶有${qualifier}_的前綴。例子如下:

public String showUsers(Model model,
      @Qualifier("foo") Pageable first,
      @Qualifier("bar") Pageable second) { … }

請求中需要帶有foo_page和bar_page等參數(shù)。

到此,相信大家對“如何使用Spring Data repository進行數(shù)據(jù)層的訪問”有了更深的了解,不妨來實際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進入相關(guān)頻道進行查詢,關(guān)注我們,繼續(xù)學(xué)習!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI