Query註解簡單使用

2021-09-12 06:23:08 字數 1390 閱讀 5686

nativequery=true 本地查詢,就是使用原生的sql語句,直接查詢資料表名,而不是實體類物件(根據資料庫的不同,在sql的語法或結構方面可能有所區別)進行查詢資料庫的操作。不設定nativequery=true 時,sql語句中的表名和欄位名時實體類名和實體類中的欄位名,兩種方式作用一樣,只是方法不同

@query(value = "select * from book b where b.name=?1", nativequery = true)

listfindbyname(string name);

book為資料庫中真實存在的資料表,name為book表中的字段。

1. 乙個使用@query註解的簡單例子:佔位符?1和?2

@query(value = "select name,author,price from book b whereb.price>?1 and b.price<?2")

listfindbypricerange(long price1, long price2);

2.  like表示式:指定引數 :name,下面要用@param("name")指明對應的引數

@query(value="select * from actor_info where school like '%' :school '%'",nativequery = true)

listfindbyschool(@param("school")string school);

3. 使用@param註解注入引數

@query(value = "select name,author,price from book b whereb.name = :name and b.author=:author and b.price=:price") listfindbynamedparam(@param("name") string name, @param("author") string author,@param("price") long price);

4. spel表示式( 特別說明:本條可能有誤,待驗證,不使用#,直接使用表名是ok的 )

此處的 '#'值為'book'物件對應的資料表名稱(book)。

實體類book,使用@entity註解後,spring會將實體類book納入管理。此時預設'#'的值就是'book'。

但是如果使用了@entity(name = "book")來註解實體類book,此時'#'的值就變成了'book'。

public inte***ce bookqueryrepositoryexample extends repository b where b.name=?1", nativequery = true)

listfindbyname(string name);

Query註解使用詳情

value 取值,要麼使用原生sql,要麼使用jpql nativequery 表示是否採用原生sql,諸如select from tablename 1 使用 形參名 示例 單個形參的情況 多個形參的情況 2 使用?數值,數值表示形參位置,1表示第乙個形參,依次內推 示例 單個形參的情況 多個形參...

springData學習(二) Query註解詳解

query註解查詢適用於所查詢的資料無法通過關鍵字查詢得到結果的查詢。這種查詢可以擺脫像關鍵字查詢那樣的約束,將查詢直接在相應的介面方法中宣告,結構更為清晰,這是spring data的特有實現。1.查詢id最大的資料 需要注意的是from後面跟的是與表名相對應的實體類名 同時返回值不能寫 retu...

java註解簡單使用

在發開過程中一直都有使用到註解,今天總算想起來研究一下註解的實現過程了。直接開車吧 自定義註解 這裡已經實現了annotation 注意,註解只有在反射呼叫的方式下才能有效 retention value retentionpolicy.runtime target value public int...