snaag / TIL

https://github.com/snaag/todo3/issues
3 stars 0 forks source link

22-12-16-FRI #27

Open snaag opened 1 year ago

snaag commented 1 year ago
snaag commented 1 year ago

스프링 입문 > 스프링 DB 접근 기술

스프링 데이터 JPA

JPA?

구현체?

ORM?

JPA 를 spring 에서 쓰려면 -> data-jpa 사용

build.gradle

implementation 'org.springframework.boot:spring-boot-starter-data-jpa'

application.properties

spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=none

JpaMemberRepository.java

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();
    }
}

SpringConfig.java

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);
    }
}