inblog logo
|
{CODE-RYU};
    SPIRNG

    H2 데이터베이스란? 설치 및 사용

    Jan 30, 2024
    H2 데이터베이스란? 설치 및 사용
    Contents
    1. H2 데이터베이스란?2. H2 데이터베이스 연결하기
     

    1. H2 데이터베이스란?

     
    H2 데이터베이스는 Java 프로그램에서 사용할 수 있는 경량 관계형 데이터베이스 관리 시스템이다. H2는 순수한 Java로 작성되어 있으며, 모바일 애플리케이션, 웹 애플리케이션 및 서버 기반 애플리케이션과 같은 다양한 환경에서 사용될 수 있다.
     
    경량성
    H2는 내장형 모드로 사용할 수 있어 별도의 데이터베이스 서버가 필요하지 않고, 애플리케이션 내에서 간단히 사용 가능
     
    포터블
    Java로 작성되어 있으며, Java Virtual Machine (JVM) 상에서 동작하므로 플랫폼 간 이식성이 뛰어남
     
    속도
    H2는 높은 성능을 제공하며, 메모리에서도 동작할 수 있어 빠른 데이터 액세스 가능
     
    자체적인 콘솔 애플리케이션
    H2는 자체적으로 제공하는 웹 기반 콘솔을 통해 데이터베이스 관리 및 쿼리 작성 가능
     
    다양한 모드 지원
    H2는 내장 모드 외에도 클라이언트-서버 모드로 사용할 수 있어 여러 애플리케이션에서 동시에 데이터베이스에 액세스할 수 있다.
     
    💡
    H2 데이터베이스는 주로 개발 및 테스트 목적으로 많이 사용된다.
     
     

    2. H2 데이터베이스 연결하기

     
    notion image
     
    새로운 프로젝트를 생성할 때 H2 Database 를 선택한다.
     
    build.gradle
    dependencies { implementation 'org.springframework.boot:spring-boot-starter-data-jpa' //하이버네이트 기술 내장, @Entity 를 찾음. 보고 테이블 생성 쿼리를 만들어냄. implementation 'org.springframework.boot:spring-boot-starter-mustache' implementation 'org.springframework.boot:spring-boot-starter-web' compileOnly 'org.projectlombok:lombok' developmentOnly 'org.springframework.boot:spring-boot-devtools' runtimeOnly 'com.h2database:h2' //db연결 runtimeOnly 'com.mysql:mysql-connector-j' //db연결 annotationProcessor 'org.projectlombok:lombok' testImplementation 'org.springframework.boot:spring-boot-starter-test' }
    resources/templates/application.yml
    spring: profiles: active: - dev //dev 는 개발 모드 , prod 는 서비스 모드를 설정한 것. 현재는 개발모드
    resources/templates/apllication-dev.yml
    server: servlet: encoding: charset: utf-8 force: true port: 8080 spring: datasource: driver-class-name: org.h2.Driver url : jdbc:h2:mem:test;MODE=MySQL // MODE=MySQL 은 방언 설정 username : sa password : //db 연결 h2: console: enabled: true //웹에 연결될 수 있게 jpa: hibernate: ddl-auto: create show-sql: true //서버가 실행될 때 @entity 되어있는걸 크리에이트함. //ibernate 가 실행될 때 show-sql: true 면 내용 띄워줌
    java/shop.mtcoding.blog/user/User
    package shop.mtcoding.blog.user; import lombok.Data; import javax.persistence.*; //db에서 조회된 user 데이터베이스 값을 여기에 받음 @Data //게터세터 @Entity // 이게 붙으면 테이블 생성됨 @Table(name="user_tb") // 테이블명 public class User { @Id // 프라이머리키 설정 @GeneratedValue(strategy = GenerationType.IDENTITY) //auto_increment private int id ; @Column(unique = true) // 유저네임은 유니크 private String username; @Column(length = 60,nullable = false) //비밀번호 길이는 60, null 불가 private String password; private String email; }
     
     
    notion image
     
    서버를 실행하면 콘솔창에 H2콘솔 사용가능하다고 뜸.
     
    http://localhost:8080/h2-console/
     
     
    notion image
    브라우저에 URL을 입력하면 이런 창이 뜬다.
    JDBC URL 은 apllication-dev.mustache 의 URL 값을 입력한다.
     
    notion image
     
     
    H2 데이터베이스가 연결된다.
    shop.mtcoding.blog/user/User 의 @Entity 와 @Table(name="user_tb") 에 의해 서버가 실행되면 테이블이 만들어진다.
     
    Share article

    {CODE-RYU};

    RSS·Powered by Inblog