In JdbcTemplate, SQL parameters are represented by a special placeholder “?” symbol and bind it by position. The problem is whenever the order of parameter is changed, you have to change the parameters bindings as well, it’s error prone and cumbersome to maintain it.

To fix it, you can use “Named Parameter“, whereas SQL parameters are defined by a starting colon follow by a name, rather than by position. In additional, the named parameters are only support in SimpleJdbcTemplate and NamedParameterJdbcTemplate.

See following three examples to use named parameters in Spring.

Example 1

Example to show you how to use named parameters in a single insert statement.

	//insert with named parameter
public void insertNamedParameter(Customer customer){ String sql = "INSERT INTO CUSTOMER " +
"(CUST_ID, NAME, AGE) VALUES (:custId, :name, :age)"; Map<String, Object> parameters = new HashMap<String, Object>();
parameters.put("custId", customer.getCustId());
parameters.put("name", customer.getName());
parameters.put("age", customer.getAge()); getSimpleJdbcTemplate().update(sql, parameters); }

Example 2

Examples to show how to use named parameters in a batch operation statement.

	public void insertBatchNamedParameter(final List<Customer> customers){

		String sql = "INSERT INTO CUSTOMER " +
"(CUST_ID, NAME, AGE) VALUES (:custId, :name, :age)"; List<SqlParameterSource> parameters = new ArrayList<SqlParameterSource>();
for (Customer cust : customers) {
parameters.add(new BeanPropertySqlParameterSource(cust));
} getSimpleJdbcTemplate().batchUpdate(sql,
parameters.toArray(new SqlParameterSource[0]));
}

Example 3

Another examples to use named parameters in a batch operation statement.

	public void insertBatchNamedParameter2(final List<Customer> customers){

	   SqlParameterSource[] params =
SqlParameterSourceUtils.createBatch(customers.toArray()); getSimpleJdbcTemplate().batchUpdate(
"INSERT INTO CUSTOMER (CUST_ID, NAME, AGE) VALUES (:custId, :name, :age)",
params); }

Spring Named Parameters examples in SimpleJdbcTemplate的更多相关文章

  1. 【spring data jpa】repository中使用@Query注解使用hql查询,使用@Param引用参数,报错:For queries with named parameters you need to use provide names for method parameters. Use @Param for query method parameters, or when on

    在spring boot中, repository中使用@Query注解使用hql查询,使用@Param引用参数 如题报错: For queries with named parameters you ...

  2. Hibernate占位符警告:use named parameters or JPA-style positional parameters instead.

    Hibernate占位符警告:use named parameters or JPA-style positional parameters instead. >>>>> ...

  3. 解决 hibernate cannot define positional parameter after any named parameters have been defined

    解决 hibernate  cannot define positional parameter after any named parameters have been defined 把模糊查询的 ...

  4. [DEPRECATION] Encountered positional parameter near xxx Positional parameter are considered deprecated; use named parameters or JPA-style positional parameters instead.

    WARN:30 20:55:45,340ms- [HqlSqlWalker]1009行-[DEPRECATION] Encountered positional parameter near line ...

  5. Spring JdbcTemplate Querying examples

    Here are few examples to show you how to use JdbcTemplate query() methods to query or extract data f ...

  6. Spring + JdbcTemplate + JdbcDaoSupport examples

    In Spring JDBC development, you can use JdbcTemplate and JdbcDaoSupport classes to simplify the over ...

  7. 吴裕雄--天生自然python TensorFlow图片数据处理:No module named 'tensorflow.examples.tutorials'解决办法

    import numpy as np import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_dat ...

  8. Positional parameter are considered deprecated; use named parameters or JPA-style positional parameters instead.

    这行代码: List<Cat> catList =session.createQuery("from Cat p where p.name.first_name=?") ...

  9. Spring JdbcTemplate操作小结

    Spring 提供了JdbcTemplate 来封装数据库jdbc操作细节: 包括: 数据库连接[打开/关闭] ,异常转义 ,SQL执行 ,查询结果的转换 使用模板方式封装 jdbc数据库操作-固定流 ...

随机推荐

  1. 【HDOJ】4801 Pocket Cube 的几种解法和优化

    1. 题目描述给定一个$2 \times 2 \times 2$的魔方,当某个面上的4个小块颜色均相同时,称这个面为complete.求对这个魔方进行$n \in [1,7]$次旋转(沿某个面顺时针或 ...

  2. MongoDB sharding cluster Step by Step

    本篇讲述MongoDB的 Sharding Cluster 的详细步骤,按着做理论上不会有什么错误. 关于说着里边的参数.变量.和设置,没有用到很多,只用到了关键的一些,其他的可以参考MongoDB的 ...

  3. poj 1061 青蛙的约会(扩展gcd)

    题目链接 题意:两只青蛙从数轴正方向跑,给出各自所在位置, 和数轴长度,和各自一次跳跃的步数,问最少多少步能相遇. 分析:(x+m*t) - (y+n*t) = p * L;(t是跳的次数,L是a青蛙 ...

  4. Go Deeper(2010成都现场赛题)(2-sat)

    G - Go Deeper Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Description ...

  5. Android+Eclipse+Java:在“正在启动 CrazySnake”期间发生了内部错误, java.lang.NullPointerException

    删除工作空间下的.metadata 文件夹 重启 Eclipse 清理工作空间

  6. C# 编写的串口通信程序

    如果,翻看我之前的博客,会找到一篇用I/O模拟IIC通信的程序文章.好吧,如果找不到可以点击这里,这里就不在赘述了,系统也已经完全调试通过了. 今天的任务是,把测试得到的数据在上位机的界面上显示出来, ...

  7. 学习:java设计模式—工厂模式

    一.工厂模式主要是为创建对象提供过渡接口,以便将创建对象的具体过程屏蔽隔离起来,达到提高灵活性的目的. 工厂模式在<Java与模式>中分为三类: 1)简单工厂模式(Simple Facto ...

  8. Java [Leetcode 204]Count Primes

    题目描述: Description: Count the number of prime numbers less than a non-negative number, n. 解题思路: Let's ...

  9. SSH整合所需jar

    Struts2.1.8+Hibernate3.2+Spring4.1.6+MySql ant-1.6.5.jar ant-antlr-1.6.5.jar ant-junit-1.6.5.jar ant ...

  10. $( ).focus()与$( )[0].focus()区别

    $( #id).focus()与$( #id)[0].focus()没有区别,因为id必须是唯一的.如果同一页面出现多个相同的ID(这是不符合w3c规范的),$(#id)也只会拿到第一个该ID,后面的 ...