Spring Boot JDBCTemplate Select Query Example

Hey guys in this post, we will discuss retrieving the records from database using JDBCTemplate in Spring Boot with complete example.


  • JDBCTemplate is class which will help us to query the database
  • JDBCTemplate implements JdbcOperations which provides methods such as query(), execute(), update() etc.,
  • BeanPropertyRowMapper is a RowMapper implementation that converts a table row into a new instance of the specified mapped target class.
  • The mapped target class must be a top-level class and it must have a default or no-arg constructor.

Complete Example

We will create this example step by step, follow this tutorial till the end

Read More:

Create a Database

Open the mysql console or workbench and execute the following queries

CREATE DATABASE springbootjdbc;

USE springbootjdbc;

CREATE TABLE tbl_employees
    name VARCHAR(255) NOT NULL,
    location VARCHAR(255) NOT NULL,

INSERT INTO tbl_employees
(name, location, dateOfBirth)
("Bushan", "India", now());

INSERT INTO tbl_employees
(name, location, dateOfBirth)
("Bharath", "India", now());

Create spring boot project

There are many different ways to create a spring boot application, you can follow the below articles to create one –

>> Create spring boot application using Spring initializer
>> Create spring boot application in Spring tool suite [STS]
>> Create spring boot application in IntelliJ IDEA

Add maven dependencies

Open pom.xml and add the following dependencies –

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
		<relativePath/> <!-- lookup parent from repository -->
	<description>Spring boot jdbc project</description>




spring-boot-starter-web dependency for building web applications using Spring MVC. It uses the tomcat as the default embedded container.

spring-boot-devtools dependency for automatic reloads or live reload of applications. spring-boot-starter-jdbc is a starter dependency for communicating with the database. lombok dependency is a java library that will reduce the boilerplate code that we usually write inside every entity class like setters, getters, and toString(). mysql-connector-java dependency is to store the data into the database.

Configure the datasource

Open application.properties file and add the following contents


Create an entity class

Create Employee.java inside the in.bushansirgur.springboot.entity package and add the following content

package in.bushansirgur.springbootjdbc.entity;

import java.util.Date;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

public class Employee {
	private Long id;
	private String name;
	private String location;
	private Date dateOfBirth;
	public Employee(String name, String location, Date dateOfBirth) {
		this.name = name;
		this.location = location;
		this.dateOfBirth = dateOfBirth;
  • @Setter annotation generates the setter methods
  • @Getter annotation generates the getter methods
  • @AllArgsConstructor generates parameterised constructor with all the fields
  • @NoArgsConstructor generates no-arg constructor

Create a DAO

Create an interface EmployeeDAO.java inside the in.bushansirgur.springboot.dao package and add the following content

package in.bushansirgur.springbootjdbc.DAO;

import java.util.List;

import in.bushansirgur.springbootjdbc.entity.Employee;

public interface EmployeeDAO {
	public List<Employee> findAll();

Create EmployeeDAOImpl.java inside the in.bushansirgur.springboot.dao package and add the following content

package in.bushansirgur.springbootjdbc.DAO;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import in.bushansirgur.springbootjdbc.entity.Employee;

public class EmployeeDAOImpl implements EmployeeDAO{

	JdbcTemplate jdbcTemplate;
	public List<Employee> findAll() {
		return jdbcTemplate.query("SELECT * FROM tbl_employees", new BeanPropertyRowMapper<Employee>(Employee.class));
  • query() takes 2 parameters, first parameter is the SQL query and the second parameter is the BeanPropertyRowMapper<>()
  • Database column names and property names in the Bean class should be same to map correctly

Create a Controller

Create EmployeeController.java inside the in.bushansirgur.springbootjdbc.controller package and add the following content

package in.bushansirgur.springbootjdbc.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import in.bushansirgur.springbootjdbc.DAO.EmployeeDAO;
import in.bushansirgur.springbootjdbc.entity.Employee;

public class EmployeeController {
	private EmployeeDAO eDAO;
	public List<Employee> findAll() {
		return eDAO.findAll();

Run the app

Run the application using the below maven command –

mvn spring-boot:run

Open the Browser or Postman and enter the following URL –

  • localhost:8080/employees


That’s it for this post. I hope you enjoyed this post, if you did then please share this with your friends and colleagues. Also, you can share this post with your social media profiles. Thank you I will see you in the next post.

Bushan Sirgur

Hey guys, I am Bushan Sirgur from Banglore, India. Currently, I am working as an Associate project in an IT company.

Leave a Reply