본문 바로가기

Java/Spring Boot

03-Spring Data JPA

01. Spring Data JPA 적용

- build.gradle에 spring data jpa 및 h2 의존성 추가

compile('org.springframework.boot:spring-boot-starter-data-jpa')
compile('com.h2database:h2')

02. Spring Data JPA TEST

1) com.face.home.springboot에 domain.reviewboard 패키지 생성

2) Reviewboard.java (실제 DB와 매칭될 클래스. DB상의 테이블이라 생각하자)

package com.face.home.springboot.domain.reviewboard;

import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

import javax.persistence.*;

@Getter
@NoArgsConstructor
@Entity
public class ReviewBoard {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(length = 500, nullable = false)
    private String title;

    @Column(columnDefinition = "TEXT", nullable = false)
    private String content;

    @Column(nullable = false)
    private String author;

    @Builder
    public ReviewBoard(String title, String content, String author) {
        this.title = title;
        this.content = content;
        this.author = author;
    }

}

 

3) ReviewboardRepository.java (Reviewboard 클래스가 DB에 접근하게 함. 징검다리. 인터페이스로 생성)

package com.face.home.springboot.domain.reviewboard;

import org.springframework.data.jpa.repository.JpaRepository;

public interface ReviewboardRepository extends JpaRepository<ReviewBoard, Long> {}

 

4) Spring Data JPA 테스트 코드 작성

- test/java/com.face.home.springboot.domain.reviewboard/ReviewbvoardRepositoryTest.java

package com.face.home.springboot.domain.reviewboard;

import org.junit.After;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;

@RunWith(SpringRunner.class)
@SpringBootTest
public class ReviewbvoardRepositoryTest {

    @Autowired
    ReviewboardRepository reviewboardRepository;

    @After
    public void cleanup() {
        reviewboardRepository.deleteAll();
    }

    @Test
    public void 게시글저장_불러오기(){

        // given
        String title = "test title";
        String content = "test content";
        String author = "tester";

        reviewboardRepository.save(ReviewBoard.builder().title(title).content(content).author(author).build());

        // when
        List<ReviewBoard> reviewBoardsList = reviewboardRepository.findAll();

        // then
        ReviewBoard reviewBoard = reviewBoardsList.get(0);
        assertThat(reviewBoard.getTitle()).isEqualTo(title);
        assertThat(reviewBoard.getContent()).isEqualTo(content);
        assertThat(reviewBoard.getAuthor()).isEqualTo(author);

    }

}

 

03. Query Log 확인

- src/main/resources/application.properties

spring.jpa.show_sql = true

 

04. 출력되는 H2 쿼리 문법을 MySQL 버전으로 변경

- src/main/resources/application.properties

spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect

 

 

 

참고

스프링 부트와 AWS로 혼자 구현하는 웹서비스

반응형