Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use Map and SqlParameterSource in namedParameterJdbcTemplate?

Tags:

spring-jdbc

String SQL = "INSERT INTO Employee (name, age, salary) VALUES (:name,:age,:salary)";
Map namedParameters = new HashMap();   
namedParameters.put("name", name);   
namedParameters.put("age", age);
namedParameters.put("salary", salary);
namedParameterJdbcTemplate.update(SQL, namedParameters);

String SQL = "UPDATE Employee SET age = :age WHERE empid = :empid";
SqlParameterSource namedParameters = new MapSqlParameterSource();
namedParameters.addValue("age", age);
namedParameters.addValue("empid", empid);
namedParameterJdbcTemplate.update(SQL, namedParameters);

Seems both Map and SqlParameterSource are same. But why did API developers added these API's ? Is there any particular scenario to use Map or SqlParameterSource which makes execution faster? Please explain me clearly. Thanks in advance.

like image 869
Sreepad Chitragar Avatar asked Mar 01 '17 05:03

Sreepad Chitragar


People also ask

How do I use SqlParameterSource?

SqlParameterSource in spring is used to insert rows in table. SqlParameterSource is more efficient than plane map approach. Java bean which is mapped to table, can directly be used to insert values. SqlParameterSource has two implementation BeanPropertySqlParameterSource and MapSqlParameterSource.

How do you use NamedParameterJdbcTemplate?

addValue("id", id); in. addValue("description", new SqlLobValue(description, new DefaultLobHandler()), Types. CLOB); String SQL = "update Student set description = :description where id = :id"; NamedParameterJdbcTemplate jdbcTemplateObject = new NamedParameterJdbcTemplate(dataSource); jdbcTemplateObject.

What is the difference between JdbcTemplate and NamedParameterJdbcTemplate?

Spring - NamedParameterJdbcTemplate Example Functionally, there's no difference between Spring's JdbcTemplate and it's variant, NamedParameterJdbcTemplate except for : NamedParameterJdbcTemplate provides a better approach for assigning sql dynamic parameters instead of using multiple '?' in the statement.

What is the advantage of NamedParameterJdbcTemplate?

NamedParameterJdbcTemplate - wraps a JdbcTemplate to provide more convenient usage with named parameters instead of the traditional JDBC "?" place holders. This provides better documentation and ease of use when you have multiple parameters for an SQL statement. Works with JDK 1.4 and up.


1 Answers

Using a Map is fine for simple cases, but there are two benefits to using SqlParamaterSource over a Map.

The first is simply the builder pattern allowing you to add multiple values inline (namedParameters.addValue().addValue().addValue() etc).

The second is more powerful. The jdbcTemplate will auto-determine the sqlType of your map values while the SqlParamaterSource allows you to explicitly use the sqlType of your choice. This can be an issue depending on your database, indexes and parameters.

An example would be Integers and Longs with an Oracle database. The jdbc template will add these objects to your query with surrounding quotes '' making them effectively strings in your database query. If you have a number in your database with leading 0's it will not be found because '0XXXX' will not match 'XXXX'. If you pass in the right sqlType, the jdbc template will do a number comparison without quotes so XXXX will equal XXXX.

like image 80
The.Laughing.Man Avatar answered Sep 27 '22 22:09

The.Laughing.Man



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!