0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

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

0
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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?