spring mvc 的jpa JpaRepository資料層 訪問方式匯總

來源:互聯網
上載者:User

標籤:cti   odi   同步   extends   mvc   entity   boolean   資源   2.2.3   

本文轉載網址:http://jishiweili.iteye.com/blog/2088265

AppleFramework在資料存取控制層採用了Spring Data作為這一層的解決方案,下面就對Spring Data相關知識作一個較為詳細的描述。 
1.Spring Data所解決的問題 
Spring Data :提供了一整套資料訪問層(DAO)的解決方案,致力於減少資料訪問層(DAO)的開發量。它使用一個叫作Repository的介面類為基礎,它被定義為訪問底層資料模型的超級介面。而對於某種具體的資料訪問操作,則在其子介面中定義。 
public interface Repository<T, ID extends Serializable> { 
} 
所有繼承這個介面的interface都被spring所管理,此介面作為標識介面,功能就是用來控制domain模型的。 
Spring Data可以讓我們只定義介面,只要遵循spring data的規範,就無需寫實作類別。 

2.什麼是Repository? 
2.1 Repository(資產庫):通過用來訪問領域對象的一個類似集合的介面,在領域與資料對應層之間進行協調。這個叫法就類似於我們通常所說的DAO,在這裡,我們就按照這一習慣把資料訪問層叫Repository 
Spring Data給我們提供幾個Repository,基礎的Repository提供了最基本的資料訪問功能,其幾個子介面則擴充了一些功能。它們的繼承關係如下: 
Repository: 僅僅是一個標識,表明任何繼承它的均為倉庫介面類,方便Spring自動掃描識別 
CrudRepository: 繼承Repository,實現了一組CRUD相關的方法 
PagingAndSortingRepository: 繼承CrudRepository,實現了一組分頁排序相關的方法 
JpaRepository: 繼承PagingAndSortingRepository,實現一組JPA規範相關的方法 
JpaSpecificationExecutor: 比較特殊,不屬於Repository體系,實現一組JPA Criteria查詢相關的方法 
我們自己定義的XxxxRepository需要繼承JpaRepository,這樣我們的XxxxRepository介面就具備了通用的資料存取控制層的能力。 
2.2 JpaRepository 所提供的準系統 
2.2.1 CrudRepository<T, ID extends Serializable>: 
這個介面提供了最基本的對實體類的添刪改查操作 
T save(T entity);//儲存單個實體 
        Iterable<T> save(Iterable<? extends T> entities);//儲存集合 
        T findOne(ID id);//根據id尋找實體 
        boolean exists(ID id);//根據id判斷實體是否存在 
        Iterable<T> findAll();//查詢所有實體,不用或慎用! 
        long count();//查詢實體數量 
        void delete(ID id);//根據Id刪除實體 
        void delete(T entity);//刪除一個實體 
void delete(Iterable<? extends T> entities);//刪除一個實體的集合 
        void deleteAll();//刪除所有實體,不用或慎用! 
2.2.2 PagingAndSortingRepository<T, ID extends Serializable> 
這個介面提供了分頁與排序功能 
Iterable<T> findAll(Sort sort);//排序 
        Page<T> findAll(Pageable pageable);//分頁查詢(含排序功能) 
2.2.3 JpaRepository<T, ID extends Serializable> 
這個介面提供了JPA的相關功能 
List<T> findAll();//尋找所有實體 
        List<T> findAll(Sort sort);//排序 尋找所有實體 
        List<T> save(Iterable<? extends T> entities);//儲存集合 
        void flush();//執行緩衝與資料庫同步 
        T saveAndFlush(T entity);//強制執行持久化 
void deleteInBatch(Iterable<T> entities);//刪除一個實體集合 
3.Spring data 查詢 
3.1 簡單條件查詢:查詢某一個實體類或者集合 
按照Spring data 定義的規則,查詢方法以find|read|get開頭 
涉及條件查詢時,條件的屬性用條件關鍵字串連,要注意的是:條件屬性以首字母大寫其餘字母小寫為規定。 
例如:定義一個Entity實體類 
class User{ 
private String firstname; 
private String lastname; 
} 
使用And條件串連時,應這樣寫: 
findByLastnameAndFirstname(String lastname,String firstname); 
條件的屬性名稱與個數要與參數的位置與個數一一對應 

3.2 使用JPA NamedQueries (標準規範實現) 
這種查詢是標準的JPA規範所定義的,直接聲明在Entity實體類上,調用時採用在介面中定義與命名查詢對應的method,由Spring Data根據方法名自動完成命名查詢的尋找。 
(1)在Entity實體類上使用@NamedQuery註解直接聲明命名查詢。 
@Entity 
@NamedQuery(name = "User.findByEmailAddress", 
  query = "select u from User u where u.emailAddress = ?1") 
