Spring Data两种仓储比较:Crud和JPA


Crud 和 JPA Repository 都是 Spring 数据库的接口。使用 Spring Data的优点是它提供了样板代码来访问您的数据层。

Crud Repository
它是基础接口并扩展了 Repository 接口。它包含用于创建、读取、更新和删除操作的CRUD方法。例如:save()、findById():

@Repository 
public interface MyPlaylistDAO extends CrudRepository { 
   Book Event findById(@Param("id") Integer id); 
}

JPA Repository
它扩展了 PagingAndSorting Repository,后者又扩展了 CrudRepository。它包含所有 CRUD 操作以及实现分页的方法。除此之外,它还提供了批量操作的方法,例如批量删除记录和将数据直接刷新到数据库。例如:saveAllAndFlush()、deleteInBatch()、findAll(Pageable pageable)

@Repository
public interface MyPlaylistDAO extends JpaRepository {
   Book findByArtist(@Param("id") Integer id);
}