spring data jpa 查詢規範
按照 spring data 的規範,查詢方法以findby
|readby
|getby
開頭
//例如:定義乙個 entity 實體類
class user{
private string firstname;
private string lastname;
} //使用and條件連線時,應這樣寫:
findbylastnameandfirstname(string lastname,string firstname);
//條件的屬性名稱與個數要與引數的位置與個數一一對應
直接在介面中定義查詢方法,如果是符合規範的,可以不用寫實現,目前支援的關鍵字寫法如下:
關鍵字簡單示例
jpql片段示例
andfindbylastnameandfirstname
where entity.lastname = ?1 and entity.firstname = ?2
orreadbylastnameorfirstname
where entity.lastname = ?1 or entity.firstname = ?2
between
getbystartdatebetween
where entity.startdate between ?1 and ?2
lessthan
findbyagelessthan
where entity.age < ?1
greaterthan
readbyagegreaterthan
where entity.age > ?1
after只作用在時間
findbystartdateafter
where entity.startdate > ?1
before只作用在時間
findbystartdatebefore
where entity.startdate < ?1
lessthanequal
findbyagelessthenequal
where entity.age <= ?1
greaterthanequal
readbyagegreaterthenequal
where entity.age >= ?1
isfindbylastnameis
where entity.lastname = ?1
equal
getbyfirstnameequal
where entity.firstname = ?1
isnull
findbyaddressisnull
where entity.address is null
isnotnull
readbyaddressisnotnull
where entity.address not null
notnull
readbyaddressnotnull
where entity.address not null
like
findbynamelike
where entity.name like ?不包括'%'符號
not like
findbynamenotlike
where entity.name not like ?1不包括'%'符號
startingwith
readbynamestartingwith
where entity.name like 『?1%』條件以'%'符號結尾
endingwith
readbyname endingwith
where entity.name like 『%?1』條件以'%'符號開頭
containing
getbynamecontaining
where entity.name like 『%?1%』包含'%'符號
orderby
findbyageorderbyaddressdesc
where entity.age = ? order by entity.address desc
notreadbyagenot
where entity.age <> ?1不等於
infindbynamein(collection name)
where entity.name in (?1 ,?2, ?3)
notin
getbynamenotin(collection name)
where entity.name not in (?1 ,?2, ?3)
true
readbyfloagtrue()
where entity.floag = true
false
readbyfloagfalse()
where entity.floag = false
ignorecase
findbynameignorecase
where upper(entity.name) = upper(?1)
SpringData JPA分頁查詢
首先我們需要知道springdata jpa 的幾個介面 其實看名字就大概懂了,也可以很方便的使用 首先我們的持久化層繼承jparepository,相當於繼承了增刪改查的持久化層以及分頁查詢的持久化層 所以如果我們要使用分頁查詢 我們只需要直接呼叫 由一開始的圖也可以看到pageable的其中乙個...
springData Jpa簡單查詢
一 介面方法整理速查 下表針對於簡單查詢,即jparepository介面 繼承了crudrepository介面 pagingandsortingrepository介面 中的可訪問方法進行整理。1 先按照功能進行分類整理,分為儲存 刪除 查詢單個 查詢多個 其他5類。2 再將不建議使用的方法置灰...
SpringData JPA多表查詢
物件導航查詢 查詢乙個物件的同時,通過此物件查詢他的關聯物件 前提得在實體類中配置它們的關聯關係 物件導航查詢 預設使用的是延遲載入的形式查詢 呼叫get方法並不會立即傳送查詢,而是在使用關聯物件的時候才會查詢 將延遲載入改為立即載入需要修改配置 fetch,需要配置到多表對映關係的註解上 priv...