Skip to Content
Just Spring Data Access
book

Just Spring Data Access

by Madhusudhan Konda
June 2012
Intermediate to advanced
74 pages
1h 40m
English
O'Reilly Media, Inc.
Content preview from Just Spring Data Access

Chapter 2. Advanced Concepts

The Spring framework has provided extensive APIs to work with the database. We’ve covered the basics of the framework in the last chapter, especially using the versatile JdbcTemplate class. This chapter elaborates on advanced concepts, including other templates, callbacks, and batch operations.

NamedParameterJdbcTemplate

In our queries, we define the bind variables using a ? operator as shown in the following snippet:

select count(*) from TRADES 
  where account = ? and security = ?

If we have a handful of these parameters, it would be an eyesore to read a query with ? all over the place. Spring has defined a new NamedParameterJdbcTemplate class that comes handy in eliminating these placeholder variables. This class basically encapsulates the JdbcTemplate by providing the enhanced functionality of declaring the bind variables using named parameters.

The same query can be tweaked as shown below using appropriate names instead of ? variables:

select count(*) from TRADES 
  where account = :account and security = :security 

The :account and :security names indicate that these variables will be passed in by some means. Note the colon (:) before the variable; this is the syntax you must follow.

There are two ways of setting these variables. One is to use a simple Map with the variables as the keys, and the other is to use SqlParameterSource, a utility provided by the framework.

Using Map

The following snippet shows how to set the variables by using a map with keys:

public ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Spring Data

Spring Data

Petri Kainulainen
Spring Data

Spring Data

Mark Pollack, Oliver Gierke, Thomas Risberg, Jon Brisbin, Michael Hunger
Just Spring Integration

Just Spring Integration

Madhusudhan Konda

Publisher Resources

ISBN: 9781449339845Errata Page