@Query注解的用法(Spring Data JPA)
来源:互联网 发布:问道辅助软件 编辑:程序博客网 时间:2024/05/18 01:29
参考文章:http://www.tuicool.com/articles/jQJBNv
1. 一个使用@Query注解的简单例子
@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表达式
@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语句(根据数据库的不同,在sql的语法或结构方面可能有所区别)进行查询数据库的操作。
@Query(value = "select * from book b where b.name=?1", nativeQuery = true)List<Book> findByName(String name);
4. 使用@Param注解注入参数
@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表达式(使用时请参考最后的补充说明)
'#{#entityName}'值为'Book'对象对应的数据表名称(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. 一个较完整的例子
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将会报错(java.lang.IllegalArgumentException),看出原因了吗,若没看出来,请看下一个例子 @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中错误的原因:
因为指定了nativeQuery = true,即使用原生的sql语句查询。使用java对象'Book'作为表名来查自然是不对的。只需将Book替换为表名book。
@Query(value = "select * from book b where b.name=?1", nativeQuery = true)List<Book> findByName(String name);
补充说明(2017-01-12):
有同学提出来了,例子5中用'#{#entityName}'为啥取不到值啊?
先来说一说'#{#entityName}'到底是个啥。从字面来看,'#{#entityName}'不就是实体类的名称么,对,他就是。
实体类Book,使用@Entity注解后,spring会将实体类Book纳入管理。默认'#{#entityName}'的值就是'Book'。
但是如果使用了@Entity(name = "book")来注解实体类Book,此时'#{#entityName}'的值就变成了'book'。
到此,事情就明了了,只需要在用@Entity来注解实体类时指定name为此实体类对应的表名。在原生sql语句中,就可以把'#{#entityName}'来作为数据表名使用。
阅读全文
0 0
- @Query注解的用法(Spring Data JPA)
- @Query注解的用法(Spring Data JPA)
- @Query注解的用法(Spring Data JPA)
- @Query注解的用法(Spring Data JPA)
- @Query注解的用法(Spring Data JPA)
- Spring Data JPA 常用注解 @Query、@NamedQuery
- Spring data jpa 之 Query注解
- Spring Data JPA进阶-@Query注解
- Spring Data Jpa @Query Pageable用法
- spring data jpa @Query注解中delete语句报错 : @Modifying注解的使用
- Spring Data JPA @Query
- spring data jpa @Query注解中delete语句报错
- Spring Data JPA 使用@Query注解执行原始Sql
- spring-data-jpa @Query注解中delete语句报错
- Spring Data JPA Using @Query
- spring data jpa的常用注解
- jpa学习5--spring data jpa 中的JpaRepository 使用 @Query 注解
- spring DATA (jpa) @query 和JdbcTemplate
- java poi 读写操作excel
- [Unity&Animator]拖动Animator面板的问题
- 加载和连接
- Android自定义View之分类和核心函数
- tgt + rbd + multiph
- @Query注解的用法(Spring Data JPA)
- JAVA OOP(二)——方法的重载、构造方法以及this关键字
- TCP/IP协议详解
- LeetCode:Reorder List
- 升级到Oracle 11g后,SAP数据库用户密码过期
- eclipse配置maven + 创建maven项目
- 第七届大数据世界论坛(BDWF 2017)邀您见证大数据在2017的爆发!
- 事务的传播行为和隔离级别
- 彻底理解ThreadLocal