public class User { 

} 
註:定義多個時使用下面的註解 
@NamedQueries(value = { 
                @NamedQuery(name = User.QUERY_FIND_BY_LOGIN, 
                                        query = "select u from User u where u." + User.PROP_LOGIN 
                                                + " = :username"), 
        @NamedQuery(name = "getUsernamePasswordToken", 
                        query = "select new com.aceona.weibo.vo.TokenBO(u.username,u.password) from User u where u." + User.PROP_LOGIN 
                            + " = :username")}) 
(2)在interface中定義與(1)對應的方法 
public interface UserRepository extends JpaRepository<User, Long> { 

  List<User> findByLastname(String lastname); 

  User findByEmailAddress(String emailAddress); 
} 
3.3 使用@Query自訂查詢(Spring Data提供的) 
這種查詢可以聲明在Repository方法中,擺脫像命名查詢那樣的約束,將查詢直接在相應的介面方法中聲明,結構更為清晰,這是Spring data的特有實現。 
例如: 
public interface UserRepository extends JpaRepository<User, Long> { 

  @Query("select u from User u where u.emailAddress = ?1") 
  User findByEmailAddress(String emailAddress); 
} 
3.4 @Query與 @Modifying 執行更新操作 
這兩個annotation一起聲明,可定義個人化更新操作,例如只涉及某些欄位更新時最為常用,樣本如下: 
@Modifying 
@Query("update User u set u.firstname = ?1 where u.lastname = ?2") 
int setFixedFirstnameFor(String firstname, String lastname); 

3.5 索引參數與具名引數 
(1)索引參數如下所示,索引值從1開始,查詢中 ”?X” 個數需要與方法定義的參數個數相一致,並且順序也要一致 
@Modifying 
@Query("update User u set u.firstname = ?1 where u.lastname = ?2") 
int setFixedFirstnameFor(String firstname, String lastname); 

(2)具名引數(推薦使用這種方式) 
可以定義好參數名,賦值時採用@Param("參數名"),而不用管順序。如下所示: 
public interface UserRepository extends JpaRepository<User, Long> { 

  @Query("select u from User u where u.firstname = :firstname or u.lastname = :lastname") 
  User findByLastnameOrFirstname(@Param("lastname") String lastname, 
                                 @Param("firstname") String firstname); 
} 

4. Transactionality(事務) 
4.1 操作單個對象的事務 
Spring Data提供了預設的交易處理方式,即所有的查詢均聲明為唯讀事務,對於持久化,更新與刪除對象聲明為有事務。 
參見org.springframework.data.jpa.repository.support.SimpleJpaRepository<T, ID> 
@org.springframework.stereotype.Repository 
@Transactional(readOnly = true) 
public class SimpleJpaRepository<T, ID extends Serializable> implements JpaRepository<T, ID>, 
                JpaSpecificationExecutor<T> { 
…… 
@Transactional 
        public void delete(ID id) { 

                delete(findOne(id)); 
        } 
…… 
} 
對於自訂的方法,如需改變spring data提供的事務預設,可以在方法上註解@Transactional聲明 

4.2 涉及多個Repository的交易處理 
進行多個Repository操作時,也應該使它們在同一個事務中處理,按照分層架構的思想,這部分屬於商務邏輯層,因此,需要在Service層實現對多個Repository的調用,並在相應的方法上聲明事務。 
例如: 
@Service(“userManagement”) 
class UserManagementImpl implements UserManagement { 

  private final UserRepository userRepository; 
  private final RoleRepository roleRepository; 

  @Autowired 
  public UserManagementImpl(UserRepository userRepository, 
    RoleRepository roleRepository) { 
    this.userRepository = userRepository; 
    this.roleRepository = roleRepository; 
  } 

  @Transactional 
  public void addRoleToAllUsers(String roleName) { 

    Role role = roleRepository.findByName(roleName); 

    for (User user : userRepository.readAll()) { 
      user.addRole(role); 
      userRepository.save(user); 
    } 
} 

5.關於DAO層的規範 
5.1對於不需要寫實作類別的情況:定義XxxxRepository 介面並繼承JpaRepository介面,如果Spring data所提供的預設介面方法不夠用,可以使用@Query在其中定義個人化的介面方法。 
5.2對於需要寫實作類別的情況:定義XxxxDao 介面並繼承com.aceona.appleframework.persistent.data.GenericDao 
書寫XxxxDaoImpl實作類別並繼承com.aceona.appleframework.persistent.data.GenericJpaDao,同時實現XxxxDao介面中的方法 

spring mvc 的jpa JpaRepository資料層 訪問方式匯總

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.