經過幾天的折騰,終于到了學習一個重量級的查詢方式上,使用@Query注解,使用注解有兩種方式,一種是JPQL的SQL語言方式,一種是原生SQL的語言,略有區別,后者我們更熟悉一些。話不多說,看代碼。
1、在CustomerRepository里添加
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
|
/** * 模糊匹配 * @param bauer * @return */ @Query ( "select c from Customer c where c.firstName=?1" ) Customer findByFirstName2(String bauer); @Query ( "select c from Customer c where c.lastName=?1 order by c.id desc" ) List<Customer> findByLastName2(String lastName); /** * 一個參數,匹配兩個字段 * @param name2 * @return * 這里Param的值和=:后面的參數匹配,但不需要和方法名對應的參數值對應 */ @Query ( "select c from Customer c where c.firstName=:name or c.lastName=:name order by c.id desc" ) List<Customer> findByName( @Param ( "name" ) String name2); /** * 一個參數,匹配兩個字段 * @param name * @return * 這里的%只能放在占位的前面,后面不行 */ @Query ( "select c from Customer c where c.firstName like %?1" ) List<Customer> findByName2( @Param ( "name" ) String name); /** * 一個參數,匹配兩個字段 * @param name * @return * 開啟nativeQuery=true,在value里可以用原生SQL語句完成查詢 */ @Query (nativeQuery = true ,value = "select * from Customer c where c.first_name like concat('%' ,?1,'%') " ) List<Customer> findByName3( @Param ( "name" ) String name); |
2、在CustomerController內添加?
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
|
/** * @Query注解方式查詢 * 查詢FirstName為指定字符串 */ @RequestMapping ( "/findByFirstName2" ) public void findByFirstName2(){ Customer customer = repository.findByFirstName2( "Bauer" ); if (customer!= null ){ System.out.println(customer.toString()); } System.out.println( "-------------------------------------------" ); } /** * @Query注解方式查詢 * 查詢LastName為指定字符串 */ @RequestMapping ( "/findByLastName2" ) public void findByLastName2(){ List<Customer> result = repository.findByLastName2( "Bauer" ); for (Customer customer:result){ System.out.println(customer.toString()); } System.out.println( "-------------------------------------------" ); } /** * @Query注解方式查詢, * 用@Param指定參數,匹配firstName和lastName */ @RequestMapping ( "/findByName" ) public void findByName(){ List<Customer> result = repository.findByName( "Bauer" ); for (Customer customer:result){ System.out.println(customer.toString()); } System.out.println( "-------------------------------------------" ); } /** * @Query注解方式查詢,使用關鍵詞like * 用@Param指定參數,firstName的結尾為e的字符串 */ @RequestMapping ( "/findByName2" ) public void findByName2(){ List<Customer> result = repository.findByName2( "e" ); for (Customer customer:result){ System.out.println(customer.toString()); } System.out.println( "-------------------------------------------" ); } /** * @Query注解方式查詢,模糊匹配關鍵字e */ @RequestMapping ( "/findByName3" ) public void findByName3(){ List<Customer> result = repository.findByName3( "e" ); for (Customer customer:result){ System.out.println(customer.toString()); } System.out.println( "-------------------------------------------" ); } |
可能看了上面的代碼有些疑惑,這里做一下解釋:
?加數字表示占位符,?1代表在方法參數里的第一個參數,區別于其他的index,這里從1開始
=:加上變量名,這里是與方法參數中有@Param的值匹配的,而不是與實際參數匹配的
JPQL的語法中,表名的位置對應Entity的名稱,字段對應Entity的屬性,詳細語法見相關文檔
要使用原生SQL需要在@Query注解中設置nativeQuery=true,然后value變更為原生SQL即可
參考:
官方文檔,http://docs.spring.io/spring-data/jpa/docs/current/reference/html/
DEMO,https://github.com/icnws/spring-data-jpa-demo
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:http://spring4all.com/article/104