-
1. 프로젝트 설정 및 테스트스프링/spring data jpa 2023. 2. 15. 21:37
1. 스프링 설정 파일 작성
application.yml
spring: datasource: url: jdbc:h2:mem:test # mem 쓰면 인메모리 데이터베이스를 사용한다. username: sa password: driver-class-name: org.h2.Driver h2: console: enabled: true # h2 콘솔에 접근이 가능하다. /h2-console 로 접속 가능. jpa: hibernate: ddl-auto: create # 애플리케이션 시작할 때마다 전체 drop 후 엔티티 기준 테이블 create properties: hibernate: # show_sql: true # jpa가 실행하는 쿼리를 콘솔에 찍는다. (아래 로그방식이 나음) format_sql: true # sql 을 이쁘게 보여준다. logging.level: org.hibernate.SQL: debug # hibernate sql 실행 로그를 남긴다. # org.hibernate.type: trace # 바인딩된 파라미터까지 볼 수 있다.
2. Entity 생성
Member.java
package study.datajpa.entity; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import lombok.Getter; import lombok.Setter; @Entity @Getter @Setter public class Member { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String username; }
3. JPA 만을 이용한 Repository 생성
MemberJpaRepository
package study.datajpa.repository; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import org.springframework.stereotype.Repository; import study.datajpa.entity.Member; @Repository public class MemberJpaRepository { @PersistenceContext // spring container가 entitiy manager 주입해줌. private EntityManager em; public Member save(Member member) { em.persist(member); return member; } public Member find(Long id) { return em.find(Member.class, id); } }
4. JpaRepository 를 상속한 repository 생성
MemberRepository.java
package study.datajpa.repository; import org.springframework.data.jpa.repository.JpaRepository; import study.datajpa.entity.Member; public interface MemberRepository extends JpaRepository<Member, Long> { }
5. 테스트 코드 작성
MemberJpaRepository.java
package study.datajpa.repository; import static org.junit.jupiter.api.Assertions.*; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.annotation.Rollback; import org.springframework.transaction.annotation.Transactional; import study.datajpa.entity.Member; @SpringBootTest @Transactional @Rollback(value = false) class MemberJpaRepositoryTest { @Autowired MemberJpaRepository memberJpaRepository; @Test void memberSaveAndFindTest() { //given String username = "userA"; Member member = new Member(username); Member savedMember = memberJpaRepository.save(member); //when Member findMember = memberJpaRepository.find(savedMember.getId()); //then assertEquals(member.getId(), findMember.getId()); assertEquals(member.getUsername(), findMember.getUsername()); assertEquals(member, findMember); // 엔티티 동일성 보장 } }
MemberRepository.java
package study.datajpa.repository; import static org.junit.jupiter.api.Assertions.*; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.annotation.Rollback; import org.springframework.transaction.annotation.Transactional; import study.datajpa.entity.Member; @SpringBootTest @Transactional @Rollback(false) public class MemberRepositoryTest { @Autowired MemberRepository memberRepository; @Test void memberSaveAndFindByIdTest() { // given Member member = new Member("memberA"); Member savedMember = memberRepository.save(member); // when Member findMember = memberRepository.findById(savedMember.getId()).get(); // then assertEquals(savedMember.getId(), findMember.getId()); assertEquals(savedMember.getUsername(), findMember.getUsername()); assertEquals(savedMember, findMember); } }
출처
김영한 - 스프링 데이터 JPA
'스프링 > spring data jpa' 카테고리의 다른 글
2. 예제 도메인 개발 (0) 2023.02.15