JPA
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
implementation 'org.springframework.boot:spring-boot-starter-web'
// implementation 'org.springframework.boot:spring-boot-starter-jdbc'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa' // jpa에 jdbc도 포함되어 있다.
runtimeOnly 'com.h2database:h2'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
}spring.datasource.url=jdbc:h2:tcp://localhost/~/test
spring.datasource.driver-class-name=org.h2.Driver
# 스프링부트 2.4부터는 꼭 추가해줘야 오류가 발생하지 않는다.
spring.datasource.username=sa
# jpa 설정 추가
# JPA가 생성하는 SQL을 호출한다.
spring.jpa.show-sql=true
# JPA는 테이블을 자동으로 생성하는 기능을 제공한다.
# none으로 설정하면 해당 기능을 끈다.
# create를 사용하면 Entity 정보를 바탕으로 테이블을 직접 생성한다.
spring.jpa.hibernate.ddl-auto=none// 테이블을 매핑해주는 애너테이션 = JPA가 관리하는 Entity라는 표시
@Entity
public class Member {
// PK 매핑
@Id
// 아이디를 디비에서 자동으로 만들어주는 전략을 설정한다.
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}스프링 데이터 JPA
스프링 데이터 JPA 기본 기능

Last updated