spring data jpa入門學習


 本文主要介紹下spring data jpa,主要聊聊為何要使用它進行開發以及它的基本使用。本文主要是入門介紹,並在最后會留下完整的demo供讀者進行下載,從而了解並且開始使用spring data jpa。

(1)spring data jpa是什么?

  小菜認為,spring data jpa,一、首先他不是ORM框架,跟hibernate不是同一個東西,hibernate是JPA標准的一個實現;二、它是對JPA進行了封裝,並且提供一些基礎的操作方法供開發者使用從而簡化數據訪問層的開發,我們使用spring data jpa 的目的就是為了簡化開發。

(2)spring data jpa 對持久層的簡化

  spring data jpa主要是對持久層進行簡化,主要體現在於:只要聲明持久層的接口,無需編寫具體的邏輯實現。這個原理是:根據方法名字進行解析,從而獲取這個方法要執行的操作。例如,findByUserName(String name),我們可以看到他要做的就是根據用戶名進行查詢。spring data jpa提供了豐富的根據關鍵字組合進行查詢,比如 like 、 not null、by等,如圖:

         

 所以我們的持久層就會顯得非常精簡,先看看一個簡單的持久層:

   

 1 package com.lcy.web.systemuser.model;
 2 
 3 import java.util.List;
 4  
 5 import org.springframework.data.domain.Pageable;
 6 import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
 7 import org.springframework.data.jpa.repository.Modifying;
 8 import org.springframework.data.jpa.repository.Query;
 9 import org.springframework.data.repository.CrudRepository;
10 import org.springframework.data.repository.PagingAndSortingRepository;
11 import org.springframework.stereotype.Repository;
12 import org.springframework.transaction.annotation.Transactional;
13 
14 import com.lcy.web.systemuser.pojo.User;
15 @Repository
16 public interface UserRepository extends  CrudRepository < User, Long > ,PagingAndSortingRepository<User,Long>, JpaSpecificationExecutor <User>{
17     
18   
19     @Query(value="select user from User user where competence=0",nativeQuery=false)
20     public List<User> getSystemUsers(Pageable pageable);
21 
22     @Query(value="select user from User user where competence=?1")
23     public List<User> getNormalUsers(Integer status);
24     /**
25      * nativeQuery=true 表示采用原生SQL語句進行操作
26      */
27     @Query(value="select * from cms_user where competence=?1",nativeQuery=true)
28     public List<User> getNormalUsersInNativeQuery(Integer status);
29     
30     @Modifying //表示修改操作,記得加事務
31     @Query(value="update User user set user.userName=?1 where user.userName=?2 ")
32     public void updateNameByName(String newName,String oldName);
33     public User findByUserNameLike(String userName);
34 }

 

 

內容不說,只要看看幾點因素,我們看到持久層只是一個接口,具體的方法的實現邏輯都省略到掉了,@Repository注解聲明是持久層,主要看看它繼承的三個類:

CrudRepository :提供基礎的增刪改查操作;
PagingAndSortingRepository:分頁和排序的操作、
JpaSpecificationExecutor :組合查詢條件,提供原生SQL查詢
(3)spring data JPA的幾個常用的功能:增刪改查、自定義SQL語句操作、分頁查詢
  3.1 增刪改查
    持久層繼承CrudRepository,CrudRepository提供了基礎的增刪改查操作,所以無需在持久層再次編寫方法,直接調用即可完成基礎的操作,比如:

1  userRepository.findAll();
2  userRepository.findOne(id);  

  3.2自定義SQL語句操作

    實際開發中有很多時候還是根據具體的需要進行一些自定義的查詢方法或者其他操作,這時候需要開發人員去編寫相關的SQL語句,spring data jpa提供了原生SQL語句操作還有面向對象的SQL操作

    比如:
1 @Query(value="select user from User user where competence=?1")
2     public List<User> getNormalUsers(Integer status);
3     /**
4      * nativeQuery=true 表示采用原生SQL語句進行操作
5      */
6     @Query(value="select * from cms_user where competence=?1",nativeQuery=true)
7     public List<User> getNormalUsersInNativeQuery(Integer status);

     3.3 分頁查詢

    關鍵類:Sort、Pageable和Specification,說明:Sort是排序條件組合,Pageable是分頁的信息組合(當前頁數、每頁記錄數等),Specification是查詢條件的組合。

    例如:

 1         //1. 排序
 2         Sort sort=new Sort(Sort.Direction.ASC, "id");
 3         //2.分頁條件
 4         Pageable pageable=new PageRequest(0, 2, sort);
 5         //3.查詢條件組合
 6         Specification<User> s=new Specification<User>() {
 7             @Override
 8             public Predicate toPredicate(Root<User> root, CriteriaQuery<?> query,
 9                     CriteriaBuilder builder) {
10                 Path<String> name=root.get("userName");
11                 Path<User> competence=root.get("competence");
12                 query.where(builder.like(name, "%K%"),builder.equal(competence, "1"));
13                 return null;
14             }
15         }; 
16         //執行
17         Page<User> users=userRepository.findAll(s,pageable);

 

  分頁寫的一些基礎使用的代碼,在實際開發中是不會這樣寫的,不夠通用導致代碼重復,當然目前已經有了相關的開源項目已經做得很好了,比如江南白衣的springside里面就有對分頁做了很好的封裝,使用起來非常方便。

簡單的spring data jpa就介紹到這里,純屬入門介紹,要深入學習還是看官方文檔比較好:

下面是參考資料:

  spring data jpa的官方文檔:http://docs.spring.io/spring-data/data-jpa/docs/current/reference/html/  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM