Spring/스프링 입문

h2 데이터베이스 설치 및 연동

코징 2022. 3. 8. 16:41

h2 데이터를 설치하고 Spring에 연결하자.

다운로드 url : https://www.h2database.com/html/download-archive.html

 

Archive Downloads

 

www.h2database.com

1) version : 1.4.200 을 설치

2) 설치 파일을 압축을 풀은 후 h2.bat 파일을 실행

3) JDBC url을 프로토콜로 연결하자.

4) 멤버 테이블 생성

CREATE TABLE member(
   id bigint generated by default as identity,
   name VARCHAR(255),
  primary key (id)
);

 

5) Spring H2 DataBase 연동

  • build.gradle의 dependencies에 implementation을 사용해 외부 라이브러리를 받는다.
dependencies {
   implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
   implementation 'org.springframework.boot:spring-boot-starter-web'
   implementation 'org.springframework.boot:spring-boot-starter-jdbc'
   runtimeOnly 'com.h2database:h2'
   testImplementation 'org.springframework.boot:spring-boot-starter-test'
}​
  • application.properties에 우리의 DB의 정보를 입력한다.
spring.datasource.url=jdbc:h2:tcp://localhost/~/test
spring.datasource.driver-class-name=org.h2.Driver

 이제 Srping에서 DB연결하는 환경설정을 완료하였고

Spring에서  DB를 사용해보자!

 

이 글은 인프런의

제목 : 스프링 입문 - 코드로 배우는 스프링 부트, 웹 MVC, DB 접근 기술

강사 : 김영한 님의 동영상을 참조해 만들었습니다.

https://www.inflearn.com/course/%EC%8A%A4%ED%94%84%EB%A7%81-%EC%9E%85%EB%AC%B8-%EC%8A%A4%ED%94%84%EB%A7%81%EB%B6%80%ED%8A%B8/dashboard