Spring Named Parameters examples in SimpleJdbcTemplate
来源:互联网 发布:python处理excel数据 编辑:程序博客网 时间:2024/06/05 16:35
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 Named Parameters examples in SimpleJdbcTemplate
- Spring SimpleJdbcTemplate Querying examples
- Spring SimpleJdbcTemplate Querying examples
- SPring SimpleJdbcTemplate
- Named parameters
- Spring 2.0的SimpleJdbcTemplate
- Spring SimpleJdbcTemplate教程
- Spring SimpleJdbcTemplate batchUpdate() example
- Spring SimpleJdbcTemplate batchUpdate() example
- Hibernate named query examples
- Named Parameters for PreparedStatement
- kettle Named Parameters
- Spring+SimpleJdbcTemplate+SimpleJdbcDaoSupport+SimpleJdbcInsert+SimpleJdbcCall
- Spring中SimpleJdbcTemplate查询用法
- Spring的JdbcTemplate、NamedParameterJdbcTemplate、SimpleJdbcTemplate
- Spring的JdbcTemplate、NamedParameterJdbcTemplate、SimpleJdbcTemplate
- Spring的JdbcTemplate、NamedParameterJdbcTemplate、SimpleJdbcTemplate
- Spring JdbcTemplate Querying examples
- U盘做启动盘后恢复原始容量
- Spring JdbcTemplate batchUpdate() example
- Spring SimpleJdbcTemplate Querying examples
- Spring SimpleJdbcTemplate batchUpdate() example
- Spring Named Parameters examples in SimpleJdbcTemplate
- 1021. Deepest Root (25) DFS & (a little idea )
- 数据结构学习之路--序
- Maven + Spring + Hibernate + MySql Example
- 数据结构学习之路-第一章:绪论
- Maven + (Spring + Hibernate) Annotation + MySql Example
- 黑马程序员--JAVA多线程
- HDU 2594 (KMP入门)
- C++语言的getline在cl和g++中的不同语义