Hitting the database with spring and jdbc-004-使用NamedParameterJdbcTemplate

Posted shamgod

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Hitting the database with spring and jdbc-004-使用NamedParameterJdbcTemplate相关的知识,希望对你有一定的参考价值。

为了使查询数据库时,可以使用命名参数,则要用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 }

 

以上是关于Hitting the database with spring and jdbc-004-使用NamedParameterJdbcTemplate的主要内容,如果未能解决你的问题,请参考以下文章

angularjs: $http.get with null parameters are not hitting the Web API controller

The database cluster was initialized with RELSEG_SIZE 1048576, but the server was compiled with RELS

RMANRMAN-05001: auxiliary filename conflicts with the target database

Oracle中Error while performing database login with the XXXdriver; Listener refused the connection wit

Can't connect to database with error (HY000/2002): No connection could be made because the target ma

There was a problem connecting to the database server. The following error had occured: Can't co