Spring Data JPA Simple Query

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.

650) this.width=650; "Src=" http://images2015.cnblogs.com/blog/730751/201703/730751-20170316113056963-1182568396. PNG "style=" border:0px; "/>

二、五个 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.

650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" border:0px; "/>

@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> entities);//Bulk Saving        t findone (id id);//Query an object based on 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);//Based on ID list   Query all Objects       boolean exists (id id);//Based on id  to determine whether the object exists       long count ();//Calculate the total number of objects       void delete (ID id) ;//Delete       void delete according to id  (T entity);//Delete an object      void delete (iterable<? extends t> entities );///Bulk Delete, collection object (when performing in the background, one delete)     void deleteall ();//Delete all   (background execution, one delete)}

650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" border:0px; "/>

2, Pagingandsortingrepository interface.

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

650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" border:0px; "/>

@NoRepositoryBean <t, ID serializable> crudrepository<t, id> {iterable<t> findAll (sort sort); Page<t> findAll (pageable pageable);}

650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" border:0px; "/>

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.

650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" border:0px; "/>

@NoRepositoryBeanpublic  interface JpaRepository<T, ID extends Serializable>         extends pagingandsortingrepository<t, id>,  QueryByExampleExecutor<T> {        List<T>  findall ();  //query all objects, return List    list<t> findall (Sort sort);  Query all objects, and sort, return List    list<t> findall (iterable<id> ids);  // Based on ID list   query all objects, return List    void flush ();  //force cache and database synchronization       <s extends t> list<s> save (iterable<s> entities);  //Bulk Save and returns the object List    <s extends t> s saveandflush (S entity);  Save and Force Synchronize database     void deleteinbatch (iterable<t> entities);  //Bulk Delete   Collection Object (when executing in the background, generate a statement to execute, with multiple orConditions)     void deleteallinbatch ();//Delete all   (executes a statement, such as: Delete from user)     t getone (id id);  //queries an object based on id , returning a reference to the object (as distinct from findone). When the object does not exist, the return reference is not NULL, but the individual property values are null         @Override      <s extends t> list<s> findall (example<s> example);  //Query by instance      @Override     <S extends T> List<S>  findall (Example<s> example, sort sort);//query by instance, and sort. }

650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" border:0px; "/>

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 the Crudrepository interface inside the corresponding method copy to the Repository interface can be.


Spring Data JPA Simple 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.