Spring Boot JDBC Template

This time I will show you how to use JDBC template in a Spring Boot application, first you need to integrate JDBC template in your build.gradle file.

buildscript {
  ext {
    springBootVersion = '1.5.4.RELEASE'
  repositories {
  dependencies {

apply plugin: 'groovy'
apply plugin: 'org.springframework.boot'

version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8

repositories {

dependencies {
  compile 'org.springframework.boot:spring-boot-starter'
  compile 'org.springframework.boot:spring-boot-starter-jdbc'
  compile 'org.springframework:spring-jdbc:4.3.9.RELEASE'
  compile 'org.codehaus.groovy:groovy'
  compile 'mysql:mysql-connector-java:5.1.34'
  testCompile 'org.springframework.boot:spring-boot-starter-test'

Then we will create a person model object

package com.jos.dem.jdbc.model

class Person {

  Long id	
  String nickname
  String email
  Integer ranking

Let’s suppose you have a MySQL database called spring_boot_jdbc_template with a person table.

CREATE TABLE `person` (
  `nickname` varchar(255) NOT NULL,
  `email` varchar(255) NOT NULL,
  `ranking` int(11) NOT NULL,
  PRIMARY KEY (`id`)

With this data:

| id | nickname | email                       | ranking |
|  1 | josdem   | joseluis.delacruz@gmail.com |       5 |
|  2 | erich    | eric@email.com              |       5 |
|  3 | martinv  | martin@email.com            |       4 |

Spring provides a template class called JdbcTemplate that makes it easy to work with SQL relational databases and JDBC, so let’s create a PersonRepository

package com.jos.dem.jdbc.repository

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

import com.jos.dem.jdbc.model.Person

class PersonRepository {

  JdbcTemplate jdbcTemplate

  List<Person> findAll() {
      'SELECT * FROM person',


BeanPropertyRowMapper can maps a row’s column value to a property by matching their names in a model object.

Now let’s create a service to delegate data access to the repository:

package com.jos.dem.jdbc.service

import com.jos.dem.jdbc.model.Person

interface PersonService {

  List<Person> getPersons()


This is the implementation

package com.jos.dem.jdbc.service.impl

import org.springframework.stereotype.Service
import org.springframework.beans.factory.annotation.Autowired

import com.jos.dem.jdbc.model.Person
import com.jos.dem.jdbc.service.PersonService
import com.jos.dem.jdbc.repository.PersonRepository

import org.slf4j.Logger
import org.slf4j.LoggerFactory

class PersonServiceImpl implements PersonService {

  PersonRepository personRepository

  Logger log = LoggerFactory.getLogger(this.class)

  List<Person> getPersons() {
    log.info 'Querying for getting persons'


Finally we get PersonService bean from the spring application context and execute the getPersons() method:

package com.jos.dem.jdbc

import org.springframework.boot.SpringApplication
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.context.ConfigurableApplicationContext

import com.jos.dem.jdbc.model.Person
import com.jos.dem.jdbc.service.PersonService

class JdbcApplication {

  static void main(String[] args) {
	ConfigurableApplicationContext context = SpringApplication.run JdbcApplication, args
    List<Person> persons = context.getBean(PersonService.class).getPersons()
    persons.each {
      println "person: ${it.dump()}"


Don’t forget to set datasource information in your application.properties


To download the project

git clone https://github.com/josdem/spring-boot-jdbc-template.git

To run the project

gradle bootRun

Return to the main article

comments powered by Disqus