Spring CrudRepository and ListCrudRepository Examples

Learn to use Spring Data’s CrudRepository and ListCrudRepository interfaces for data accessing in a Spring Boot application. We will learn to configure the Spring Data support and use these interfaces for CRUD operations.

1. Spring CrudRepository and ListCrudRepository

The CrudRepository is a generic interface for CRUD operations on a repository for a JPA entity type. It is an old interface that can be used in Spring Data 2 as well.

The ListCrudRepository is a new interface added in Spring Data 3.x. The ListCrudRepository is an extension to CrudRepository which returns a List whereas CrudRepository returns an Iterable. The goal behind its introduction is that in most cases, returning an Iterable does not offer any additional benefit over List as we ultimately have to iterate over the entities anyway.

public interface CrudRepository<T, ID> extends Repository<T, ID> {

	<S extends T> Iterable<S> saveAll(Iterable<S> entities);
	Iterable<T> findAll();
	Iterable<T> findAllById(Iterable<ID> ids);

	//other CRUD methods

public interface ListCrudRepository<T, ID> extends CrudRepository<T, ID> {

	<S extends T> List<S> saveAll(Iterable<S> entities);
	List<T> findAll();
	List<T> findAllById(Iterable<ID> ids);

Since Spring Data 3.0, PagingAndSortingRepository does not extend the CrudRepository so if you are using the PagingAndSortingRepository then you need to extend from both repositories for backward compatability.

2. Setup

To setup the usage of CrudRepository and ListCrudRepository interfaces, we need to follow these steps:

2.1. Maven

All the repositories are part of Spring Data module so include it in the classpath:


In a Spring Boot application, we can directly add the starter dependency which transitively include all the necessary dependencies.


2.2. Configure DataSource

To configure the DataSource bean, the simplest approach is to add the properties related to the underlying database. The following properties configure a DataSource that connects to H2 database in the runtime.


Additionally, we need to include the dependencies for JDBC driver class according to the database used.


2.3. @Entity Class

We will also need to create JPA entities that can be persisted and retrieved from the database using the above repository interfaces.

public class Item {

  @GeneratedValue(strategy = GenerationType.IDENTITY)
  public Long id;
  public String name;

  //Setters and Getters

3. Using CrudRepository

To use the CrudRepository, we need to create a new interface and extend it with CrudRepository. It will automatically include all the default list and CRUD methods for our use.

import com.howtodoinjava.demo.model.Item;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

public interface ItemRepository extends CrudRepository<Item, Long> {
	//additonal query methods if required
	List<Item> findByName(String name);

Next, we can use autowire this repository in the service classes and use it.

ItemRepository itemRepository;

//Create Items

for (int i = 1; i <= 3; i++){
  itemRepository.save(new Item(Long.valueOf(i), "Item " + i));

//Retreive items

Iterable<Item> items = itemRepository.findAll();
log.info("All Items : " + items);

4. Using ListCrudRepository

Similar to CrudRepository, the ListCrudRepository also has the same usage except that it returns the List instead of Iterable.

Note that as List extends Iterable so the previous code will still work if we do not explicitly use the List type.

public interface ItemRepository extends ListCrudRepository<Item, Long> {

//Retreive items

List<Item> items = itemRepository.findAll();

5. Conclusion

This short Spring Data tutorial discusses how to use the CrudRepository and ListCrudRepository interfaces and how to use them. It also discusses the changes made in Spring Data 3.0 to keep the compatible with previous releases.

Happy Learning !!

Source Code on Github


Notify of
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.