Spring Data JDBC
- is a Spring Data Module that is not as complex as Spring Data JPA
- it does NOT provide:
- cache
- lazy loading
- write-behind
- schema generation. As a result, we are responsible for explicitly creating the schema
- or many other features of JPA
- it does provide:
- its own ORM
- mapped entities
- repositories
- query annotations
- JdbcTemplate
Dependencies
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jdbc</artifactId>
</dependency>
Creating Entities
We don’t need to use the annotation @Table or @Column in the Person class. The default naming strategy of Spring Data JDBC does all the mappings implicitly between the entity and the table
public class Person {
@Id
private long id;
private String firstName;
private String lastName;
// constructors, getters, setters
}
Creating Repositories
We can create a Spring Data JDBC repository by extending the Repository, CrudRepository, or PagingAndSortingRepository interface
@Repository
public interface PersonRepository extends CrudRepository<Person, Long> {
List<Person> findByFirstName(String firstName);
@Modifying
@Query("UPDATE person SET first_name = :name WHERE id = :id")
boolean updateByFirstName(@Param("id") Long id, @Param("name") String name);
}