关于@Query注解的用法(Spring Data JPA)
目录
@Query注解的用法
1.一个使用@Query注解的简单例子
2.Like表达式
3.使用NativeSQLQuery
4.使用@Param注解注入参数
5.SPEL表达式(使用时请参考最后的补充说明)
6.一个较完整的例子
7.解释例6中错误的原因
@Query注解使用详情
常用属性
取值方式
CRUD
@Query注解的用法
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);
更多相关文章
- Android(安卓)NDK 知识系列(五)
- Android在layout xml中使用include
- Tab形式的菜单实现总结TabHost 和 FragmentTabHost
- android的socket开发注意事项
- Android帮助文档(第二部分)开发工具
- 【Demo 0005】Android(安卓)资源
- Android中使用WebView, WebChromeClient和WebViewClient加载网页
- Android(安卓)网络请求库Retrofit简单使用
- 使用eclipse创建android工程时得到警告