Spring Data JPA Learning Notes

Source: Internet
Author: User

Let's look at some common query operations in JPA:

 //and---are equivalent to the AND keyword in SQL, such as findbyheightandsex (int height,char sex);  PublicList<user> Findbyheightandsex (intHeightCharsex); //The or---is equivalent to the OR keyword in SQL, such as findbyheightorsex (int height,char sex);  PublicList<user> Findbyheightorsex (intHeightCharsex); //The between---is equivalent to the between keyword in SQL, such as findbyheightbetween (int min, int max);  PublicList<user> Findbyheightbetween (intMinintmax); //The LessThan---is equivalent to "<" in SQL, such as Findbyheightlessthan (int max);  PublicList<user> Findbyheightlessthan (intmax); //GreaterThan---is equivalent to ">" in SQL, such as Findbyheightgreaterthan (int min);  PublicList<user> Findbyheightgreaterthan (intmin); //IsNull---is equivalent to "is null" in SQL, such as Findbynameisnull ();  PublicList<user>Findbynameisnull (); //isnotnull---is equivalent to "is not NULL" in SQL, such as Findbynameisnotnull ();  PublicList<user>Findbynameisnotnull (); //notnull---and isnotnull equivalence;  PublicList<user>Findbynamenotnull (); //The like---is equivalent to the "as" in SQL, such as Findbynamelike (String name);   PublicList<user>findbynamelike (String name); //The notlike---is equivalent to "not like" in SQL, such as Findbynamenotlike (String name);  PublicList<user>findbynamenotlike (String name); //The order-by---is equivalent to the "FINDBYNAMENOTNULLORDERBYHEIGHTASC" in SQL, such as the ();  PublicList<user>Findbynamenotnullorderbyheightasc (); //The NOT ---is equivalent to "in SQL"! = ", such as Findbynamenot (String name);  PublicList<user>Findbynamenot (String name); //In ---is equivalent to ' in ' in SQL, such as Findbynamein (String name);   PublicList<user>Findbynamein (String name); //The notin---is equivalent to "not in" SQL, such as Findbynamenotin (String name);   PublicList<user> Findbynamenotin (String name);

The style in JPA is like this, and each method is actually a SQL command, with some keywords to implement a command such as like in and so on in SQL.

The most important thing is that in the process of re-development, we only need to write a DAO in one method, do not need us to write the DAO implementation class, so that can greatly high code reuse rate, improve our development efficiency.

There will be people who ask, how do some of the more complex related queries to be implemented, the JPA approach is: the use of native SQL commands to implement those complex related queries, the following look at the case

//query operations with native SQL@Query (value = "Select o.* from Orders o, user U where o.uid=u.id and u.name=?1", Nativequery =true) @Modifying PublicList<order>findorderbyname (String name); //delete operations with native SQL@Query (value = "Delete from orders where id=?1", Nativequery =true) @Modifying Public voidDeleteorderbyid (intID); //delete operations with native SQL@Query (value = "Delete from orders where uid=?1", Nativequery =true) @Modifying Public voidDeleteorderbyuid (intuid); //modify operations with native SQL@Query (value = "Update orders set name=?1 where id=?2", Nativequery =true) @Modifying Public voidUpdateordername (String name,intID); //insert operations with native SQL@Query (value = "INSERT into orders (NAME,UID) value (? 1,?2)", Nativequery =true) @Modifying Public voidInsertOrder (String name,intUID);

The above example shows that the use of JPA to achieve native SQL operations, can be very convenient for database table operation.
So if the query statement is not very complex, the query time requirements are not particularly harsh projects, can be used in the JPA project development.

The following is the introduction of how JPA is the effect of paging, in fact, JPA derived from Hibernate, so the paging function itself is very good support. Here's a concrete example:

// Implementing paging Functionality  

@RequestMapping (value = "/params", method=requestmethod.get) @ResponseBody PublicString Getentrybyparams (@RequestParam (value = "Name", defaultvalue = "Zhiqianglin") string name, @RequestParam (value = "Page", Def Aultvalue = "0") Integer page, @RequestParam (value = "Size", DefaultValue = "15") (Integer size) {sort sort=NewSort (Sort.Direction.DESC, "id"); pageable pageable=Newpagerequest (page, size, sort); Page<User> pages=Userdao.findbynamenot (name,pageable); Iterator<User> it=Pages.iterator ();  while(It.hasnext ()) {System.out.println ("Value:" +( User) It.next ()). GetId ()); }             return"Success...login ..."; }  

The code above is in the DAO layer, and one is in the controller.

A return value of page is added to the DAO layer, and the parameter value is pageable. In the controller layer, the Pageable class is instantiated, and then the DAO layer is called as a paging method.

With these steps you can easily achieve the effect of paging, it seems that is not particularly convenient.

Finally, let's introduce how JPA implements transactional operations. In fact, because the springboot has done a good job in the package, the use of special convenience. Here's a look at the case:

 @RequestMapping ("/saveorder" ) @ResponseBody @Transactional ()  public   String Saveorder () {order O1  =new  Order          ("One", 2 =new  order ("All", 2 =new  order ("All", 2 =new  Order ("", 2 return  "Successfull....saveorder ..."      

As long as the above method to add @Transaction This annotation can be easily realized the operation of the transaction, is not particularly convenient ah.

Spring Data JPA Learning Notes

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.