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数据库操作-固定流 ...
随机推荐
- Android开发之源码:多次点击事件的原理和实现
多次点击事件 多次点击事件原理:最后一次点击事件与第一次点击事件的时间间隔是否小于某个时间,当小于的时候,就认为这是一个多次点击事件. Android源码实现效果: import android.ap ...
- Git for windows 中文乱码解决方案
1.git status时显示乱码,如下: \316\304\261\276\316\304\265\265.txt 解决方案: $ git config --global core.quotepat ...
- POJ1037A decorative fence(好dp)
1037 带点组合的东西吧 黑书P257 其实我没看懂它写的嘛玩意儿 这题还是挺不错的 一个模糊的思路可能会好想一些 就是大体的递推方程 dp1[][]表示降序 dp2[][]表示升序 数组的含义为长 ...
- vi编辑器基本用法介绍
vi是Linux系统中编写文件的工具 如果vi出现乱码情况,需要升级vi,命令如下: sudo apt-get install vim //升级vi vi的启动方式有两种,直接使用vi命令和在vi命 ...
- 图表框架HelloCharts(1)线形图
效果图 1. 导入 .aar 2. fragment_line_chart.xml <RelativeLayout xmlns:android="http://schemas.andr ...
- Web Api 如何做上传文件的单元测试
代码如下: //--------上传------------ HttpClient client = new HttpClient(); #region MultipartFormDataConten ...
- [Sciter系列] MFC下的Sciter–2.Sciter中的事件,tiscript,语法
[Sciter系列] MFC下的Sciter–2.Sciter中的事件,tiscript,CSS部分自觉学习,重点说明Tiscript部分的常见语法和事件用法. 本系列文章的目的就是一步步构建出一个功 ...
- 根据中国气象局提供的API接口实现天气查询
中国气象局提供了三个天气查询的API接口: [1]http://www.weather.com.cn/data/sk/101190101.html [2]http://www.weather.com. ...
- memcache的最佳实践方案。
基本问题 1.memcached的基本设置 1)启动Memcache的服务器端 # /usr/local/bin/memcached -d -m 10 -u root -l 192.168.0.200 ...
- 【C#学习笔记】载入图片并居中
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...