Sparta(JAVA심화3기) - TIL/스파르타 강의 - JPA, Docker, 입문, 숙련, 심화

JPA (Java Persistence API) - 1주차

Dev.99_tale 2025. 2. 11. 01:12

수업하기 앞서 JPA가 무엇인지 간단한 설명

JPA

  • 자바에서 데이터베이스를 쉽게 다룰 수 있도록 도와주는 ORM기술

JPA 장점

  • SQL을 직접 다룰 필요 없음 → 객체 중심의 프로그래밍 가능
  • 자동으로 SQL 생성 → 개발자가 쿼리 최적화에 신경 쓰지 않아도 됨
  • 변경 감지 (Dirty Checking) 지원 → save()를 호출하지 않아도 변경 사항 자동 반영
  • 트랜잭션 관리 용이 → @Transactional을 사용하여 간편하게 처리
  • 다양한 데이터베이스 지원 → DB 변경 시 SQL을 수정할 필요 없이 유지보수 가능

 

ORM (Object-Relational Mapping)  - (객체 - 관계 - 매핑)

  • SQL을 직접 작성하지 않고, 객체(Entity)를 사용하여 데이터베이스를 조작하는 방식

 

스파르타 JPA 강의  - 수업 목표

  • Spring Boot 설정
  • JDBC를 통한 데이터베이스 연결
  • 데이터베이스 관리
  • RawJPA와 Spring Data JPA를 이용한 데이터 처리 및 객체 관리

1주차 - 프로젝트 셋팅

  • 프로젝트 생성
    • Spring Boot를 활용하여 프로젝트를 설정하고 초기 의존성을 구성하는 방법
  • 프로젝트 의존성 이해하기 (Implementation)
    •  5가지 프로젝트에서 각각 사용하게될 의존성에 대해서 설명

 

의존성이란 ? 어떤 객체나 모듈이 다른 객체 또는 모듈에 의존하는 관계

  • compile 시점 의존성
    • 프로젝트를 컴파일 할 때 사용된다. 즉, 해당 라이브러리의 API를 사용할 수 있다.
  • runtime 시점 의존성
    • 애플리케이션을 실행할 때 사용된다. 즉, 실행 시에도 라이브러리가 필요하다. 
  • 은닉성
    • implementation으로 추가된 의존성은 다른 프로젝트 모듈에서 직접 접근할 수 없다. 이는 모듈 간의 캡슐화를 도와준다.

 

의존성 옵션 (build.gradle > dependencies)

  • implemenataion 옵션
    • 직접적인 의존성을 추가할 때 사용된다.
    • 이는 특정 라이브러리나 모듈이 프로젝트 컴파일 시 필요하지만, 해당 라이브러리가 프로젝트 외부로 공개될 필요가 없다는 것을 의미함.
  • runtimeOnly 옵션
    • compile 시점에는 필요없고 runtime 시점에만 필요한 라이브러리를 추가할 때 사용한다.
    • 대표적으로 Logging 관련 라이브러리, DB 관련 라이브러리 등이 있다.
  • testImplementation 옵션
    • 테스트 코드를 수행할 때 적용할 라이브러리를 추가할 때 사용한다. - Mockito, Junit
    • 테스트 용도로만 라이브러리나 빌트인 DB 사용하고 싶다면 해당 옵션을 사용하면 된다.

 

1. JDBC 프로젝트 세팅

  • runtimeOnly
    • h2database:h2
  • implemenataion
    • spring-boot-starter-jdbc
    • spring-boot-starter-web
# build.gradle
dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.springframework.boot:spring-boot-starter-jdbc'
    runtimeOnly 'com.h2database:h2'
}

 

2. JDBC Template 프로젝트 세팅

  • runtimeOnly
    • h2database:h2
  • implemenataion
    • spring-boot-starter-jdbc
    • spring-boot-starter-web
# build.gradle
dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.springframework.boot:spring-boot-starter-jdbc'
    runtimeOnly 'com.h2database:h2'
}

3. QueryMapper 프로젝트 세팅

  • runtimeOnly
    • h2database:h2
  • implemenataion
    • spring-boot-starter-jdbc
    • mybatis-spring-starter
    • spring-boot-starter-web
# build.gradle
dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-jdbc'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter:3.0.3'
    runtimeOnly 'com.h2database:h2'
}

4. QueryFileMapper 프로젝트 세팅

  • runtimeOnly
    • h2database:h2
  • implemenataion
    • spring-boot-starter-jdbc
    • mybatis-spring-starter
    • spring-boot-starter-web
# build.gradle
dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-jdbc'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter:3.0.3'
    runtimeOnly 'com.h2database:h2'
}

5. JPA 프로젝트 세팅

  • untimeOnly
    • h2database:h2
  • implemenataion
    • spring-boot-starter-jdbc
    • spring-boot-starter-jpa
    • querydsl-jpa
    • spring-boot-starter-web