Creating a simple repository

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 managedTodoand the type of the primary keyLong.
  • 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.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset