LoginSignup
0
0

More than 5 years have passed since last update.

NamedParameterJdbcTemplateをつかったselectのサンプル

Posted at

備忘録

import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Component;

import com.google.common.collect.ImmutableMap;

@Component
public class SampleDao {

    @Autowired
    private NamedParameterJdbcTemplate jdbcTemplate;

    public Optional<String> find(final String id) {
        var sql = "select id, name from samples where id = :id";
        final var params = ImmutableMap.<String, Object>builder().put("id", id).build();
        return jdbcTemplate.query(sql, params, (rs, num) -> rs.getString("name")).stream().findFirst();
    }

}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0