1. 라이브러리 가져오기
(생략)
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
(생략)
2. 서버 실행하기
실행 후 로그에 특정 주소가 나온다
jdbc부터 끝까지 복사 한다.
ex) jdbc:h2:mem:11270f92-a5f8-4cf5-a8c7-e1f8388876b3
3. h2 사용한다는 내용 설정하기
[application.properties]
spring.h2.console.enabled=true
4. h2 콘솔 진입하여 접속하기
http://localhost:8080/h2-console
접속 완료
위 처럼 로그 보면서 하면 힘드니
Schema를 서버 실행시 미리 생성해놓자
1. 실행시 testdb라는 이름으로 데이터베이스 생성할 수 있도록 설정
[application.properties]
spring.h2.console.enabled=true
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource는 뭘까?
Spring이 없었을 때는
DriverManager로 해당하는 드라이버를 가져와서
Connection을 얻고
DB접근하여
ResultSet으로 가져와서
루핑해 데이터를 가져와 사용했다.
그때와 다른 점은 크게 두가지인 것 같다.
1. DB Connection 자동화
2. Dataset ORM을 통해 POJO로 바로 사용가능 하게함
지금은 ORM이라는 걸 통해서
POJO(자바객체)로 칼럼과 매핑시켜
바로 가져와 사용할 수 있고
디비 커넥션에 대해 생각할 필요가 없다.
HikariCP의 Datasource가 이 모든 것을 담당하는 것 같다(확실하게는 모르겠음)
application properties에 선언된 datasource는
HikariCP의 datasource 객체인 듯 하고
URL을 선언해주면
datasource가 h2디비에 데이터베이스를 추가해주는 것 같다.
2. schema.sql 생성
[schema.sql]
create table test
(
id bigint not null,
name varchar(255) not null,
author varchar(255) not null,
primary key (id)
);
3. 서버 실행해 확인해보기
http://localhost:8080/h2-console
'서버' 카테고리의 다른 글
Spring boot3 - Spring JDBC에 대한 이해와 기본 실습 (2) | 2024.11.29 |
---|---|
Spring boot 3 - Thymeleaf 핫리로드까지는 아니지만 리로드시 적용시키기 (0) | 2024.11.27 |
Spring boot3 - Spring Actuator 사용하여 health check 하기 (2) | 2024.11.26 |
Spring boot3 - maven으로 빌드, 내장 tomcat은 어디있는지 확인하기 (0) | 2024.11.26 |
Spring boot3 - properties에 있는 환경변수를 bean으로 등록해 사용해보기 (2) | 2024.11.25 |