15.SPRING JPA 如何做條件查詢


JPA 條件查詢方式

JPA 條件查詢方式很有意思,是不需要寫 SQL 語句的,只需要在 dao 接口里按照規范的命名定義對應的方法名,及可達到查詢相應字段的效果了。
在如下代碼里做了如下事情:
1. 首先通過 @Before 把 Category表里所有數據都刪除了,並新增了10條。
2. 然后 test1() 查詢所有數據,看看新增的10條數據。
3. 接着,test2() 通過自定義的接口方法 findByName,根據name 查詢分類表
4. 接着,test3() 通過自定義的接口方法 findByNameLikeAndIdGreaterThanOrderByNameAsc,根據名稱模糊查詢,id 大於某值, 並且名稱正排序查詢。
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package  com.how2java.springboot.test;
 
import  java.util.List;
 
import  org.junit.Before;
import  org.junit.Test;
import  org.junit.runner.RunWith;
import  org.springframework.beans.factory.annotation.Autowired;
import  org.springframework.boot.test.context.SpringBootTest;
import  org.springframework.test.context.junit4.SpringRunner;
 
import  com.how2java.springboot.Application;
import  com.how2java.springboot.dao.CategoryDAO;
import  com.how2java.springboot.pojo.Category;
 
@RunWith (SpringRunner. class )
@SpringBootTest (classes = Application. class )
public  class  TestJPA {
 
     @Autowired  CategoryDAO dao;
     
     @Before
     public  void  before() {
         List<Category> cs=  dao.findAll();
         for  (Category c : cs) {
             dao.delete(c);
         }
         
         for  ( int  i =  0 ; i <  10 ; i++) {
             Category c =  new  Category();
             c.setName( "category "  + i);
             dao.save(c);
         }
         
     }
     
     @Test
     public  void  test1() {
         List<Category> cs=  dao.findAll();
         System.out.println( "所有的分類信息:" );
         for  (Category c : cs) {
             System.out.println(c.getName());
         }
         System.out.println();
     }
     
     @Test
     public  void  test2() {
         System.out.println( "查詢名稱是 \"category 1 \"的分類:" );
         List<Category> cs=  dao.findByName( "category 1" );
         for  (Category c : cs) {
             System.out.println( "c.getName():" + c.getName());
         }
         System.out.println();
     }
     @Test
     public  void  test3() {
         System.out.println( "根據名稱模糊查詢,id 大於5, 並且名稱正排序查詢" );
         List<Category> cs=  dao.findByNameLikeAndIdGreaterThanOrderByNameAsc( "%3%" , 5 );
         for  (Category c : cs) {
             System.out.println(c);
         }
         System.out.println();
         
     }
}

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
package  com.how2java.springboot.dao;
 
import  java.util.List;
 
import  org.springframework.data.jpa.repository.JpaRepository;
 
import  com.how2java.springboot.pojo.Category;
 
public  interface  CategoryDAO  extends  JpaRepository<Category,Integer>{
 
     public  List<Category> findByName(String name);
     
     public  List<Category> findByNameLikeAndIdGreaterThanOrderByNameAsc(String name,  int  id);
}
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package  com.how2java.springboot.pojo;
 
import  javax.persistence.Column;
import  javax.persistence.Entity;
import  javax.persistence.GeneratedValue;
import  javax.persistence.GenerationType;
import  javax.persistence.Id;
import  javax.persistence.Table;
 
@Entity
@Table (name =  "category_" )
public  class  Category {
 
     @Id
     @GeneratedValue (strategy = GenerationType.IDENTITY)
     @Column (name =  "id" )
     private  int  id;
     
     @Column (name =  "name" )
     private  String name;
     public  int  getId() {
         return  id;
     }
     public  void  setId( int  id) {
         this .id = id;
     }
     public  String getName() {
         return  name;
     }
     public  void  setName(String name) {
         this .name = name;
     }
     @Override
     public  String toString() {
         return  "Category [id="  + id +  ", name="  + name +  "]" ;
     }
     
}

 

實現原理

 
雖然 JPA 沒有自己手動寫sql 語句,但是通過反射獲取自定義的接口方法里提供的信息,就自導用戶希望根據什么條件來查詢了。 然后 JPA 底層再偷偷摸摸地拼裝對應的 sql 語句,丟給數據庫,就達到了條件查詢的效果啦。

對反射不熟悉的同學,可了解反射基礎教程:  反射基礎教程
 

條件查詢規范

JPA 條件查詢方式 只是個別舉例,下表把 jpa 做的各種查詢規范都列出來了。 如果要做其他相關查詢,按照表格中的規范設計接口方法即可。
 
http://download.how2j.cn/2004/springboot.rar
 
 


免責聲明!

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



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