Open In App
Related Articles

Spring Boot – Spring JDBC vs Spring Data JDBC

Like Article
Save Article
Report issue

Spring JDBC

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.


<?xml version="1.0" encoding="UTF-8"?>
      <!-- Spring framework -->
         <!-- You can change corresponding version here -->
      <!-- MySQL database driver -->
         <!-- You can change corresponding version here -->


Model Class:

We need a MODEL class to start


public class <ModelClass>
    int column1;
    String column2;
    int column3;
    // ....
    // corresponding getter and setter
    // methods for each and every column


DAO Pattern:


public interface <SampleDAO>
    // To insert the records
    public void insert(ModelClass object);
    // To get the records
    public ModelClass findByXXX(int xxx);


Implementation of DAO Interface:


import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.sql.DataSource;
// Provide necessary interface imports
// Provide necessary ModelClass imports
public class JdbcSampleDAO implements SampleDAO {
    private DataSource dataSource;
    public void setDataSource(DataSource dataSource)
        this.dataSource = dataSource;
    public void insert(ModelClass object)
        // Write the necessary statements like providing SQL
        // insert statement
        // and execute them for making record insertion
    public ModelClass findByXXX(int XXX)
        // Write the necessary statements like providing SQL
        // select statement
        // and execute them for making record display


Spring Data JDBC

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.




Let us see what are the dependencies needed for Spring Data JDBC in the Spring maven project.


      <!-- Specify the required version here -->


POJO Class:

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

  1. Created POJO needs to match with the database table. If not, it uses @Table annotation and this helps to refer to the actual table name.
  2. @Id annotation is required to identify the primary key
  3. It is a good practice that all the persistence fields in the POJO and database table columns need to match. Otherwise, @Column annotation is helpful to provide column names.

Sample POJO class


import lombok.Data;
// Required for generating default getter and setter
// If POJO class name and db table name differs
@Table("<mention the name of db table>")
public class <POJOClass> {
    // For representing Primary key
    private Long id;
    private String <columnName1>;
    private String <columnName2>;
    private Integer <columnName3>;


For a POJO class, it is not mandatory to have

  1. A parameterized constructor.
  2. Access methods.

Repository Interface:

Repository Interface along with Query methods and @Query annotations are supported. Created repositories can be a sub-interface of CrudRepository.


import org.springframework.stereotype.Repository;
import java.util.List;
public interface <SampleRepository> extends CrudRepository<<POJOClass>, Long> {
    // XXX can be firstName, lastName, age, gender etc.,
    List<POJOClass> findByXXX(String XXX);


The named query can be constructed as


@Query("select * from <tableName> where condition = :<param>")
List<POJOClass> findByXXX(@Param("param") String XXX);


Difference Between Spring JDBC and Spring Data JDBC

Spring JDBC

Spring Data JDBC

Spring JDBC is a Model class.Spring Data JDBC is a POJO class.
Getter and setters are mandatory.Getter and setters are not mandatory.
The parameterized constructor will be helpful.The parameterized constructor may not be helpful.
There is no specific annotation is required. The only thing is we should have equal attributes match with the DB table and each attribute should have a getter and setter.@Table, @ID, and @Column annotations are helpful to mention for direct connection with the database.
Data Access Layer is specified with the interface and its implementation.Data Access Layer is simple and it omits lazy loading, cache implementation, etc., which is there in JPA(Java Persistence API).

Last Updated : 12 Jul, 2022
Like Article
Save Article
Share your thoughts in the comments
Similar Reads