Spring NamedParameterJdbcTemplate and Collections.singletonMap Example


In this post I will show you how to use NamedParameterJdbcTemplate and Collections.singletonMap to execute query for returning result. The NamedParameterJdbcTemplate class adds support for programming JDBC statements using named parameters, as opposed to programming JDBC statements using only classic placeholder (?) arguments. The NamedParameterJdbcTemplate class wraps a JdbcTemplate, and delegates to the wrapped JdbcTemplate to do much of its work.

An SqlParameterSource is a source of named parameter values to a NamedParameterJdbcTemplate. The Collections.singletonMap returns an immutable map, mapping only the specified key to the specified value. The returned map is serializable.


Eclipse 2020-06, At least Java 8, Gradle 6.5.1, Maven 3.6.3, Spring Boot 2.3.2, MySQL 8.0.17

Project Setup

You can create either gradle or maven based project in Eclipse. The name of the project is spring-namedparameterjdbctemplate-collections-singletonmap.

If you are creating gradle based project then you can use below build.gradle script:

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

plugins {
    id 'java-library'
    id 'org.springframework.boot' version "${springBootVersion}"

sourceCompatibility = 12
targetCompatibility = 12

repositories {

dependencies {
	implementation "org.springframework.boot:spring-boot-starter:${springBootVersion}"
	//required for jdk 9 or above

If you are creating maven based project then you can use below pom.xml file:

<project xmlns="http://maven.apache.org/POM/4.0.0"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">




		<!--required only if jdk 9 or higher version is used-->

					<source>at least 8</source>
					<target>at least 8</target>

MySQL Table

You need to create a table called user under roytuts database in MySQL server.

  `id` int unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(50) NOT NULL,
  `email` varchar(100) NOT NULL,
  `phone` int unsigned NOT NULL,
  `address` varchar(250) NOT NULL,
  PRIMARY KEY (`id`)

Database Configuration

I will use annotation based configuration and we need to create appropriate beans for working with database.

I am using application.properties file which is kept under src/main/resources classpath folder.

The content of the properties file is given below:


#disable schema generation from Hibernate

The required configuration Java class is given below:

package com.roytuts.spring.namedparameterjdbctemplate.collections.singletonmap.config;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

public class Config {

	private Environment environment;

	public DataSource dataSource() {

		DriverManagerDataSource ds = new DriverManagerDataSource();
		return ds;

	public NamedParameterJdbcTemplate namedParameterJdbcTemplate(DataSource dataSource) {
		NamedParameterJdbcTemplate jdbcTemplate = new NamedParameterJdbcTemplate(dataSource);

		return jdbcTemplate;



Model Class

You need to create a POJO class that will map table and Java class together.

package com.roytuts.spring.namedparameterjdbctemplate.collections.singletonmap.model;

public class User {

	private Integer id;

	private String name;

	private String email;

	private String phone;

	private String address;

	public User() {

	public User(String name, String email, String phone, String address) {
		this.name = name;
		this.email = email;
		this.phone = phone;
		this.address = address;

	//getters and setters

	public String toString() {
		return "User [id=" + id + ", name=" + name + ", email=" + email + ", phone=" + phone + ", address=" + address
				+ "]";


DAO Class

DAO class is where perform database operations. For my example, here I am going to count the number of users for the given user’s name and return returning the result.

package com.roytuts.spring.namedparameterjdbctemplate.collections.singletonmap.dao;

import java.util.Collections;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Component;

public class UserDao {

	private NamedParameterJdbcTemplate jdbcTemplate;

	public int countByName(String name) {
		final String sql = "select count(*) from user where name = :name";

		Map<String, String> namedParameters = Collections.singletonMap("name", name);

		return jdbcTemplate.queryForObject(sql, namedParameters, Integer.class);


Main Class

A class having main method with @SpringBootApplication annotation is enough to deploy the Spring Boot application into embedded Tomcat server.

package com.roytuts.spring.namedparameterjdbctemplate.collections.singletonmap;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import com.roytuts.spring.namedparameterjdbctemplate.collections.singletonmap.dao.UserDao;

public class SpringNamedParameterJdbcTemplateSingletonMapApp implements CommandLineRunner {

	private UserDao dao;

	public static void main(String[] args) {
		SpringApplication.run(SpringNamedParameterJdbcTemplateSingletonMapApp.class, args);

	public void run(String... args) throws Exception {
		System.out.println("Count: " + dao.countByName("Soumitra"));


Testing the Application

Executing the above class will give you the following output:

Count: 1

Source Code


Thanks for reading.

Author: Soumitra

Hi, I am, Soumitra, the owner of roytuts.com and it is my passion for sharing my knowledge and I have been writing blogs on various technologies since 2014. If you like my tutorials, you may also want to like my Facebook Page, follow me on Twitter, Github.

Leave a Reply

Your email address will not be published. Required fields are marked *