JPA (Java Persistence API) Spring-boot์์ RDB์ ๊ด๋ฆฌ๋ฅผ ์ฉ์ดํ๊ฒ ํด์ฃผ๋ API์ด๋ค. (= ์๋ฐ ํ์ค ORM)
ORM(Object Relational Mapping): DB์ ๊ฐ์ฒด ์งํฅ ํ๋ก๊ทธ๋๋ฐ ๊ฐ์ ๋ฐ์ดํฐ๋ฅผ ๋ณํํ๋ ํ๋ก๊ทธ๋๋ฐ ๊ธฐ๋ฒ
build.gradle
์ ๋ค์์ ์ถ๊ฐํ๋ค.
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'com.h2database:h2'
}
spring-boot-starter-data-jpa
h2
domain/posts
ํด๋๋ฅผ ๋ง๋ ๋คPosts.java
๋ฅผ ๋ง๋ ๋ค.package com.example.demo.domain.posts;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
@Getter
@NoArgsConstructor
@Entity
public class Posts {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(length = 500, nullable = false)
private String title;
@Column(columnDefinition = "TEXT", nullable = false)
private String content;
private String author;
@Builder
public Posts(String title, String content, String author){
this.title = title;
this.content = content;
this.author = author;
}
}
@NoArgsConstructor
@Entity
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(length = 500, nullable = false)
@Builder
domain/posts
ํด๋ ์๋์ PostsRepository.java
๋ฅผ ๋ง๋ ๋ค.package com.example.demo.domain.posts;
import org.springframework.data.jpa.repository.JpaRepository;
public interface PostsRepository extends JpaRepository<Posts, Long>{
}
application.properties
์ ๋ค์์ ์ถ๊ฐํ๋ค.
# ์คํ๋๋ ์ฟผ๋ฆฌ ๋ก๊ทธ
spring.jpa.show-sql=true
# H2๋ฌธ๋ฒ ๋ก๊ทธ๋ฅผ MySQL๋ฌธ๋ฒ ๋ก๊ทธ๋ก ์ถ๋ ฅ
spring.jpa.properties.hibernate.dialect.storage_engine=innodb
spring.datasource.hikari.jdbc-url=jdbc:h2:mem:testdb;MODE=MYSQL
spring.datasource.hikari.username=rivernine
test
์๋์ ๋์ผ ๊ตฌ์กฐ์ ํด๋๋ฅผ ๋ง๋ ๋ค. domain/posts
PostsRepositoryTest.java
๋ฅผ ๋ง๋ ๋ค.package com.example.demo.domain.posts;
import static org.assertj.core.api.Assertions.assertThat;
import java.util.List;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@ExtendWith(MockitoExtension.class)
@SpringBootTest
public class PostsRepositoryTest {
@Autowired
PostsRepository postsRepository;
@AfterEach
public void cleanup() {
postsRepository.deleteAll();
}
@Test
public void board_save() {
String title = "test_title";
String content = "test_content";
postsRepository.save(Posts.builder()
.title(title)
.content(content)
.author("test@gmail.com")
.build());
List<Posts> postsList = postsRepository.findAll();
Posts posts = postsList.get(0);
assertThat(posts.getTitle()).isEqualTo(title);
assertThat(posts.getContent()).isEqualTo(content);
}
}
@AfterEach
postsRepository.save()
postsRepository.findAll()
Run Test
๋ฅผ ๋๋ฅด๋ฉด unit test๋ฅผ ์งํํ ์ ์๋ค.
๋๋ฒํฌ ์ฝ์
์ ํ์ธํ์.@AfterEach
๋ก ์ง์ ํด์ค method๋ฅผ ์คํํ๋ ๊ฒ์ ํ์ธํ ์ ์๋ค.# ๋น๋
./gradlew build
# Spring-boot ์คํ
java -jar ./build/lib/*.jar
๋ชจ๋ ์์ค๋ ๊นํ๋ธ์ ์ฌ๋ ค๋์๋ค.
์ฐธ๊ณ ์์ : ์คํ๋ง๋ถํธ์ AWS๋ก ํผ์ ๊ตฌํํ๋ ์น ์๋น์ค