스프링 JdbcTemplate
- 순수 Jdbc와 동일한 환경설정을 하면 됨
- 스프링 JdbcTemplate과 MyBatis 같은 라이브러리는 JDBC API에서 본 반복 코드를 대부분 제거해줌
- SQL은 직접 작성해야 함
JdbcTemplateMemberRepository
package com.danny.learningspring.repository;
import com.danny.learningspring.domain.Member;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.simple.SimpleJdbcInsert;
import javax.sql.DataSource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
public class JdbcTemplateMemberRepository implements MemberRepository{
private final JdbcTemplate jdbcTemplate;
public JdbcTemplateMemberRepository(DataSource dataSource) {
jdbcTemplate = new JdbcTemplate(dataSource);
}
@Override
public Member save(Member member) {
SimpleJdbcInsert jdbcInsert = new SimpleJdbcInsert(jdbcTemplate);
jdbcInsert.withTableName("member").usingGeneratedKeyColumns("id");
Map<String, Object> parameters = new HashMap<>();
parameters.put("name", member.getName());
Number key = jdbcInsert.executeAndReturnKey(new MapSqlParameterSource(parameters));
member.setId(key.longValue());
return member;
}
@Override
public Optional<Member> findById(Long id) {
List<Member> result = jdbcTemplate.query("select * from member where id=?", memberRowMapper(), id);
return result.stream().findAny();
}
@Override
public Optional<Member> findByName(String name) {
List<Member> result = jdbcTemplate.query("select * from member where name=?", memberRowMapper(), name);
return result.stream().findAny();
}
@Override
public List<Member> findAll() {
return jdbcTemplate.query("select * from member", memberRowMapper());
}
private RowMapper<Member> memberRowMapper() {
return (rs, rowNum) -> {
Member member = new Member();
member.setId(rs.getLong("id"));
member.setName(rs.getString("name"));
return member;
};
}
}
- 생성자가 하나일 때는 @Autowired를 생략해도 됨
- option + enter -- replace with lamda
- RowMapper -- 원하는 형태의 결과값을 반환할 수 있음
- SELECT로 나온 여러개의 값을 반환할 수 있음
- 사용자가 원하는 형태로도 얼마든지 받을 수 있음
SpringConfig
package com.danny.learningspring;
import com.danny.learningspring.repository.JdbcMemberRepository;
import com.danny.learningspring.repository.JdbcTemplateMemberRepository;
import com.danny.learningspring.repository.MemberRepository;
import com.danny.learningspring.repository.MemoryMemberRepository;
import com.danny.learningspring.service.MemberService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.sql.DataSource;
@Configuration
@RequiredArgsConstructor
public class SpringConfig {
private final DataSource dataSource;
// @Autowired
// public SpringConfig(DataSource dataSource) {
// this.dataSource = dataSource;
// }
@Bean
public MemberService memberService(){
return new MemberService(memberRepository());
}
@Bean
public MemberRepository memberRepository() {
return new JdbcTemplateMemberRepository(dataSource);
// return new JdbcMemberRepository(dataSource);
// return new MemoryMemberRepository();
}
}
- Repository 교체
MemberServiceIntegrationTest 실행 결과
Passed
'Spring 입문' 카테고리의 다른 글
스프링MVC 입문23 (0) | 2022.11.14 |
---|---|
스프링MVC 입문22 (0) | 2022.11.14 |
스프링MVC 입문20 (0) | 2022.11.14 |
스프링MVC 입문19 (0) | 2022.11.14 |
스프링MVC 입문18 (0) | 2022.11.11 |
댓글