为什么我们在创建仓库的时候需要创建xxxCustom和xxxImplclass?

Why we need to create xxxCustom and xxxImpl class when we create repository?

根据这些信息,当我们创建存储库class时,最好为一个存储库创建 1 个 classes 和 2 个接口 UserRepository(interface), UserRepositoryCustom(class) , UserRepositoryImpl(接口).

http://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repositories.custom-implementations

但是我们可以在没有这些 classes 的情况下创建存储库 class... 为什么我们需要创建这些 classes 以及如果我们创建这些 classes 的优点(或缺点)是什么那些 classes?

如果你签出 Spring 数据 core conceptsUserRepository 接口定义扩展 CrudRepositoryJPAReposiory 提供免费的所有基本 CRUD 操作在一个实体上。

public interface UserRepository extends JpaRepository<User, Long>

您可以使用 naming convention approach or by using @Query attribute.

在此存储库界面中添加您自己的基本自定义查询

如果您想执行一些无法在 UserRepository 中轻松管理和定义的自定义逻辑,例如复杂的连接和存储过程,您需要访问底层 EntityManager 您需要 UserCustomRepository 接口。 UserRepository 将扩展此接口以继承方法。

public interface UserRepository extends JpaRepository<User, Long>, UserCustomRepository {
    void myCustomMethod();
}

您需要在UserRepositoryImplclass中自行提供这些方法的实现。 Spring 数据在此 class 中查找自定义方法实现,并在调用时调用它们。

希望这个解释对您有所帮助。