Spring Data JPA Simple Query-interface method

Source: Internet
Author: User
Tags findone iterable

First, the interface method to arrange the quick check

The following table organizes the accessible methods in a simple query, the Jparepository interface (which inherits the Crudrepository interface, the Pagingandsortingrepository interface). (1) First, according to the function of sorting, divided into save, delete, find a single, find multiple, the other 5 categories. (2) The method is not recommended to use gray, such methods are more Crudrepository interface, pagingandsortingrepository interface definition, and later Jparepository interface also defined alternative methods, more convenient to use, For example: When looking for multiple objects, it is easier to return a List than to return a iterable.

二、五个 Interface Detailed

1, Crudrepository interface.

where T is the entity class to manipulate, the ID is the type of the entity class primary key. This interface provides 11 common methods of operation.

@NoRepositoryBean public  interface crudrepository<t, ID extends serializable> extends repository<t, id > {     <s extends t> s save (s entity);//Save      <s extends t> iterable<s> Save (iterable<s> ent ities);//Bulk Save      T FindOne (ID ID);//Query an object by ID. Returns the object itself when the object does not exist, returns null       iterable<t> findAll ();//Queries all objects      iterable<t> findAll (iterable<id> IDS);//Query All objects based on the ID list      Boolean exists (ID ID);//Determine whether the object has     a long count () based on the ID, or the total number of computed objects      void Delete (ID ID);// Delete void Delete      (T entity) by ID;//delete an object     void Delete (iterable<? extends t> entities);//Bulk Delete, collection object (when the background is executed, One delete)    void DeleteAll ();//delete all (when performing in the background, one strip Delete)}

2, Pagingandsortingrepository interface.

This interface inherits the Crudrepository interface and provides two methods for paging and sorting functions.

@NoRepositoryBean public  Interface pagingandsortingrepository<t, ID extends serializable> extends Crudrepository<t, id> {  
Iterable<t> findAll (sort sort);//Sort only page<t> findAll (pageable pageable);//pagination and sorting }

3, Jparepository interface.

The interface inherits the Pagingandsortingrepository interface.

Also inherit the Querybyexampleexecutor interface, this is an "instance" to query the interface, follow-up and write the article detailed description.

 @NoRepositoryBeanpublic Interface jparepository<t, ID extends serializable> extends Pagingandsortingrepo Sitory<t, Id>, querybyexampleexecutor<t> {list<t> findAll ();//Query All objects, return List list<t> fi Ndall (sort sort); Query all objects and sort, return List list<t> findAll (iterable<id> IDs); Queries all objects based on the ID list, returning the list void flush (); Force the cache to synchronize with the database <s extends t> list<s> Save (iterable<s> entities); Bulk Save and return object list <s extends T> s Saveandflush (s entity); Save and Force synchronize database void Deleteinbatch (iterable<t> entities); Bulk Delete Collection objects (when background execution, generate a statement execution, with multiple or conditions) void deleteallinbatch ();//delete all (execute a statement, such as: Delete from user) T GetOne (ID ID); Queries an object by ID, returning a reference to the object (as distinguished from FindOne). When the object is not saved, the return reference is not NULL, but the individual property values are null @Override <s extends t> list<s> findAll (example<s> Example); Based on the instance query @Override <s extends t> list<s> findAll (example<s> Example, sort sort);//query by instance, and sort. }

A few notes:

(1) Several query, and batch save method, compared with Crudrepository interface, return is List, use more convenient.

(2) Added InBatch Delete, the actual execution, the background generates an SQL statement, more efficient. In comparison, the deletion method of the Crudrepository interface is a one-piece deletion, even if the deleteAll is a one-piece deletion.

(3) Add the GetOne () method, remember that the method returns an object reference, and when the queried object does not exist, its value is not NULL.

4. Jpaspecificationexecutor interface
This interface provides support for JPA criteria queries (dynamic queries). This interface is very useful , specific non-stick source.

5. Repository interface
This interface is the most basic interface, just a symbolic interface, there is no definition of any method, then what is the use of this interface? Since spring data JPA provides this interface, naturally it has its usefulness, for example, we do not want to provide some of the methods, such as we just want to provide additions and modifications, do not provide a Delete method, then the previous few interfaces are not done, this time, we can inherit this interface, Then copy the corresponding method inside the Crudrepository interface to the Repository interface.

Key words Method naming SQL WHERE clause
and Findbynameandpwd where Name=? and pwd =?
Or Findbynameorsex where Name=? or sex=?
Is,equals Findbyid,findbyidequals where id=?
Between Findbyidbetween where ID between? and?
LessThan Findbyidlessthan Where ID <?
Lessthanequals Findbyidlessthanequals Where ID <=?
GreaterThan Findbyidgreaterthan where ID >?
Greaterthanequal Findbyagegreaterthanequal Where is age >=?
After Findbyidafter where ID >?
Before Findbyidbefore Where ID <?
IsNull Findbynameisnull where name is null
Isnotnull,notnull Findbynamenotnull Where name is NOT NULL
Like Findbynamelike Where name like?
Notlike Findbynamenotlike Where name isn't like?

Startingwith

Findbynamestartingwith Where name like '?% '
Endingwith Findbynameendingwith Where name like '%? '
Containing Findbynamecontaining Where name like '%?% '
By Findbyidorderbyxdesc where id=? ORDER BY x Desc
Not Findbynamenot where name <>?
Inch Findbyidin (collection<?> c) where ID in (?)
Notin Findbyidnotin (collection<?> c) where ID not in (?)
True

Findbyaaatue

where AAA = True
False Findbyaaafalse where AAA = False
IgnoreCase Findbynameignorecase where UPPER (name) =upper (?)
Top findTop100 Top 10/where ROWNUM <=10

Example:

//Demo /**   comid                     State            effecttime         */ findtop1bycomidandstateandeffecttimebetweenorderbyeffecttim    Edesc,,,;  

Spring Data JPA Simple Query-interface method

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.