Spring Data JPA

http://www.ibm.com/developerworks/cn/opensource/os-cn-spring-jpa/

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接口中的方法 

在Service層調用XxxxRepository接口與XxxxDao接口完成相應的業務邏輯



轉自:http://blog.csdn.net/shuimumu/article/details/7676425

發佈了115 篇原創文章 · 獲贊 105 · 訪問量 194萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章