.\h2.bat
명령어 입력 ddl을 디렉토리를 하나 만들어서 따로 관리하는게 좋다고 함
dependencies { // 라이브러리들
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
implementation 'org.springframework.boot:spring-boot-starter-web'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
//데이터베이스 관련 라이브러리
implementation 'org.springframework.boot:spring-boot-starter-jdbc'
runtimeOnly 'com.h2database:h2'
}
spring.datasource.url=jdbc:h2:tcp://localhost/~/test
spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.username=sa
spring.datasource.url=jdbc:h2:tcp://localhost/~/test
: db urlspring.datasource.driver-class-name=org.h2.Driver
: h2 db 사용여기까지 하면 일단 스프링이 datasource를 db랑 연결하는것 까지 해줌. 즉 db에 접근하기 위한 준비가 된 것임
이제 전에 만들어놓은 MemberRepository를 구현하는 JdbcMemberRepository 클래스를 만들면 됨
클래스 코드는 그냥 복붙(옛날방식이라 참고만)
package hello.hellospring;
import hello.hellospring.repository.JdbcMemberRepository;
import hello.hellospring.repository.MemberRepository;
import hello.hellospring.repository.MemoryMemberRepository;
import hello.hellospring.service.MemberService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.sql.DataSource;
@Configuration
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 MemoryMemberRepository();
return new JdbcMemberRepository(dataSource);
}
}
@Autowired
로 생성자 주입이 가능함