Springboot JPA Implementation of additions, deletions, paging, sorting, transaction operations and other functions __springboot

Source: Internet
Author: User
today I would like to introduce some of the common operations of JPA in Springboot, such as: additions and deletions, paging, sorting, transaction operations and other functions.

Here are some common query operations in JPA:

    and---Equivalent to the AND keyword in SQL, such as findbyheightandsex (int height,char sex); public list<user> Findbyheightandse

   X (int height,char sex); An OR---is equivalent to an OR keyword in SQL, such as findbyheightorsex (int height,char sex); public list<user> findbyheightorsex (int h

    Eight,char sex); Between---Equivalent to the Between keyword in SQL, such as findbyheightbetween (int min, int max); public list<user> Findbyheightbet

    Ween (int min,int max); LessThan---equivalent to "<" in SQL, such as Findbyheightlessthan (int max); public list<user> Findbyheightlessthan (int ma

    x); GreaterThan---equivalent to ">" in SQL, such as Findbyheightgreaterthan (int min); public list<user> Findbyheightgreatertha

    n (int min);

    IsNull---Equivalent to "is null" in SQL, such as Findbynameisnull (); public list<user> findbynameisnull ();

    Isnotnull---Equivalent to "is not NULL" in SQL, such as Findbynameisnotnull (); public list<user> findbynameisnotnull (); Notnull---and isnotnull equivalence; public list<user> Findbynamenotnull ();
    The like---is equivalent to the SQL, such as Findbynamelike (String name);

    Public list<user> findbynamelike (String name); Notlike---Equivalent to "not like" in SQL, such as Findbynamenotlike (String name); public list<user> Findbynamenotlike (strin

    g name); The by-order---equivalent to the FINDBYNAMENOTNULLORDERBYHEIGHTASC () in SQL, such as the List<user>findbynamenotnullor ();

    DERBYHEIGHTASC (); Not---is equivalent to "in SQL.

    = ", such as Findbynamenot (string name), public list<user> Findbynamenot (string name);
    In---is equivalent to ' in ' in SQL, such as Findbynamein (String name);

    Public list<user> Findbynamein (String name);
    The notin---equivalent to ' not in ' in SQL, such as Findbynamenotin (String name); Public list<user> Findbynamenotin (String name);
The style in JPA is this: Each method is actually a SQL command, with some keywords to implement commands such as like in and so on in SQL.

The most important thing is that we are in the process of development, only need to write a DAO in one of the methods, do not need us to write DAO implementation class, this can be very high code reuse rate, improve our development efficiency.

There will inevitably be people asking, "How are some of the more complicated queries to be implemented, and the JPA approach is to use native SQL commands to implement complex relational queries, and look at the case below."

Query operation using native SQL
    @Query (value = "Select o.* from Orders o, user U where o.uid=u.id and u.name=?1", Nativequery = True)
    @Modifying public
    list<order> findorderbyname (String name);

    Delete operation using native SQL
    @Query (value = "Delete from orders where id=?1", Nativequery = True)
    @Modifying public
    Void Deleteorderbyid (int id);

    Delete operation using native SQL
    @Query (value = "Delete from orders where uid=?1", Nativequery = True)
    @Modifying public
    vo ID deleteorderbyuid (int uid);

    Modify operations using native SQL
    @Query (value = "Update orders set name=?1 where id=?2", Nativequery = True)
    @Modifying
    p ublic void Updateordername (String name,int ID);

    Insert operation using native SQL
    @Query (value = INSERT INTO orders (NAME,UID) value (? 1,?2), Nativequery = True)
    @Modifying c18/>public void InsertOrder (String name,int uid);
In the case above, the use of JPA to implement native SQL operations can be very convenient for database table operations.
So if the query is not very complex, the query time requirements are not particularly demanding projects, can be used in JPA for project development.

Here's how JPA realizes the paging effect, in fact JPA is derived from hibernate, so it has a good support for paging function. Here's a concrete example:

    Implementation of paging functionality
    page<user> findbynamenot (String name,pageable pageable);
@RequestMapping (value = "/params", method= requestmethod.get)
    @ResponseBody public
    String Getentrybyparams (@ Requestparam (value = "Name", defaultvalue = "Lin Zhiqiang") String name, @RequestParam (value = "Page", DefaultValue = "0") Integer Page, @RequestParam (value = "Size", DefaultValue = ") Integer size" {
        sort sort = new Sort (Sort.Direction.DESC, "id ");
        pageable pageable = new Pagerequest (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.

Add a return value to page in the DAO layer, and the parameter value is pageable. The controller layer pageable this class by instantiating it, and then invokes the paging method of the DAO layer.

Through these steps you can easily achieve the effect of paging, it does not look particularly convenient.

Finally, I'm giving you an introduction to how JPA implements transactional operations. In fact, because the Springboot has done a very good encapsulation of the transaction, it is particularly convenient to use. Here's a look at the case:

    @RequestMapping ("/saveorder")
    @ResponseBody
    @Transactional () public
    String Saveorder () {Order
        o1= New Order ("one", 2);
        Order O2=new ("2");
        Order O3=new ("2");
        Order O4=new ("2");
        Orderdao.save (O1);
        Orderdao.save (O2);
        Orderdao.save (O3);
        Orderdao.save (O4);
        Return "Successfull....saveorder ...";
    }
Just add the top of the method @TransactionThis annotation can be easily easy to achieve the operation of the transaction, is not particularly convenient ah.

But here are a few things to be aware of:

1. This annotation implementation of the transaction manager is the default, if you do not want to default is the transaction manager, you can add, I do not introduce more here.

2. The isolation level of the transaction can also set itself, specific please see this blog click on the Open link

3. The dissemination behavior of the transaction also can set itself, specific please see this blog click on Open link

Here are some common methods for JPA, thank you for your reading.

If you have any questions or doubts about the article, you can add my subscription number in the above message, the subscription number above I will regularly update the latest blog. If it's too much trouble, you can just add me wechat:lzqcode






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.