포트폴리오/SPRINGBOOT 옛날

2-03 : ORM / JPA / H2-DB / JPA 셋팅

서버관리자 페페 2022. 9. 27. 20:08

단 하나의 맥락

: 개발 환경에서의 간이 DB 처리

 


Semiflow Bundle

: ORM 개념

: JPA 개념

: H2 DB 설치

: JPA 환경설정

 


상세 내용

- 질문이나 답변을 생성하면 데이터가 필요하고, 그 데이터를 처리하기 위해 DB가 필요

- 해당 DB를 다루기 위해서 SQL query가 필요한데, ORM(object relational mapping) 을 사용해 자바로 간단히 DB를 다룰 수 있음

- 데이터를 관리하는 데 사용되는 ORM class = Entity

- ORM을 사용하면 DB 종류에 상관 없이 일관된 코드를 유지 가능하여, 유지 보수의 편리

- 내부에서 안전한 SQL 쿼리를 자동 생성하므로, (개발자가 달라도) 통일된 query 작성 및 오류 발생률 낮춤

 

insert into question (subject, content) values ('안녕하세요', '가입 인사드립니다 ^^');
insert into question (subject, content) values ('질문 있습니다', 'ORM이 궁금합니다');

일반적 Query

 

Question q1 = new Question();
q1.setSubject("안녕하세요");
q1.setContent("가입 인사드립니다 ^^");
this.questionRepository.save(q1);

Question q2 = new Question();
q2.setSubject("질문 있습니다");
q2.setContent("ORM이 궁금합니다");
this.questionRepository.save(q2) 

ORM 

 

 

 


JPA

스프링부트는  JPA(Java Persistence API) 를 사용하여 DB를 처리

JPA는 자바에서 ORM의 기술 표준으로  사용되는 Interface 모음

 


 

H2 database

- 개발이나 주로 소규모 프로젝트에서 사용되는 파일 기반 경량 DB

- H2 DB를 사용하여 빠르게 개발, 실제 운영은 좀 더 규모있는 DB를 사용한다

- 설치 > 셋팅 > 생성 > 확인

 

(... 생략 ...)

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    developmentOnly 'org.springframework.boot:spring-boot-devtools'
    compileOnly 'org.projectlombok:lombok'
    annotationProcessor 'org.projectlombok:lombok'
    runtimeOnly 'com.h2database:h2'
}

(... 생략 ...)

build.grade에 h2 DB 설치

 

 

# DATABASE
spring.h2.console.enabled=true
spring.h2.console.path=/h2-console
spring.datasource.url=jdbc:h2:~/local
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=

application.properties에 셋팅

  • spring.h2.console.enabled - H2 콘솔의 접속을 허용할지의 여부이다. true로 설정한다.
  • spring.h2.console.path - 콘솔 접속을 위한 URL 경로이다.
  • spring.datasource.url - 데이터베이스 접속을 위한 경로이다.
  • spring.datasource.driverClassName - 데이터베이스 접속시 사용하는 드라이버이다.
  • spring.datasource.username - 데이터베이스의 사용자명이다. (사용자명은 기본 값인 sa로 설정한다.)
  • spring.datasource.password - 데이터베이스의 패스워드이다. 로컬 개발 용도로만 사용하기 때문에 패스워드를 설정하지 않았다.

 

홈 디렉토리에 파일 생성

local.mv.db

 

작동 확인 및 JDBC URL 설정

 


JPA 환경설정

- 자바에서 H2 database를 사용하기 위해 build.gradle에 추가

(... 생략 ...)

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    developmentOnly 'org.springframework.boot:spring-boot-devtools'
    compileOnly 'org.projectlombok:lombok'
    annotationProcessor 'org.projectlombok:lombok'
    runtimeOnly 'com.h2database:h2'
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
}

(... 생략 ...)

 

 

application.properties 파일 셋팅

# DATABASE
spring.h2.console.enabled=true
spring.h2.console.path=/h2-console
spring.datasource.url=jdbc:h2:~/local
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=

# JPA
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.H2Dialect
spring.jpa.hibernate.ddl-auto=update

'포트폴리오 > SPRINGBOOT 옛날' 카테고리의 다른 글

2-05 : repository  (6) 2022.09.29
2-04 : Entity  (4) 2022.09.27
2-02 : Controller  (0) 2022.09.27
2-01 : Structure  (0) 2022.09.25
1-05 : Devtools, Lombok  (0) 2022.09.25