Spring of Spring Data Study Guide

Spring Data is a sub-project of the Spring, to simplify access to the database. Spring Data project provides standardized access to operational data, the specification agreed to a unified standard for relational databases and non-relational database operations.

 

Spring Data Repository interface constraints by providing unified data access. Its source code is shown below:

package org.springframework.data.repository;

public interface Repository<T, ID> {

}

The interface receives two parameters:

  • Generic T: represents the type of entity currently operating
  • ID: indicates the type of the entity currently operating primary key type

The method does not provide any interface in the Repository, which is generally used when the sub-operation data interfaces. In project development, developers only need to define data access interface their own projects, then allowed to inherit these sub-Spring Data provided by the interface, you can achieve the CRUD operation on the data. Repository subinterface common packet interface has the following:

  • CrudRepository:
  • PagingAndSortingRepository:

CrudRepository

CrudRepository interface provides basic entity class of deletions change search operation, its source code is shown below:

package org.springframework.data.repository;

public interface CrudRepository<T, ID> extends Repository<T, ID> {
    <S extends T> S save(S entity);
    <S extends T> Iterable<S> saveAll(Iterable<S> entities);
    Optional<T> findById(ID id);
    boolean existsById(ID id);
    Iterable<T> findAll();
    Iterable<T> findAllById(Iterable<ID> ids);
    long count();
    void deleteById(ID id);
    void delete(T entity);
    void deleteAll(Iterable<? extends T> entities);
    void deleteAll();
}

PagingAndSortingRepository

PagingAndSortingRepository CrudRepository interface inherits from the interface, in addition to having the function CrudRepository outside interface, the interface also provides paging and sorting functions, its source code is shown below:

package org.springframework.data.repository;

public interface PagingAndSortingRepository<T, ID> extends CrudRepository<T, ID> {
    Iterable<T> findAll(Sort sort);
    Page<T> findAll(Pageable pageable);
}

 

Spring Data JPA

Spring Data JPA is a subproject of the Spring Data technology, using Spring Data JPA data access data access interface to make the interface can only inherit JpaRepository.

JpaRepository

JpaRepository PagingAndSortingRepository interface extends the interface, which additionally provides more functionality, the source code as follows:

package org.springframework.data.jpa.repository;

public interface JpaRepository<T, ID> extends PagingAndSortingRepository<T, ID>, QueryByExampleExecutor<T> {
    List<T> findAll();
    List<T> findAll(Sort sort);
    List<T> findAllById(Iterable<ID> ids);
    <S extends T> List<S> saveAll(Iterable<S> entities);
    void flush();
    <S extends T> S saveAndFlush(S entity);
    void deleteInBatch(Iterable<T> entities);
    void deleteAllInBatch();
    T getOne(ID id);
    @Override
    <S extends T> List<S> findAll(Example<S> example);
    @Override
    <S extends T> List<S> findAll(Example<S> example, Sort sort);
}

 

Guess you like

Origin www.cnblogs.com/hrvyzou/p/11068016.html
Recommended