A custom repository can be created by extending the repository marker interface. In the following example, we extend the repository interface with two methods—findAll and count:
import org.springframework.data.repository.Repository;
public interface TodoRepository extends Repository<Todo, Long> {
Iterable<Todo> findAll();
long count();
}
A few important things to note are as follows:
- public interface TodoRepository extends Repository<Todo, Long>: The TodoRepository interface extends the Repository interface. The two generic types indicate the entity being managed—Todo—and the type of the primary key—Long.
- Iterable<Todo> findAll(): Used to list all the todos. Note that the name of the method should match what's defined in CrudRepository.
- long count(): Used to find the count of all the todos.