Spring Data Jpa 使用@Query標註自定義查詢語句
阿新 • • 發佈:2019-02-04
在使用JPA訪問關係型資料庫和Spring
Data Jpa: 分頁和排序兩篇文章中我們學會了如何使用Spring Data Jpa 進行簡單的查詢以及分頁等功能,Spring Data Jpa本身所支援的功能已經非常強大了,也能夠支援大部分的場景。但是,現實場景永遠比想象的要複雜,有時候我們確實需要像Sql語句這樣更加強大以及靈活的方式來進行查詢。Spring Data Jpa 當然給我們提供了這樣的方式,我們可以使用@Query
標註輕鬆的使用類似sql查詢的功能。今天我們來嘗試一下如何使用@Query
標註來自定義查詢吧。
基礎物件
Spring Data Jpa所提供的通過方法名進行查詢的功能已經能夠覆蓋絕大多數單表查詢了。但是,我們的查詢絕不僅限於單表查詢,很多時候我們還是需要進行多表查詢的,因此我們今天設計兩個表,blog
user
,通過這兩個表的聯合查詢來試驗@Query
標註。
@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
private String username;
private String role;
......
}
@Entity
public class Blog {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
private String title;
private String content;
@ManyToOne
private User creator;
......
}
關聯查詢
blog表和user表通過creator相關聯。在user表中,我們設計了一個屬性角色--role
。如果我們想找到某種角色的使用者所建立的blog列表應該怎麼辦?讓我們通過@Query
標註去實現它吧:
public interface BlogRepository extends PagingAndSortingRepository <Blog, Integer> {
@Query("select blog from Blog blog join blog.creator creator where creator.role = ?1")
Page<Blog> findByRole(String role, Pageable pageable);
}
在BlogRepository中,我們通過@Query標註使用了HQL進行查詢,通過它,我們可以更加靈活的進行各種查詢(當然,Spring Data JPA同樣支援多表聯查),如果不喜歡使用方法名來定義查詢條件或者查詢過於複雜的話,@Query標註是一個很不錯的選擇。
查詢結果
我們來寫一個Controller展示我們的查詢結果:
@RestController
public class TestController {
@Autowired BlogRepository blogRepository;
@RequestMapping(value = "", method=RequestMethod.GET)
public Page<Blog> getEntryByPageable(@PageableDefault(value = 15, sort = { "id" }, direction = Sort.Direction.DESC)
Pageable pageable, @RequestParam(value = "role", defaultValue = "") String role) {
if("".equals(role)){
return blogRepository.findAll(pageable);
}
return blogRepository.findByRole(role, pageable);
}
}
然後,我們進入根目錄,執行mvn spring-boot:run
將應用run起來。所有資料會在應用啟動時新增進資料庫當中。然後我們訪問http://localhost:8080/,我們可以得到所有blog的分頁結果:
{
"content":[
{
"id":246,
"title":"blog122",
"content":"this is teacher blog content",
"creator":{"id":1,"username":"teacher","role":"teacher"}
},
{
"id":245,
"title":"blog121",
"content":"this is teacher blog content",
"creator":{"id":1,"username":"teacher","role":"teacher"}
},
{
"id":244,
"title":"blog120",
"content":"this is teacher blog content",
"creator":{"id":1,"username":"teacher","role":"teacher"}
},
{
"id":243,
"title":"blog119",
"content":"this is teacher blog content",
"creator":{"id":1,"username":"teacher","role":"teacher"}
},
{
"id":242,
"title":"blog118",
"content":"this is teacher blog content",
"creator":{"id":1,"username":"teacher","role":"teacher"}
}
],
"last":false,
"totalElements":246,
"totalPages":50,"size":5,
"number":0,
"first":true,
"sort":[{"direction":"DESC","property":"id","ignoreCase":false,"nullHandling":"NATIVE","ascending":false}],
"numberOfElements":5
}
{
"content":[
{"id":123,"title":"blog122","content":"this is student blog content","creator":{"id":2,"username":"student","role":"student"}},
{"id":122,"title":"blog121","content":"this is student blog content","creator":{"id":2,"username":"student","role":"student"}},
{"id":121,"title":"blog120","content":"this is student blog content","creator":{"id":2,"username":"student","role":"student"}},
{"id":120,"title":"blog119","content":"this is student blog content","creator":{"id":2,"username":"student","role":"student"}},
{"id":119,"title":"blog118","content":"this is student blog content","creator":{"id":2,"username":"student","role":"student"}}
],
"last":false,
"totalElements":123,
"totalPages":25,
"size":5,
"number":0,
"first":true,
"sort":[{"direction":"DESC","property":"id","ignoreCase":false,"nullHandling":"NATIVE","ascending":false}],
"numberOfElements":5}
版權宣告本文由