關(guān)于@Query注解的用法(Spring Data JPA)
@Query注解的用法
1.一個(gè)使用@Query注解的簡(jiǎn)單例子
@Query(value = "select name,author,price from Book b where b.price>?1 and b.price<?2") List<Book> findByPriceRange(long price1, long price2);
2.Like表達(dá)式
@Query(value = "select name,author,price from Book b where b.name like %:name%") List<Book> findByNameMatch(@Param("name") String name);
3.使用Native SQL Query
所謂本地查詢,就是使用原生的sql語句(根據(jù)數(shù)據(jù)庫的不同,在sql的語法或結(jié)構(gòu)方面可能有所區(qū)別)進(jìn)行查詢數(shù)據(jù)庫的操作。
@Query(value = "select * from book b where b.name=?1", nativeQuery = true) List<Book> findByName(String name);
4.使用@Param注解注入?yún)?shù)
@Query(value = "select name,author,price from Book b where b.name = :name AND b.author=:author AND b.price=:price") List<Book> findByNamedParam(@Param("name") String name, @Param("author") String author, ? ? ? ? @Param("price") long price);
5.SPEL表達(dá)式(使用時(shí)請(qǐng)參考最后的補(bǔ)充說明)
'#{#entityName}'值為'Book'對(duì)象對(duì)應(yīng)的數(shù)據(jù)表名稱(book)。
public interface BookQueryRepositoryExample extends Repository<Book, Long>{ ? ? ? ?@Query(value = "select * from #{#entityName} b where b.name=?1", nativeQuery = true) ? ? ? ?List<Book> findByName(String name); }
6.一個(gè)較完整的例子
public interface BookQueryRepositoryExample extends Repository<Book, Long> { ? ? @Query(value = "select * from Book b where b.name=?1", nativeQuery = true)? ? ? List<Book> findByName(String name);// 此方法sql將會(huì)報(bào)錯(cuò)(java.lang.IllegalArgumentException),看出原因了嗎,若沒看出來,請(qǐng)看下一個(gè)例子 ? ? @Query(value = "select name,author,price from Book b where b.price>?1 and b.price<?2") ? ? List<Book> findByPriceRange(long price1, long price2); ? ? @Query(value = "select name,author,price from Book b where b.name like %:name%") ? ? List<Book> findByNameMatch(@Param("name") String name); ? ? @Query(value = "select name,author,price from Book b where b.name = :name AND b.author=:author AND b.price=:price") ? ? List<Book> findByNamedParam(@Param("name") String name, @Param("author") String author, ? ? ? ? ? ? @Param("price") long price); }
7.解釋例6中錯(cuò)誤的原因
因?yàn)橹付薾ativeQuery = true,即使用原生的sql語句查詢。使用java對(duì)象'Book'作為表名來查自然是不對(duì)的。只需將Book替換為表名book。
@Query(value = "select * from book b where b.name=?1", nativeQuery = true) List<Book> findByName(String name);
補(bǔ)充說明:
有同學(xué)提出來了,例子5中用'#{#entityName}'為啥取不到值???
先來說一說'#{#entityName}'到底是個(gè)啥。從字面來看,'#{#entityName}'不就是實(shí)體類的名稱么,對(duì),他就是。
實(shí)體類Book,使用@Entity注解后,spring會(huì)將實(shí)體類Book納入管理。默認(rèn)'#{#entityName}'的值就是'Book'。
但是如果使用了@Entity(name = "book")來注解實(shí)體類Book,此時(shí)'#{#entityName}'的值就變成了'book'。
到此,事情就明了了,只需要在用@Entity來注解實(shí)體類時(shí)指定name為此實(shí)體類對(duì)應(yīng)的表名。在原生sql語句中,就可以把'#{#entityName}'來作為數(shù)據(jù)表名使用。
@Query注解使用詳情
常用屬性
value
: 取值,要么使用原生SQL,要么使用JPQLnativeQuery
:表示是否采用原生SQL,諸如select * from tableName
取值方式
1、使用:形參名
示例:
單個(gè)形參的情況
多個(gè)形參的情況:
2、使用?數(shù)值,數(shù)值表示形參位置,1表示第一個(gè)形參,依次內(nèi)推
示例:
單個(gè)形參的情況:
多個(gè)形參的情況:
特殊情況:數(shù)值也可不寫,若不寫具體的數(shù)值,默認(rèn)是從1開始遞增,如下圖示例:
3、使用@Param("參數(shù)名")+:參數(shù)名
通常使用@Param注解都是在多個(gè)形參的情況下使用
4、獲取實(shí)體類名稱,使用#{#entityName}
CRUD
使用@Query注解實(shí)現(xiàn)刪、改、查、增的示例,如下所示:
- 刪
@Modifying @Transactional @Query(value = "delete from User where id = ?1") void deleteByUserId(Integer id);
?后面的數(shù)值1,表示第一個(gè)形參的值,以此類推,如果方法有多個(gè)形參,數(shù)值也會(huì)依次遞增,特殊情況,數(shù)值也可不寫,若不寫具體的數(shù)值,默認(rèn)是從1開始遞增
- 改
@Modifying @Transactional @Query("update User set email = ?1 where id = ?2") void updateUser(String email,Integer id);
- 查
@Query(value = "select * from tb_user ?where ?email like concat('%',?2,'%') and username like concat('%',?1,'%') ",nativeQuery = true) User findByUsernameAndEmail( String username, String email);
- 增
@Modifying @Transactional @Query(value = "insert into tb_user(email,id_card,username,wage) values (:email,:idCard,:username,:wage)",nativeQuery = true) void insertUser(String email,String idCard,String username,Double wage);
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
怎樣通過反射獲取非靜態(tài)內(nèi)部類實(shí)例
這篇文章主要介紹了怎樣通過反射獲取非靜態(tài)內(nèi)部類實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03SpringMVC中轉(zhuǎn)發(fā)與重定向的區(qū)別淺析
這篇文章主要給大家介紹了關(guān)于SpringMVC中轉(zhuǎn)發(fā)與重定向的區(qū)別,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12基于Spring定時(shí)任務(wù)的fixedRate和fixedDelay的區(qū)別
這篇文章主要介紹了基于Spring定時(shí)任務(wù)的fixedRate和fixedDelay的區(qū)別,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10java 文件目錄讀寫刪除操作詳細(xì)實(shí)現(xiàn)代碼
這篇文章主要介紹了java 文件讀寫刪操作詳細(xì)實(shí)現(xiàn)代碼,需要的朋友可以參考下2017-09-09JDK8新特性-java.util.function-Function接口使用
這篇文章主要介紹了JDK8新特性-java.util.function-Function接口使用,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-04-04