NamedParameterJdbcTemplate

// some JDBC-backed DAO class...
private NamedParameterJdbcTemplate namedParameterJdbcTemplate;

public void setDataSource(DataSource dataSource) {
    this.namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(dataSource);
}

public int countOfActorsByFirstName(String firstName) {

    String sql = "select count(*) from T_ACTOR where first_name = :first_name";

    SqlParameterSource namedParameters = new MapSqlParameterSource("first_name", firstName);

    return this.namedParameterJdbcTemplate.queryForObject(sql, namedParameters, Integer.class);
}

MapSqlParameterSource

쿼리문에 입력할 파라미터를 Map 형식으로 지정한다.

public int useMapSqlParameterSource(String firstName) {
    String sql = "select count(*) from customers where first_name = :first_name";
    SqlParameterSource namedParameters = new MapSqlParameterSource("first_name", firstName);
    return namedParameterJdbcTemplate.queryForObject(sql, namedParameters, Integer.class);
}

다음과 같이 체인 형식으로도 지정할 수도 있다.

SqlParameterSource params = new MapSqlParameterSource()
    .addValue("name", "판매자1")
    .addValue("address", "마포구 용강동");

https://github.com/benelog/spring-jdbc-tips/blob/master/spring-jdbc-core.md