Open snaag opened 1 year ago
@Entity
data-jpa
사용data-jpa
를 쓰게 되면, 데이터 변경 시 @Transactional
필요함implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=none
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 {
// data-jpa 라이브러리를 받았음 (build.gradle), 그러면 spring boot 가 알아서 EntityManager 를 만들어줌, 얘가 DB 랑 다 연결해줌
// JPA 는 entity manager 라는걸로 모든걸 함
private final EntityManager em;
public JpaMemberRepository(EntityManager em) {
this.em = em;
}
@Override
public Member save(Member member) {
em.persist(member); // persist: 영속하다, 저장하다
return member;
}
@Override
public Optional<Member> findById(Long id) {
Member member = em.find(Member.class, id); // 조회할 type, 식별자 (PK), 요게 select 문임
return Optional.ofNullable(member);
}
@Override
public Optional<Member> findByName(String name) {
// PK 로 조회하는게 아니기 때문에...
List<Member> result = em.createQuery("select m from Member m where m.name = :name", Member.class)
.setParameter("name", name)
.getResultList();
return result.stream().findAny();
}
@Override
public List<Member> findAll() {
return em.createQuery("select m from Member m", Member.class)
.getResultList();
}
}
package hello.hellospring;
import hello.hellospring.repository.*;
import hello.hellospring.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.persistence.EntityManager;
import javax.sql.DataSource;
@Configuration
public class SpringConfig {
EntityManager em;
@Autowired
public SpringConfig(EntityManager em) {
this.em = em;
}
// spring bean 에 등록하라는 뜻
@Bean
public MemberService memberService() {
return new MemberService(memberRepository());
}
@Bean
public MemberRepository memberRepository() {
return new JpaMemberRepository(em);
}
}