How to Create Repository in Spring Boot: Simple Guide
In Spring Boot, create a repository by defining an interface that extends
JpaRepository or CrudRepository. This interface allows you to perform database operations without writing implementation code.Syntax
To create a repository in Spring Boot, define an interface that extends JpaRepository<EntityType, IDType>. Replace EntityType with your entity class and IDType with the type of its primary key.
- interface: declares the repository interface
JpaRepository: Spring Data interface providing CRUD and pagination methodsEntityType: your JPA entity classIDType: type of the entity's ID (e.g., Long, Integer)
java
public interface YourEntityRepository extends JpaRepository<YourEntity, Long> { // custom query methods if needed }
Example
This example shows how to create a repository for a User entity with an ID of type Long. The repository interface extends JpaRepository to inherit common database operations.
java
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; import jakarta.persistence.Entity; import jakarta.persistence.Id; @Entity public class User { @Id private Long id; private String name; // getters and setters public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } } @Repository public interface UserRepository extends JpaRepository<User, Long> { // Additional query methods can be added here }
Output
No console output; Spring Boot auto-implements UserRepository for database operations.
Common Pitfalls
Common mistakes when creating repositories in Spring Boot include:
- Not annotating the entity class with
@Entity, so Spring Data cannot map it. - Using the wrong ID type in the repository interface that does not match the entity's ID.
- Forgetting to add
@Repositoryannotation (optional but recommended for clarity). - Trying to instantiate the repository interface manually instead of letting Spring inject it.
java
/* Wrong: ID type mismatch */ public interface UserRepository extends JpaRepository<User, String> { } /* Right: Matching ID type */ public interface UserRepository extends JpaRepository<User, Long> { }
Quick Reference
Summary tips for creating repositories in Spring Boot:
- Extend
JpaRepository<Entity, ID>for full CRUD support. - Ensure your entity class is annotated with
@Entityand has a proper ID field. - Use Spring's dependency injection to get repository instances.
- Add custom query methods by following Spring Data method naming conventions.
Key Takeaways
Create a repository by extending JpaRepository with your entity and ID types.
Annotate your entity class with @Entity and define a proper ID field.
Let Spring Boot inject the repository; do not instantiate it manually.
Use @Repository annotation on the interface for clarity and exception translation.
Follow Spring Data method naming to add custom queries easily.