-
[๊ฐ์] ์ค์ ! ์คํ๋ง ๋ถํธ์ JPA ํ์ฉ1 - ์น ์ ํ๋ฆฌ์ผ์ด์ ๊ฐ๋ฐ 1SPRING/INFLEARN 2021. 9. 15. 05:36
์๋ก์ด ํ๊ธฐ ์๊ฐ์ ์ค์ ! ์คํ๋ง ๋ถํธ์ JPA ํ์ฉ1 - ์น ์ ํ๋ฆฌ์ผ์ด์ ๊ฐ๋ฐ - ์ธํ๋ฐ | ๊ฐ์
์ค๋ฌด์ ๊ฐ๊น์ด ์์ ๋ก, ์คํ๋ง ๋ถํธ์ JPA๋ฅผ ํ์ฉํด์ ์น ์ ํ๋ฆฌ์ผ์ด์ ์ ์ค๊ณํ๊ณ ๊ฐ๋ฐํฉ๋๋ค. ์ด ๊ณผ์ ์ ํตํด ์คํ๋ง ๋ถํธ์ JPA๋ฅผ ์ค๋ฌด์์ ์ด๋ป๊ฒ ํ์ฉํด์ผ ํ๋์ง ์ดํดํ ์ ์์ต๋๋ค., ๋ณธ
www.inflearn.com
1. ํ๋ก์ ํธ ํ๊ฒฝ ์ค์
- ํ๋ก์ ํธ ์์ฑ
ํ๋ก์ ํธ ๋ง๋ค ๋ ๋ฃ์ ์์กด๊ด๊ณ๋ค Lombok ํ๋ฌ๊ทธ์ธ ์ค์น ํ (๋ ๋์ด์์ด ์๋ต)
Gradle -> IntelliJ IDEA ์ด๊ฑฐ ์ฒดํฌํ๊ธฐ - ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ดํด๋ณด๊ธฐ
cmd์ฐฝ์์ ์ด๊ฑธ ์น๋ฉด ์์กด๊ด๊ณ๊ฐ ์ฃผ๋ฃจ๋ฃฉ ๋์จ๋ค. - View ํ๊ฒฝ ์ค์
๊ฐ์ด๋๊ฐ ํ์ํ๋ค๋ฉด : https://spring.io/guides
Spring | Guides
spring.io
thymeleaf๋ ์ด๋ ๊ฒ ๋งคํ. ์ค์ ๋ฐ๊ฟ ์ ์๋ค.
resources:templates/ +{ViewName}+ .html
์์ ํ๋ ๊ฑฐ ๋ค์ run ์์ํค๊ณ ๋ ๋ฐ์ํ๋ ๋ฐฉ๋ฒ : devtools
implementation 'org.springframework.boot:spring-boot-devtools'
<!--hello.html--> <!DOCTYPE HTML> <html xmlns:th="http://www.thymeleaf.org"> <head> <title>Hello</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> </head> <body> <p th:text="'์๋ ํ์ธ์. ' + ${data}" >์๋ ํ์ธ์. ์๋</p> </body> </html>
//HelloController.java @Controller public class HelloController { @GetMapping("hello") public String hello(Model model){ model.addAttribute("data", "hello!!!"); return "hello"; } }
- H2 ๋ฐ์ดํฐ๋ฒ ์ด์ค ์ค์น
๋๋ H2 ์ค์น๊ฐ ๋ค ๋์ด์์ผ๋ฏ๋ก (์๋ ๋งํฌ์์ ์์ํ์๋ง์ ์ค์นํจ) ๊ทธ๋ฅ ์งํ
https://dowlsovo.tistory.com/28?category=1013818
[๊ฐ์] ์คํ๋ง ์ ๋ฌธ - ์ฝ๋๋ก ๋ฐฐ์ฐ๋ ์คํ๋ง ๋ถํธ, ์น MVC, DB์ ๊ทผ ๊ธฐ์ 3
inf.run/reCZ [๋ฌด๋ฃ] ์คํ๋ง ์ ๋ฌธ - ์ฝ๋๋ก ๋ฐฐ์ฐ๋ ์คํ๋ง ๋ถํธ, ์น MVC, DB ์ ๊ทผ ๊ธฐ์ - ์ธํ๋ฐ | ๊ฐ์ ์คํ๋ง ์ ๋ฌธ์๊ฐ ์์ ๋ฅผ ๋ง๋ค์ด๊ฐ๋ฉด์ ์คํ๋ง ์น ์ ํ๋ฆฌ์ผ์ด์ ๊ฐ๋ฐ ์ ๋ฐ์ ๋น ๋ฅด๊ฒ ํ์ตํ ์ ์
dowlsovo.tistory.com
์๋์ฐ ๊ธฐ์ค ์ผ๋ก h2 ์คํ์ํค๋ฉด (ip):8082/์ด์ฉ๊ตฌ ์ ์ฉ๊ตฌ ํ๋ ์ฐฝ์ด ๋ฌ๋ค.
๋ค๋ ๊ทธ๋๋ก ๋ ๋๊ณ ip๋ฅผ localhost๋ก ๋ณ๊ฒฝํ๋ค.
์ด๋ฐ ์ฐฝ์ด ๋จ๋๋ฐ ์์ ๊ฐ์ด ์นธ์ ์ฑ์ด๋ค. (๋ ๋ฒ์งธ ์ ์๋ถํฐ๋ jdbc:h2:tcp://localhost/~/jpashop์ผ๋ก ์ ๋ ฅ)
C:\Users\์ฌ์ฉ์์ด๋ฆ ์ ์ด ํ์ผ์ด ์์ฑ๋๋ค. - JPA์ DB ์ค์ , ๋์ํ์ธ
#application.yml spring: datasource: url: jdbc:h2:tcp://localhost/~/jpashop #MVCC=TRUE๋ ๋ ๋นจ๋ผ์ง๊ฒ ํจ username: sa password: driver-class-name: org.h2.Driver jpa: hibernate: ddl-auto: create properties: hibernate: # show_sql: true #System.out:์ฌ์ฉ ์ํจ format_sql: true logging: level: org.hibernate.SQL: debug #logger org.hibernate.type: trace
//MemberRepository.java @Repository //component scan ๋์. ์๋ ๋น๋ฑ๋ก public class MemberRepository { @PersistenceContext //์ํฐํฐ ๋ฉ๋์ ( EntityManager ) ์ฃผ์ private EntityManager em; public Long save(Member member){ em.persist(member); return member.getId(); } public Member find(Long id){ return em.find(Member.class, id); } }
ํ ์คํธ ์ฝ๋ ์์ฑ ctrl+shift+t
junit4๋ก ์์ฑ
//MemberRepositoryTest.java @RunWith(SpringRunner.class) @SpringBootTest public class MemberRepositoryTest { @Autowired MemberRepository memberRepository; @Test @Transactional //test์ ๋ถ์ผ๋ฉด ๋ฐ์ดํฐ ๋กค๋ฐฑ @Rollback(false) //๋กค๋ฐฑ ์์ํ๋ฉด public void testMember() throws Exception{ //given Member member = new Member(); member.setUsername("MemberA"); //when Long savedId = memberRepository.save(member); Member findMember = memberRepository.find(savedId); //then Assertions.assertThat(findMember.getId()).isEqualTo(member.getId()); Assertions.assertThat(findMember.getUsername()).isEqualTo(member.getUsername()); Assertions.assertThat(findMember).isEqualTo(member); } }
๋ก๊ทธ ์์๊ฒ ๋จ๊ธฐ๊ธฐ
implementation 'com.github.gavlyukovskiy:p6spy-spring-boot-starter:1.5.6'
https://github.com/gavlyukovskiy/spring-boot-data-source-decorator
GitHub - gavlyukovskiy/spring-boot-data-source-decorator: Spring Boot integration with p6spy, datasource-proxy, flexy-pool and s
Spring Boot integration with p6spy, datasource-proxy, flexy-pool and spring-cloud-sleuth - GitHub - gavlyukovskiy/spring-boot-data-source-decorator: Spring Boot integration with p6spy, datasource-p...
github.com
์กฐ๊ธ ๋ฌด๊ฑฐ์์ ธ์ ์ค๋ฌด์์๋ ์ฐ๊ธฐ ์ข ๊ทธ๋ฐ ๋ถ๋ถ์ด ์๋ค.
2. ๋๋ฉ์ธ ๋ถ์ ์ค๊ณ
- ์๊ตฌ์ฌํญ ๋ถ์
โป ํ์ ๊ธฐ๋ฅ
ํ์ ๋ฑ๋ก
ํ์ ๋ชฉ๋ก - ์กฐํ
โป ์ํ ๊ธฐ๋ฅ
์ํ ๋ฑ๋ก
์ํ ๋ชฉ๋ก - ์กฐํ, ์์
โป ์ฃผ๋ฌธ ๊ธฐ๋ฅ
์ํ ์ฃผ๋ฌธ
์ฃผ๋ฌธ ๋ด์ญ - ์กฐํ, ์ทจ์- ๋๋ฉ์ธ ๋ชจ๋ธ๊ณผ ํ ์ด๋ธ ์ค๊ณ
์ค๋ฌด์์ ์ฐ๋ฉด ์๋๋ ๋ถ๋ถ๋ค์ด ์ข ์๋ค.
ex) ๋ค๋๋ค ์๋๋ค, ์๋ฐฉํฅ ์ฐ๊ด๊ด๊ณ ์๋๋ค.
์ธ๋ ํค๊ฐ ์๋ ๊ณณ์ ์ฐ๊ด๊ด๊ณ์ ์ฃผ์ธ์ผ๋ก ํ๋ผ
- ์ํฐํฐ ํด๋์ค ๊ฐ๋ฐ
- ๋ชจ๋ ์ํฐํฐ์ @Entity @Getter @Setter ๋ถ์ฌ์ค
- @ManyToMany, @ManyToOne, @OneToMany, @OneToOne ์ mapped๋นํ๋ ์ ๋ง (mapped = "")๋ถ์ฌ์ค
- @JoinColumn, @Table ๋ฑ์ผ๋ก ์ด๋ฆ ๋ง๋ค์ด์ค
- @Embedded, @Embeddable ๋ก(๋ ์ค ํ๋๋ง ์ฌ์ฉ ๊ฐ๋ฅ) ๋ฌถ๋ ์ญํ
- @Enumerated(EnumType.STRING)
๋๋์ด ์ฑ๊ณต. ์์๋ก OREDRS๋ง ํด๋์ Getter๋ ์๋ ์ด์ด๋์ง๋ง, Setter๋ ์ฌ๊ธฐ ์์ ์์๋ง ์ฝ๊ฒ ํ๊ธฐ ์ํด ์ด์ด๋๋๊ฒ. ๋์ค์ Setter๊ฐ ๋๋ฌด ๋ง์์ง๋ฉด ์ด๋์ ์์ ํ๋์ง ๋ชฐ๋ผ์ ๋ณต์กํด์ง๋ค.
์์ฑ์๋ ๊ธฐ๋ณธ ์์ฑ์ ํ๋๋, protected๋ก ๋ง๋ค์ด์ ์๋ก ์์ฑ ๋ชปํ๊ฒ ํ์.
ใ ฃ์ค๋ฌด์์๋ @ManyToMany๋ฅผ ์ฌ์ฉํ์ง ์๋๋ค. ์ค๊ฐ ํ ์ด๋ธ์ด ์ ๋งคํด์ง๋ค.
- ์ํฐํฐ ์ค๊ณ์ ์ฃผ์์
- Setter๋ฅผ ์ฌ์ฉํ์ง ๋ง์
- ๋ชจ๋ ์ฐ๊ด๊ด๊ณ๋ ์ง์ฐ๋ก๋ฉ์ผ๋ก - @XToOne์ FetchType์ (๋๋จธ์ง๋ LAZY๊ฐ ๊ธฐ๋ณธ๊ฐ์ด๋ผ ใฑใ ) LAZY๋ก ๋ฐ๊พผ๋ค. ์ฆ์๋ก๋ฉ์ผ๋ก ํด๋์ผ๋ฉด ๋์ค์ ๋์ฅํ๋จ
- ์ปฌ๋ ์ ์ ํ๋์์ ๋ฐ๋ก ์ด๊ธฐํํ๋ค. ๊ทธ๊ฒ์ด ๊ฐ์ฅ ์์ .
- ๊ด๋ก ์นด๋ฉ ์ผ์ด์ค ์ธ๋์ค์ฝ์ด(memberPoint member_point)
(์ ) _(์ธ๋์ค์ฝ์ด)
๋๋ฌธ์ ์๋ฌธ์ - cascade : ์์ ๋ฐ์ดํฐ๋ฅผ ์ ์ฅํ๋ฉด ์๋์ผ๋ก ๋ถ๋ฌ์์ง
- ์ฐ๊ด๊ด๊ณ ํธ์ ๋ฉ์๋
ex)
public void setMember(Member member) {
this.member = member;
member.getOrders().add(this);
}'SPRING > INFLEARN' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