1 분 소요

인프런 - 실전! 스프링 데이터 JPA 공부 내용

프로젝트 생성

  • https://start.spring.io/

  • groupId : study
  • artifact : data-jpa
  • packaging : jar
  • java : 21
  • dependencies :
    • Spring Data JPA
    • Spring Web
    • H2 Database
    • Lombok
  • build.gradle
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
plugins {  
    id 'java'  
    id 'org.springframework.boot' version '3.2.2'  
    id 'io.spring.dependency-management' version '1.1.4'  
}  
  
group = 'study'  
version = '0.0.1-SNAPSHOT'  
  
java {  
    sourceCompatibility = '21'  
}  
  
configurations {  
    compileOnly {  
       extendsFrom annotationProcessor  
    }  
}  
  
repositories {  
    mavenCentral()  
}  
  
dependencies {  
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'  
    implementation 'org.springframework.boot:spring-boot-starter-web'  
    implementation 'com.github.gavlyukovskiy:p6spy-spring-boot-starter:1.9.0'  
    compileOnly 'org.projectlombok:lombok'  
    runtimeOnly 'com.h2database:h2'  
    annotationProcessor 'org.projectlombok:lombok'  
    testImplementation 'org.springframework.boot:spring-boot-starter-test'  
}  
  
tasks.named('test') {  
    useJUnitPlatform()  
}
  • application.yml ```yml

spring:
datasource:
url: jdbc:h2:tcp://localhost/~/datajpa
username: sa
password:
driver-class-name: org.h2.Driver

jpa:
hibernate:
ddl-auto: create
properties:
hibernate:
# show_sql: true
format_sql: true

logging.level:
org.hibernate.SQL: debug

```

  • 폴더 구조

예제 도메인 모델

엔티티 클래스

ERD

공통 인터페이스 기능

  • 순수 JPA 기반 리포지토리 만들기
  • 스프링 데이터 JPA 공통 인터페이스 소개
  • 스프링 데이터 JPA 공통 인터페이스 활용

    순수 JPA 기반 리포지토리 만들기

  • 기본 CRUD
    • 저장
    • 변경 -> 변경 감지 사용
    • 삭제
    • 전체 조회
    • 단건 조회
    • 카운트

참고 : JPA에서 수정은 변경 감지 기능을 사용하면 된다. 트랜잭션 안에서 엔티티를 조회한 다음에 데이터를 변경하면, 트랜잭션 종료 시점에 변경 감지 기능이 작동해서 변경된 엔티티를 감지하고 UPDATE SQL을 실행한다.

태그: ,

카테고리:

업데이트:

댓글남기기