Spring Data JDBC provides JDBC-based repositories without requiring applications to manage connections and exceptions through old-style JDBC service code. It keeps a simpler, more limited object–relational mapping model than Spring Data JPA: there are no sessions, dirty tracking, caching, or lazy loading. Load an entity through a query, then explicitly save it when it changes. section 15.0
Start with a user repository
In a Spring Boot application, add spring-boot-starter-data-jdbc and a MySQL driver. Supply a schema.sql that creates USERS, then map a User class with @Table("USERS"), @Id, and @Column. Declare interface UserRepository extends CrudRepository<User, Long> and add List<User> findAll(). For MySQL, set spring.sql.init.mode=always if the startup script must run; this example recreates the table at startup. section 15.1
Choose a query style according to the condition:
- Derive queries from repository method names, such as findByUsername(String username) or findByRegistrationDateBetween(LocalDate start, LocalDate end). Names must match entity properties; an invalid name fails when the application context loads. Derived conditions cannot use joins. section 15.2.1
- Use @Query for SQL the method name cannot derive. For example, countByUserId needs an explicit SELECT COUNT(*) FROM USERS_ADDRESSES WHERE USER_ID = :USER_ID because userId is not a property of its repository entity. SQL is database-specific rather than JPQL. section 15.3.4 section 15.0
Relationships require deliberate table and class design. A User reference to Address annotated @MappedCollection(idColumn = "USER_ID") models the one-to-one example. For many-to-many, introduce a USERS_ADDRESSES table and a mapped intermediary class rather than relying on JPA relationship annotations. Schema DDL remains the application's responsibility. section 15.3.1 section 15.3.4
Table 15.2 Spring Data JPA capabilities vs. Spring Data JDBC capabilities (view table figure)
The capability comparison helps decide whether simpler JDBC repositories justify giving up JPA features such as portability and automatic schema generation. section 15.3.4


