dependencies {
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
runtimeOnly 'com.h2database:h2'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
}
implementation 'org.springframework.boot:spring-boot-starter-data-jpa' runtimeOnly 'com.h2database:h2'
<- 이 부분을 추가하였다.
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=none
경로 - resources/application.properties
package hello.hellospring.domain;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class Member {
@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;
}
}
@Entity
- JPA가 관리하는 Entity가 되도록 설정한다.
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
- 데이터베이스가 생성해주는 pk를 맵핑해준다.
package hello.hellospring.repository;
import hello.hellospring.domain.Member;
import javax.persistence.EntityManager;
import java.util.List;
import java.util.Optional;
public class JpaMemberRepository implements MemberRepository {
private final EntityManager em;
public JpaMemberRepository(EntityManager em) {
this.em = em;
}
public Member save(Member member) {
em.persist(member);
return member;
}
public Optional<Member> findById(Long id) {
Member member = em.find(Member.class, id);
return Optional.ofNullable(member);
}
public List<Member> findAll() {
return em.createQuery("select m from Member m", Member.class)
.getResultList();
}
public Optional<Member> findByName(String name) {
List<Member> result = em.createQuery("select m from Member m where
m.name = :name", Member.class)
.setParameter("name", name)
.getResultList();
return result.stream().findAny();
}
}
private final EntityManager em;
- JPA는 EntityManager를 사용하는데 EntityManager는 앞서 설정한 라이브러리를 통해 자동으로 생성되고 EntityManager를 사용해서 DB와의 통신을 할 수 있다.
EntityManager에서 제공하는 persist, find, createQuery 등을 통해서
데이터베이스와 통신을 하기위한 기능들을 사용할 수 있다.
import org.springframework.transaction.annotation.Transactional
@Transactional
public class MemberService {}
JPA를 통한 모든 데이터 변경은 트랜잭션 안에서 실행해야 하기 때문에 서비스 계층에 @Transactional
애노테이션을 추가해준다.
package hello.hellospring;
import hello.hellospring.repository.*;
import hello.hellospring.service.MemberService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.persistence.EntityManager;
import javax.sql.DataSource;
@Configuration
public class SpringConfig {
private final DataSource dataSource;
private final EntityManager em;
public SpringConfig(DataSource dataSource, EntityManager em) {
this.dataSource = dataSource;
this.em = em;
}
@Bean
public MemberService memberService() {
return new MemberService(memberRepository());
}
@Bean
public MemberRepository memberRepository() {
return new JpaMemberRepository(em);
}
}
EntityManager를 사용할 수 있도록 설정을 변경해주면 JPA를 사용하기 위한 설정이 끝난다.