关于@Query注解的用法(Spring|关于@Query注解的用法(Spring Data JPA)

目录

  • @Query注解的用法
    • 1.一个使用@Query注解的简单例子
    • 2.Like表达式
    • 3.使用NativeSQLQuery
    • 4.使用@Param注解注入参数
    • 5.SPEL表达式(使用时请参考最后的补充说明)
    • 6.一个较完整的例子
    • 7.解释例6中错误的原因
  • @Query注解使用详情
    • 常用属性
    • 取值方式
    • CRUD
【关于@Query注解的用法(Spring|关于@Query注解的用法(Spring Data JPA)】
@Query注解的用法
1.一个使用@Query注解的简单例子
@Query(value = "https://www.it610.com/article/select name,author,price from Book b where b.price>?1 and b.price findByPriceRange(long price1, long price2);


2.Like表达式
@Query(value = "https://www.it610.com/article/select name,author,price from Book b where b.name like %:name%")List findByNameMatch(@Param("name") String name);


3.使用Native SQL Query
所谓本地查询,就是使用原生的sql语句(根据数据库的不同,在sql的语法或结构方面可能有所区别)进行查询数据库的操作。
@Query(value = "https://www.it610.com/article/select * from book b where b.name=?1", nativeQuery = true)List findByName(String name);


4.使用@Param注解注入参数
@Query(value = "https://www.it610.com/article/select name,author,price from Book b where b.name = :name AND b.author=:author AND b.price=:price")List findByNamedParam(@Param("name") String name, @Param("author") String author,@Param("price") long price);


5.SPEL表达式(使用时请参考最后的补充说明)
'#{#entityName}'值为'Book'对象对应的数据表名称(book)。
public interface BookQueryRepositoryExample extends Repository{@Query(value = "https://www.it610.com/article/select * from #{#entityName} b where b.name=?1", nativeQuery = true)List findByName(String name); }


6.一个较完整的例子
public interface BookQueryRepositoryExample extends Repository {@Query(value = "https://www.it610.com/article/select * from Book b where b.name=?1", nativeQuery = true) List findByName(String name); // 此方法sql将会报错(java.lang.IllegalArgumentException),看出原因了吗,若没看出来,请看下一个例子@Query(value = "https://www.it610.com/article/select name,author,price from Book b where b.price>?1 and b.price findByPriceRange(long price1, long price2); @Query(value = "https://www.it610.com/article/select name,author,price from Book b where b.name like %:name%")List findByNameMatch(@Param("name") String name); @Query(value = "https://www.it610.com/article/select name,author,price from Book b where b.name = :name AND b.author=:author AND b.price=:price")List findByNamedParam(@Param("name") String name, @Param("author") String author,@Param("price") long price); }


7.解释例6中错误的原因
因为指定了nativeQuery = true,即使用原生的sql语句查询。使用java对象'Book'作为表名来查自然是不对的。只需将Book替换为表名book。
@Query(value = "https://www.it610.com/article/select * from book b where b.name=?1", nativeQuery = true)List findByName(String name);

补充说明:
有同学提出来了,例子5中用'#{#entityName}'为啥取不到值啊?
先来说一说'#{#entityName}'到底是个啥。从字面来看,'#{#entityName}'不就是实体类的名称么,对,他就是。
实体类Book,使用@Entity注解后,spring会将实体类Book纳入管理。默认'#{#entityName}'的值就是'Book'。
但是如果使用了@Entity(name = "book")来注解实体类Book,此时'#{#entityName}'的值就变成了'book'。
到此,事情就明了了,只需要在用@Entity来注解实体类时指定name为此实体类对应的表名。在原生sql语句中,就可以把'#{#entityName}'来作为数据表名使用。

@Query注解使用详情
常用属性
  • value : 取值,要么使用原生SQL,要么使用JPQL
  • nativeQuery :表示是否采用原生SQL,诸如select * from tableName

取值方式
1、使用:形参名
示例:
单个形参的情况
关于@Query注解的用法(Spring|关于@Query注解的用法(Spring Data JPA)
文章图片

多个形参的情况:
关于@Query注解的用法(Spring|关于@Query注解的用法(Spring Data JPA)
文章图片

2、使用?数值,数值表示形参位置,1表示第一个形参,依次内推
示例:
单个形参的情况:
关于@Query注解的用法(Spring|关于@Query注解的用法(Spring Data JPA)
文章图片

多个形参的情况:
关于@Query注解的用法(Spring|关于@Query注解的用法(Spring Data JPA)
文章图片

特殊情况:数值也可不写,若不写具体的数值,默认是从1开始递增,如下图示例:
关于@Query注解的用法(Spring|关于@Query注解的用法(Spring Data JPA)
文章图片

3、使用@Param("参数名")+:参数名
通常使用@Param注解都是在多个形参的情况下使用
关于@Query注解的用法(Spring|关于@Query注解的用法(Spring Data JPA)
文章图片

4、获取实体类名称,使用#{#entityName}
关于@Query注解的用法(Spring|关于@Query注解的用法(Spring Data JPA)
文章图片


CRUD
使用@Query注解实现删、改、查、增的示例,如下所示:
@Modifying@Transactional@Query(value = "https://www.it610.com/article/delete from User where id = ?1")void deleteByUserId(Integer id);

?后面的数值1,表示第一个形参的值,以此类推,如果方法有多个形参,数值也会依次递增,特殊情况,数值也可不写,若不写具体的数值,默认是从1开始递增
@Modifying@Transactional@Query("update User set email = ?1 where id = ?2")void updateUser(String email,Integer id);

@Query(value = "https://www.it610.com/article/select * from tb_userwhereemail like concat('%',?2,'%') and username like concat('%',?1,'%') ",nativeQuery = true)User findByUsernameAndEmail( String username, String email);

@Modifying@Transactional@Query(value = "https://www.it610.com/article/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);

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

    推荐阅读