Spring Named Parameters examples in SimpleJdbcTemplate
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的更多相关文章
- 【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 ...
- Hibernate占位符警告:use named parameters or JPA-style positional parameters instead.
Hibernate占位符警告:use named parameters or JPA-style positional parameters instead. >>>>> ...
- 解决 hibernate cannot define positional parameter after any named parameters have been defined
解决 hibernate cannot define positional parameter after any named parameters have been defined 把模糊查询的 ...
- [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 ...
- Spring JdbcTemplate Querying examples
Here are few examples to show you how to use JdbcTemplate query() methods to query or extract data f ...
- Spring + JdbcTemplate + JdbcDaoSupport examples
In Spring JDBC development, you can use JdbcTemplate and JdbcDaoSupport classes to simplify the over ...
- 吴裕雄--天生自然python TensorFlow图片数据处理:No module named 'tensorflow.examples.tutorials'解决办法
import numpy as np import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_dat ...
- 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=?") ...
- Spring JdbcTemplate操作小结
Spring 提供了JdbcTemplate 来封装数据库jdbc操作细节: 包括: 数据库连接[打开/关闭] ,异常转义 ,SQL执行 ,查询结果的转换 使用模板方式封装 jdbc数据库操作-固定流 ...
随机推荐
- View的个得区域函数getHitRect,getDrawingRect,getLocalVisibleRect,getGlobalVisibleRect(*)
注意: OnCreate()函数中 调用下面函数,结果全为0,要等UI控件都加载完了才能得到绘制时的值. getHitRect 以父控件的左上为原点,计算当前view在父控件的区域,不管父控件在屏幕的 ...
- openVPN使用
http://www.williamlong.info/archives/3814.html http://openvpn.ustc.edu.cn/ http://www.williamlong.in ...
- 宏ut_2pow_round
计算 m的整数倍 不大于n #define ut_2pow_round(n, m) ((n) & ~((m) - 1)) #include <stdio.h>#include &l ...
- EF DataBase First生成model的验证
如何避免在EF自动生成的model中的DataAnnotation被覆盖掉 相信很多人刚接触EF+MVC的时候,DataBase First模式生成model类中加验证信息的时候,会在重新生成mode ...
- LeetCode Contains Duplicate II (判断重复元素)
题意:如果有两个相同的元素,它们之间的距离不超过k,那么返回true,否则false. 思路:用map记录每个出现过的最近的位置,扫一边序列即可.扫到一个元素就判断它在前面什么地方出现过.本题数据有点 ...
- erl0001-Erlang 设计原则 process port io
Erlang原理 (转载自ITEYE cryolite博客 ps:精彩)by Robert Virding This is a description of some of the basic pro ...
- jQuery live与bind的区别
平时在使用jQuery进行AJAX操作的时候,新生成的元素事件会失效,有时候不得不重新绑定一下事件,但是这样做很麻烦.例如评论分页后对评论内容的JS验证会失效等.在jQuery1.3之前有一个插件会解 ...
- 【C#学习笔记】读文件
using System; using System.IO; namespace ConsoleApplication { class Program { static void Main(strin ...
- 【Java集合框架】规则集--Set
集合: Java主要支持三种: 1.规则集(Set) 用于存储一组不重复的元素 2.线性表(List) 用于存储一个由元素构成的有序集合 3.队列(Queue) 同与数据结构中的队列,存储用先进先出的 ...
- 【Java】List的三种遍历方法
public void List_Test(){ List<String>list = new ArrayList<String>(); for(int i = 0;i < ...