Spring Boot
Article 8: Spring Data JPA Repositories (CRUD Made Easy)
By Utility Zone · 2026-01-27T18:29:57.060193
1. Introduction
Now that your database and entity are ready, the next question is:
How do we save, read, update, and delete data?
Spring Data JPA answers this with repositories — no boilerplate SQL, no manual DAO code.
This article shows how CRUD becomes almost effortless.
2. What Is a Repository?
A repository is a data access layer abstraction.
Instead of writing:
- SQL queries
- JDBC code
- EntityManager boilerplate
You simply define an interface.
Spring generates the implementation automatically.
3. JpaRepository Basics
Spring Data provides multiple base interfaces. The most commonly used is:
JpaRepository<T, ID>
Where:
T→ Entity typeID→ Primary key type
4. Creating Your First Repository
4.1 Create Repository Package
com.example.demo.repository
4.2 Create UserRepository
package com.example.demo.repository;
import com.example.demo.model.User;
import org.springframework.data.jpa.repository.JpaRepository;
public interface UserRepository extends JpaRepository<User, Long> {
}
That’s it. No implementation needed.
5. Built-in CRUD Methods
JpaRepository already provides:
| Method | Purpose |
|---|---|
save() | Create / Update |
findById() | Find by ID |
findAll() | Fetch all |
deleteById() | Delete |
count() | Count records |
6. Using Repository in Service Layer
@Service
public class UserService {
private final UserRepository userRepository;
public UserService(UserRepository userRepository) {
this.userRepository = userRepository;
}
public User createUser(User user) {
return userRepository.save(user);
}
public List<User> getAllUsers() {
return userRepository.findAll();
}
}
7. Exposing CRUD APIs
@RestController
@RequestMapping("/users")
public class UserController {
private final UserService userService;
public UserController(UserService userService) {
this.userService = userService;
}
@PostMapping
public User create(@RequestBody User user) {
return userService.createUser(user);
}
@GetMapping
public List<User> getAll() {
return userService.getAllUsers();
}
}
Test using Postman or curl.
8. Derived Query Methods (Magic ✨)
Spring Data can derive queries from method names.
Example:
List<User> findByName(String name);
User findByEmail(String email);
No SQL required.
9. Custom Queries Using @Query
@Query("SELECT u FROM User u WHERE u.email LIKE %:email%")
List<User> searchByEmail(@Param("email") String email);
Use when:
- Complex queries
- Joins
- Performance tuning
10. Pagination & Sorting
Page<User> findAll(Pageable pageable);
Usage:
PageRequest.of(0, 5, Sort.by("name"));
Essential for real applications.
11. Common Beginner Mistakes
❌ Putting logic in controller
❌ Forgetting @Service
❌ Wrong generic types
❌ Not handling Optional
12. What You Should Understand Before Moving On
You should now know:
- What repositories are
- How
JpaRepositoryworks - How CRUD APIs are built
- How query methods work
13. What’s Next?
➡ Article 9: Database Migrations with Flyway
- Schema versioning
- Controlled DB changes
- Production-safe migrations
Type Next when you’re ready.