Usage of spring data JPA @query

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.