Spring Data JDBC Basics

Basic Spring
≈ 2 min read

What Spring Data JDBC adds

Plain JDBC gives you full control over SQL, but it also means doing a lot of repetitive work yourself: executing read and write operations, passing parameters, turning result rows into objects, and organizing typical CRUD code. Spring Data JDBC keeps the relational model visible while taking care of much of this mechanics for you.

The main unit of work here is a domain object mapped to a table. You can declare a repository interface for it, and Spring creates the implementation during application startup.

@Table("customers")
class Customer {
    @Id
    Long id;
    String name;
}

interface CustomerRepository extends CrudRepository<Customer, Long> {
}

In this example, Customer is stored in the customers table, the id field is the identifier, and the repository receives basic data operations. We will examine each of these mechanisms in detail on the following pages.

Why the model stays close to the database

Spring Data JDBC does not try to hide the relational database behind a complex graph of automatically tracked objects. It is still important to understand tables, keys, SQL, and the boundaries of data that is persisted together. This makes behavior more direct: calling a repository means real database work, not access to a long-lived persistence context.

Spring Data JDBC is not a wrapper around the JdbcTemplate class. It builds a higher-level model around objects and repositories, even though a relational database and SQL still sit underneath.