为什么我们使用 MapSqlParameterSource

use*_*914 2 spring spring-jdbc

我是 spring 世界的新手,在上面的代码中我理解查询,但我不明白为什么使用“new MapSqlParameterSource("username" ,username)”?

  public boolean exists(String username) {      
       return jdbc.queryForObject("select count(*) from users where username=:username",
           new MapSqlParameterSource("username" ,username),Integer.class)>0;                                        }
Run Code Online (Sandbox Code Playgroud)

使用它的目的是什么?

提前致谢

Man*_*dan 8

这是因为一些开发人员不喜欢?在 sql 语句中使用。这?是如何命名或引用的(主要是第一个):

  • 占位符
  • 绑定参数

因此 Spring 提供了相同的方法,例如 Hibernate/JPA 如何:parameter通过MapSqlParameterSource类来处理它。

我建议你也做一个研究RowMapper<T>