SpringData JPA的複雜查詢

2021-10-02 06:47:08 字數 4012 閱讀 9253

使用@query註解,結合jpql的語句方式完成查詢

/**

* jparepository《實體類型別,主鍵型別》:用來完成基本crud操作

* jpaspecificationexecutor《實體類型別》:用於複雜查詢(分頁等查詢操作)

*/public

inte***ce

customerdao

extends

jparepository

, jpaspecificationexecutor

測試方法:

@test

public

void

testfindallcustomer()

}@test

public

void

testfindcustomer()

}@test

@transactional

// 缺省會回滾

@rollback

(false

)// 不自動回滾

public

void

testqupdata()

/**

* nativequery : 使用本地sql的方式查詢

*/@query

(value=

"select * from cst_customer"

,nativequery=

true

)public list

findsql()

;@query

(value=

"select * from cst_customer where cust_name like ?1"

,nativequery=

true

)public list

findsqlbyname

(string nama)

;

/**

* 方法名的約定:

* findby : 查詢

* 物件中的屬性名(首字母大寫) : 查詢的條件

* custname

* * 預設情況 : 使用 等於的方式查詢

* 特殊的查詢方式

** findbycustname -- 根據客戶名稱查詢

** 再springdatajpa的執行階段

* 會根據方法名稱進行解析 findby from ***(實體類)

* 屬性名稱 where custname =

** 1.findby + 屬性名稱 (根據屬性名稱進行完成匹配的查詢=)

* 2.findby + 屬性名稱 + 「查詢方式(like | isnull)」

* findbycustnamelike

* 3.多條件查詢

* findby + 屬性名 + 「查詢方式」 + 「多條件的連線符(and|or)」 + 屬性名 + 「查詢方式」

*///方法命名方式查詢(根據客戶名稱查詢客戶)

public list

findbycustname

(string custname)

;

keyword

sample

jpql

andfindbylastnameandfirstname

… where x.lastname = ?1 and x.firstname = ?2

orfindbylastnameorfirstname

… where x.lastname = ?1 or x.firstname = ?2

is,equals

findbyfirstnameis,findbyfirstnameequals

… where x.firstname = ?1

between

findbystartdatebetween

… where x.startdate between ?1 and ?2

lessthan

findbyagelessthan

… where x.age < ?1

lessthanequal

findbyagelessthanequal

… where x.age <=?1

greaterthan

findbyagegreaterthan

… where x.age > ?1

greaterthanequal

findbyagegreaterthanequal

… where x.age >= ?1

after

findbystartdateafter

… where x.startdate > ?1

before

findbystartdatebefore

… where x.startdate < ?1

isnull

findbyageisnull

… where x.age is null

isnotnull,notnull

findbyage(is)notnull

… where x.age not null

like

findbyfirstnamelike

… where x.firstname like ?1

notlike

findbyfirstnamenotlike

… where x.firstname not like ?1

startingwith

findbyfirstnamestartingwith

endingwith

findbyfirstnameendingwith

… where x.firstname like ?1 (parameter bound with prepended %)

containing

findbyfirstnamecontaining

orderby

findbyageorderbylastnamedesc

… where x.age = ?1 order by x.lastname desc

notfindbylastnamenot

… where x.lastname <> ?1

infindbyagein(collection ages)

… where x.age in ?1

notin

findbyagenotin(collection age)

… where x.age not in ?1

true

findbyactivetrue()

… where x.active = true

false

findbyactivefalse()

… where x.active = false

ignorecase

findbyfirstnameignorecase

… where upper(x.firstame) = upper(?1)

spring data jpa遇到的坑

1 column註解name最好全寫了,不然遇到像hibernate駝峰和下劃線風格配置變更時會出現找不到屬性,尤其開啟自動建表情況 2 唯讀操作情況,如果對讀取物件進行了屬性修改操作,那麼jpa會認為你要修改這個實體,使用hibernate的情況下回預設執行乙個update的sql,然後你懂得由於...

spring Data Jpa的分頁管理

通過jpa大大簡化了我們對資料庫的開發工作。但是,之前的例子中我們只提到了最簡單的crud 增刪改查 操作。實際上,spring data jpa對於分頁以及排序的查詢也有著完美的支援,接下來,我們來學習如何通過pageable來對資料庫進行分頁查詢。一 pageable 是spring data庫...

SpringData Jpa中count的使用

通常,我們會有統計數量的需求,jpa對一些簡單統計數量的需求通過方法名就可以解析。然而對於稍微複雜的需求則無法通過方法名解析。對於這種需求,還是需要寫sql實現。當我的repository介面繼承jparepository時,缺省會繼承它的乙個count 方法 crudrepository介面中有c...