![]() |
VOOZH | about |
Understanding the difference between Spring JDBC and Spring Data JDBC is important for choosing the right approach to interact with relational databases in Spring Boot applications. Both frameworks serve the same purpose but differ significantly in terms of abstraction, ease of use, and developer productivity. The main difference is:
The table below demonstrates the difference between Spring JDBC and Spring Data JDBC
Features | Spring JDBC | Spring Data JDBC |
|---|---|---|
Abstraction Level | Low-level, requires manual SQL queries and boilerplate code. | Higher-level, provides abstractions for database operations. |
Model Class | Requires a model class with getters and setters. | Uses POJOs with annotations like @Table, @Id, and @Column. |
Repository Support | No built-in repository support; DAO pattern is used. | Provides repository interfaces (e.g., CrudRepository) for CRUD operations. |
SQL Query Management | Manual SQL query management is required. | SQL queries are generated automatically; custom queries can be added using @Query. |
JPA Features | No JPA features. | No JPA features like lazy loading or caching. |
Use Case | Suitable for complex queries and fine-grained control over database operations. | Ideal for simpler use cases with reduced boilerplate code. |
Spring can perform JDBC operations by having connectivity with any one of jars of RDBMS like MySQL, Oracle, or SQL Server, etc., For example, if we are connecting with MySQL, then we need to connect "mysql-connector-java".
Let us see how a pom.xml file of a maven project looks like.
pom.xml:
Model Class:
We need a MODEL class to start
DAO Pattern:
Implementation of DAO Interface:
It belongs to the Spring Data family. Basically, it provides abstractions for the JDBC-based Data Access Layer. It provides easy to use Object Relational Mapping (ORM) framework to work with databases. It can support entity objects and repositories. Because of this, a lot of complexities are reduced. The data access layer is simple. Hence JPA features like Lazy Loading, caching of entities, etc. are omitted. Because of this JDBC operations on the entities are taken care of well. Let us see what are the dependencies needed for Spring Data JDBC in the Spring Boot maven project.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jdbc</artifactId>
</dependency>
Let us see what are the dependencies needed for Spring Data JDBC in the Spring maven project.
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jdbc</artifactId>
<!-- Specify the required version here -->
<version>{version}</version>
</dependency>
For making the entity, we need to use a POJO(Plain old java object). Spring Data JDBC can map our POJO to a database table. The following key points need to be observed
Sample POJO class:
For a POJO class, it is not mandatory to have
Repository Interface:
Repository Interface along with Query methods and @Query annotations are supported. Created repositories can be a sub-interface of CrudRepository.
Spring Data JDBC only supports native SQL queries, not JPQL.
The named query can be constructed as,
@Query("SELECT * FROM table_name WHERE column_name = :param")
List<POJOClass> findByColumn(@Param("param") String param);