NamedParameterJdbcTemplate (Spring JDBC)
NamedParameterJdbcTemplate is a Spring JDBC class that works like JdbcTemplate, but instead of using positional parameters (?), it allows you to use named parameters in SQL queries.
👉 In simple words:
NamedParameterJdbcTemplate = JdbcTemplate with readable, named SQL parameters
It makes SQL queries easier to read, safer to write, and easier to maintain, especially when queries become large or complex.
Why Use NamedParameterJdbcTemplate?
Problems with JdbcTemplate
Stringsql="SELECT * FROM users WHERE name = ? AND status = ?";
jdbcTemplate.query(sql, name, status);- Hard to read when there are many
? - Easy to mix up parameter order
- Difficult to maintain large queries
Solution with Named Parameters
Stringsql="SELECT * FROM users WHERE name = :name AND status = :status";
- SQL is self-explanatory
- Parameter order does not matter
- Reduces bugs caused by wrong indexing
- Much easier to maintain
How NamedParameterJdbcTemplate Works (Internally)
ClientCode
|
v
NamedParameterJdbcTemplate
|
v
JdbcTemplate
|
v
DataSource
|
v
Database
- Converts named parameters (
:name) into positional parameters (?) - Delegates execution to
JdbcTemplate - Keeps full control over SQL
Key Features
| Feature | Description |
|---|---|
| Named Parameters | Uses :paramName instead of ? |
| Readable SQL | Easy to understand and debug |
| Safe Mapping | Avoids parameter-order mistakes |
| Collection Support | Supports IN queries with lists |
| Bean Mapping | Maps object fields directly |
Basic Setup
Spring Boot automatically configures NamedParameterJdbcTemplate if spring-boot-starter-jdbc is present.
@Autowired
private NamedParameterJdbcTemplate namedJdbcTemplate;
Common Usage Examples
1. Insert Data Using Named Parameters
Uses named placeholders instead of ?, making SQL more readable and reducing mistakes caused by parameter order.
Stringsql="INSERT INTO users(name, email) VALUES (:name, :email)";
Map<String, Object> params =newHashMap<>();
params.put("name","Amit");
params.put("email","[email protected]");
namedJdbcTemplate.update(sql, params);
2. Query for a Single Object
Fetches one record using named parameters, improving clarity and avoiding confusion in complex queries.
Stringsql="SELECT * FROM users WHERE id = :id";
Map<String, Object> params = Map.of("id",1L);
Useruser= namedJdbcTemplate.queryForObject(
sql,
params,
newBeanPropertyRowMapper<>(User.class)
);
3. Using MapSqlParameterSource
Provides a type-safe and fluent way to pass parameters, making the code cleaner and easier to maintain than raw maps.
Stringsql="UPDATE users SET email = :email WHERE id = :id";
SqlParameterSourceparamSource=
newMapSqlParameterSource()
.addValue("id",1L)
.addValue("email","[email protected]");
namedJdbcTemplate.update(sql, paramSource);
4. Using BeanPropertySqlParameterSource
Automatically maps Java object fields to SQL parameters, reducing manual coding and improving consistency.
Useruser=newUser("John","[email protected]");
Stringsql="INSERT INTO users(name, email) VALUES (:name, :email)";
namedJdbcTemplate.update(sql,newBeanPropertySqlParameterSource(user));
5. Query with IN Clause
Handles collections safely and easily by expanding lists automatically, which is difficult and error-prone with JdbcTemplate.
Stringsql="SELECT * FROM users WHERE id IN (:ids)";
Map<String, Object> params = Map.of("ids", List.of(1,2,3));
List<User> users = namedJdbcTemplate.query(
sql,
params,
newBeanPropertyRowMapper<>(User.class)
);
Difference Between JdbcTemplate and NamedParameterJdbcTemplate
| Aspect | JdbcTemplate | NamedParameterJdbcTemplate |
|---|---|---|
| Parameter Style | Uses positional parameters (?) | Uses named parameters (:name, :id) |
| SQL Readability | Lower when many parameters are used | Higher and more self-explanatory |
| Parameter Order Dependency | Yes, parameters must be passed in correct order | No order dependency; parameters matched by name |
| Risk of Bugs | Higher (wrong parameter order can cause issues) | Lower (clear parameter mapping) |
| Code Maintainability | Harder for complex queries | Easier to maintain and modify |
| Learning Curve | Very easy | Easy |
| SQL Control | Full control over SQL | Full control over SQL |
| Internal Working | Direct JDBC execution | Internally uses JdbcTemplate |
| Performance | Very high | Almost same as JdbcTemplate (negligible difference) |
When to Use NamedParameterJdbcTemplate
Use it when:
- Queries have many parameters
- SQL readability matters
INclauses are common- You want safer, cleaner SQL
Not necessary when:
- Queries are very simple
- Only 1–2 parameters are used
Real-World Use Cases
- Complex reporting queries
- Search and filter APIs
- Dynamic SQL conditions
- Batch database operations
Conclusion
NamedParameterJdbcTemplate makes SQL cleaner, safer, and easier to maintain by replacing positional (?) parameters with meaningful names.
It keeps all the power and performance of JdbcTemplate while greatly improving readability and reducing bugs, making it an excellent choice for real-world, complex SQL scenarios.
