其他分享
首页 > 其他分享> > (转)CrudRepository JpaRepository PagingAndSortingRepository之间的区别

(转)CrudRepository JpaRepository PagingAndSortingRepository之间的区别

作者:互联网

1. 简介

本文介绍三种不同的Spring Data repository和它们的功能,包含以下三种:

2. Spring Data Repositories

首先介绍JpaRepository,它继承自PagingAndSortingRepository,而PagingAndSortingRepository又继承自CrudRepository
每个都有自己的功能:

由于三者之间的继承关系,所以JpaRepository包含了CrudRepository和PagingAndSortingRepository所有的API

当我们不需要JpaRepository和PagingAndSortingRepository提供的功能时,可以简单使用CrudRepository。

下面我们通过例子更好地理解它们提供的API。
首先创建Product实体:

@Entity
public class Product {
 
    @Id
    private long id;
    private String name;
 
    // getters and setters
}

然后实现通过名称查询Product的操作:

@Repository
public interface ProductRepository extends JpaRepository<Product, Long> {
    Product findByName(String productName);
}

这样就实现了通过名称查询Product的方法,Spring Data Repository根据方法名自动生成相应的实现。

3. CrudRepository

先看下CrudRepository接口的源码:

public interface CrudRepository<T, ID extends Serializable>
  extends Repository<T, ID> {
 
    <S extends T> S save(S entity);
 
    T findOne(ID primaryKey);
 
    Iterable<T> findAll();
 
    Long count();
 
    void delete(T entity);
 
    boolean exists(ID primaryKey);
}

方法功能介绍:

4. PagingAndSortingRepository

PagingAndSortingRepository接口的源码如下:

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

该接口提供了findAll(Pageable pageable)这个方法,它是实现分页的关键。
使用Pageable时,需要创建Pageable对象并至少设置下面3个参数:

Sort sort = new Sort(new Sort.Order(Direction.ASC, "lastName"));
Pageable pageable = new PageRequest(0, 5, sort);

5. JpaRepository

JpaRepository接口源码:

public interface JpaRepository<T, ID extends Serializable> extends
  PagingAndSortingRepository<T, ID> {
 
    List<T> findAll();
 
    List<T> findAll(Sort sort);
 
    List<T> save(Iterable<? extends T> entities);
 
    void flush();
 
    T saveAndFlush(T entity);
 
    void deleteInBatch(Iterable<T> entities);
}

简单介绍下每个方法的作用:

6. Spring Data Repositories的缺点

尽管Spring Data Repositories有很多优点,但是它们也有缺点:

  1. 这种方式把我们的代码和类库以及具体抽象(如:Page和Pageable)绑定。它们不是这个类库独有的,但是我们必须小心不要暴露这些内部实现的细节。
  2. 通过继承CrudRepository接口,我们暴露了所有方法。这可能满足大部分情况,但是我们可能会遇到这样的情况:我们希望获得对公开的方法的更细粒度的控制,例如创建一个不包含save(...)和delete(...)的ReadOnlyRepository,此时继承CrudRepository就不满足要求了。



标签:...,PagingAndSortingRepository,Pageable,JpaRepository,CrudRepository,findAll
来源: https://www.cnblogs.com/-wanglei/p/11125465.html