M03 Q25 What is a Repository interface?
Repository interface is a Java interface that describes DAO with expected behaviors, based on which Spring Data will automatically generate DAO logic. Repository interface takes Domain Class and ID of type to manage.
Custom Repository interface needs to extend one of the following interface:
- Repository – basic marker repository
- CrudRepository – adds generic methods for CRUD operations
- PagingAndSortingRepository – adds findAll methods for paging/sorting
- JpaRepository – JPA specific extension of Repository
There is also a possibility to use @Query
annotation with your own SQL query.
It is a great thing about JPA spring data is that you do not have to define all of those methods are your own. All you have to do is to create an interface that describes the expected behavior for DAO class that extends one of the repository interfaces. And you are informing Spring Data on which packages should be scanned for those repository interfaces in @EnableJpaRepositories
annotation.
In the Spring Boot you do not have to use @EnableJpaRepositories cause it is used whenever Spring Data is available and Spring JPA is available in your dependencies. And the whole classpath of your application will be scanned.