Spring:Spring中集成JDBC;(二)

2014-11-23 23:41:21 · 作者: · 浏览: 1
方法 3、可变参数列表,列出了要绑定到查询上的索引参数值。


使用命名参数:

private static final String SQL_INSERT_SPITTER=
		"insert into spitter(username,password,fullname)"+
		"values(:username,:password,:fullname)";

public void addSpitter(Spitter spitter){
		Map
  
   params=new HashMap
   
    (); params.put("username",spitter.getUsername()); params.put("password",spitter.getPassword()); params.put("fullname",spitter.getFullName()); jdbcTemplate.update(SQL_INSERT_SPITTER,params); spitter.setId(queryForIdentity()); } 
   
  


SimpleJdbcDaoSupport,通过getSimpleJdbcTemplate()能够便捷地访问SimpleJdbcTemplete. 如下:

public voidaddSpitter(Spitterspitter){
		getSimpleJdbcTemplate().update(SQL_INSERT_SPITTER,
		spitter.getUsername(),
		spitter.getPassword(),
		spitter.getFullName(),
		spitter.getEmail(),
		spitter.isUpdateByEmail());
		spitter.setId(queryForIdentity());
}