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