为什么我们扩展JpaRepository接口而不实现它



TestRepository ***extends*** JpaRespotiroy<Test,Long>

JpaRepository是一个接口。为什么我们要扩展它,而不是像我们在Java中所知道的那样实现它?据我所知,接口是实现的,而不是扩展的。

有人能给我解释一下吗?

我假设您的代码看起来像

interface TestRepository extends JpaRepository<Test, Long>

所以TestRepository是一个接口,接口可以扩展其他接口而不是实现接口。

TestRepository将在运行时基于SimpleJpaRepository 从Spring Data JPA实现

https://github.com/spring-projects/spring-data-jpa/blob/main/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/support/SimpleJpaRepository.java

在Java 中

  1. 类扩展类class Test Extends Thread
  2. interface扩展接口public interface TestI extends Runtime
  3. 类实现接口public class Test implements Runnable

最新更新