Spring Data Jpa (two) JPA underlying query

Describes the basic method Spring Data Common Public inside

(1)Spring Data Common的Repository

  Spring Data Repository located inside the Common the lib is Spring Data database operations done inside the bottom of the abstract interface, top parent class, inside which the source what methods are not real, only play a role in identification. Id and domain management domain class class type as the type parameter, this type of interface is mainly used as marker interface to be used to capture, and to help you discover this interface expansion interface. Spring underlying dynamic agent found the time to do as long as its implementation class or sub-class, represents the repository operation

  Repository source as follows

  With this class, we will be able to follow it to find the basic interface and operating like a lot of Spring Data JPA provided, and its implementation. This interface defines all entities and operations ID Repostory two generic parameters. We do not need to inherit any interface, as long as inherit this interface, you can use a lot of conventions Spring JPA which provides a method of inquiry and query annotations.

 

 

(2) CrudRepository Detailed Method  

  By class diagram can be seen CrudRepository provides a common generic CRUD methods.

  CrudRepository interface内容

(1) Save entity method. We see additional implementation class by class relationships earlier. SimpleJpaRepository inside of implementation

  

    We found it to pass into the first check entity is not present, then the judge is new or updated; is not based on two kinds of mechanisms, one is judged according to the primary key exists, the other is based on Version to determine (later explained Version Detailed time). If we go to the JPA console print out SQL, at least there will be two, one query, one is the insert or update.

(2) Batch save. (1) the same principle and procedure. To achieve this is for the above cycle call save method.

(3) The primary key querying entity.
(4) The entity determines whether there is a primary key.
(5) a list of all queries entities.
(6) the list of entities based on the primary key query list.
(7) the total number of queries.
(8) delete the primary key. We see additional implementation class by class relationships earlier. SimpleJpaRepository inside of implementation:

 

    We'll go see JPA query about, do save, there is no throw an exception. Here especially emphasize delete and save method, because some people will be superfluous in practical work, go to their own judgment processing queries do, in fact, the underlying Spring JPA have been taken into account.

 

(3) PagingAndSortingRepository Detailed Method

  By class diagram, we can see PagingAndSortingRepository Following all the basic method of bearing CrudRepository, it adds paging and sorting of query results such as basic, common, common Paging some fruit limiting.

  PagingAndSortingRepository interface内容

    (1) take the set of all objects according to the ranking.

    (2) The pagination query and sorted and packaged using the Page object. Pageable object contains the paging and Sort objects.

  PagingAndSortingRepository和CrudRepository都是Spring Data Common的标准接口,如果我们采用JPA,那它对应的实现类就是Spring Data JPA的model里面的SimpleJpaRepository。如果是其他NoSQL的实现Mongodb,那它的实现就在Spring Data Mongodb的model里面。

 

(4)JpaRepository方法详解

  JpaRepository到这里可以进入分水岭了,上面的那些都是Spring Data为了兼容NoSQL而进行的一些抽象封装,从JpaRepository开始是对关系型数据库进行抽象封装。从类图可以看得出来它继承了PagingAndSortingRepository类,也就继承了其所有方法,并且实现类也是SimpleJpaRepository。从类图上还可以看出JpaRepository继承和拥有了QueryByExampleExecutor的相关方法。

  通过源码和CrudRepository相比较,它支持Query By Example,批量删除,提高删除效率,手动刷新数据库的更改方法,并将默认实现的查询结果变成了List。

 

(5)Repository的实现类SimpleJpaRepository

  SimpleJpaRepository是JPA整个关联数据库的所有Repository的接口实现类。如果想进行扩展,可以继承此类,如QueryDsl的扩展,还有默认的处理机制。如果将此类里面的实现方法看透了,基本上JPA的API就能掌握大部分。同时也是Spring JPA动态代理的实现类,包括我们后面讲的Query Method。

 

Guess you like

Origin www.cnblogs.com/youqc/p/11096085.html