SPRING IN ACTION 第4版笔记-第十章Hitting the database with spring and jdbc-004-使用NamedParameterJdbcTemplate

为了使查询数据库时,可以使用命名参数,则要用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);
}
上一篇:Linux架构思维导图


下一篇:Socket网络编程--Libev库学习(1)