Usage of @Query annotations (Spring Data JPA) Reference article: http://Www.tuicool.com/articles/jQJBNv 1. A simple example of using @query annotations @query (value="Select Name,author,price from book B where b.price>?1 and b.price<?2") List<Book> Findbypricerange (LongPrice1,LongPrice2); 2. Like expression @query (value="Select Name,author,price from book B where b.name like%:name%") List<Book> Findbynamematch (@Param ("name") String name); 3using native SQL query, the so-called local query is the operation of querying the database using native SQL statements (which may differ in terms of SQL syntax or structure depending on the database). @Query (Value="SELECT * from book B where B.name=?1", Nativequery =true) List<Book>findbyname (String name);4. Use the @param annotation injection parameter @query (value="Select Name,author,price from book B where b.name =: Name and B.author=:author and b.price=:p Rice") List<Book> Findbynamedparam (@Param ("name") String name, @Param ("author") String author, @Param (" Price")LongPrice ); 5. Spel expression (please refer to the last supplemental note when using)'#{#entityName}'Value is' Book'The data Table name (book) that corresponds to the object. Public InterfaceBookqueryrepositoryexample extends Repository<book, long>{@Query (value="SELECT * from #{#entityName} b where b.name=?1", Nativequery =true) List<Book>findbyname (String name);} 6. A more complete example of copying code Public InterfaceBookqueryrepositoryexample extends Repository<book, long>{@Query (value="SELECT * from book B where B.name=?1", Nativequery =true) List<Book> findbyname (String name);//This method SQL will be error (java.lang.IllegalArgumentException), see the reason, if not see, see the next example@Query (Value="Select Name,author,price from book B where b.price>?1 and b.price<?2") List<Book> Findbypricerange (LongPrice1,LongPrice2); @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=:p Rice") List<Book> Findbynamedparam (@Param ("name") String name, @Param ("author") String author, @Param (" Price")LongPrice );} Copy Code7. Explains the reason for the error in Example 6: Because Nativequery is specified=true, which is querying using native SQL statements. Working with Java objects' Book'It is not right to look at nature as a table name. Simply replace the book with the table name book. @Query (Value="SELECT * from book B where B.name=?1", Nativequery =true) List<Book> findbyname (String name);
Usage of spring data JPA @query