@Configuration
public class SpringConfig {
private DataSource dataSource;
@Autowired
public SpringConfig(DataSource dataSource) {
this.dataSource = dataSource;
}
@Bean
public MemberService memberService(){
return new MemberService(memberRepository());
}
@Bean
public MemberRepository memberRepository(){
// return new MemoryMemberRepository();
return new JdbcMemberRepository(dataSource);
}
}
(컴포넌트 스캔 방식이 아닌 직접 스프링 빈으로 등록하였기 때문에 Repository 수정이 간편하다.)
스프링 DB접근 기술 - 순수JDBC
(주의! 이렇게 JDBC API로 직접 코딩하는 것은 20년 전 이야기이다. 참고만 하고 넘어가자)
build.gradle 라이브러리 추가
application.properties 추가
(스프링부트 2.4부터는 `spring.datasource.username=sa`를 꼭 추가해주어야 한다. 그렇지 않으면
`Wrong user name or password` 오류가 발생한다.)
JdbcMemberRepository
(Connection을 얻거나 닫을때는 DataSourceUtils를 통해야 한다.)
SpringConfig 수정
(컴포넌트 스캔 방식이 아닌 직접 스프링 빈으로 등록하였기 때문에 Repository 수정이 간편하다.)
스프링 빈과 의존관계
회원관리 예제 - Repository개발 및 테스트 케이스 작성 데이터 : 회원 ID, 이름 기능 : 회원 등록 , 조회 MemberRepository.java (Interface) package hello.hellospring.repository; import hello.hellospring.d..
write-read.tistory.com
스프링 입문 - 코드로 배우는 스프링 부트, 웹 MVC, DB 접근 기술 - 인프런
스프링 입문자가 예제를 만들어가면서 스프링 웹 애플리케이션 개발 전반을 빠르게 학습할 수 있습니다. 초급 프레임워크 및 라이브러리 웹 개발 서버 개발 Back-End Java Spring MVC Spring Boot 온라인
www.inflearn.com
'기록 > 스프링부트 시작하기' 카테고리의 다른 글