为了使查询数据库时,可以使用命名参数,则要用NamedParameterJdbcTemplate
1.Java文件配置
@Bean
public NamedParameterJdbcTemplate jdbcTemplate(DataSource dataSource) {
return new NamedParameterJdbcTemplate(dataSource);
}
2.在Dao层中使用
private static final String INSERT_SPITTER =
"insert into Spitter " +
" (username, password, fullname, email, updateByEmail) " +
"values " +
" (:username, :password, :fullname, :email, :updateByEmail)"; public void addSpitter(Spitter spitter) {
Map < String, Object > paramMap = new HashMap < String, Object > ();
paramMap.put("username", spitter.getUsername());
paramMap.put("password", spitter.getPassword());
paramMap.put("fullname", spitter.getFullName());
paramMap.put("email", spitter.getEmail());
paramMap.put("updateByEmail", spitter.isUpdateByEmail());
jdbcOperations.update(INSERT_SPITTER, paramMap);
}