๐Ÿ’ป Programming/Spring

๐Ÿ’ป Programming/Spring

[Spring] DTO ↔ Entity ๋ณ€ํ™˜ํ•˜๊ธฐ

16๋ฒˆ์งธ ๊ณผ์ œ [Spring] Entity, DTO, VO ์˜ ๊ฐœ๋…๊ณผ ํŠน์ง• 12๋ฒˆ์งธ ๊ณผ์ œ Entity - ์‹ค์ œ DB์˜ ํ…Œ์ด๋ธ”๊ณผ ๋งคํ•‘๋˜๋Š” ๊ฐ์ฒด - id๋ฅผ ํ†ตํ•ด ๊ฐ๊ฐ์˜ Entity๋ฅผ ๊ตฌ๋ถ„ํ•œ๋‹ค - ๋กœ์ง์„ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋‹ค import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.GeneratedValue; impo sooz.tistory.com ์ด์ „์— Entity์™€ DTO, VO์˜ ๊ฐœ๋…๊ณผ ํŠน์ง•์„ ์•Œ์•„๋ณด์•˜๋‹ค. ๋ฐฐ๋‹ฌ API ๊ตฌํ˜„ ํ”„๋กœ์ ํŠธ๋ฅผ ์ง„ํ–‰ํ•˜๋ฉด์„œ ์šฐ์„ ์ ์œผ๋กœ Food Entity ๋ฅผ ๊ตฌํ˜„ํ•˜์˜€๊ณ  ์ด๋ฅผ DTO๋กœ ๋ณ€ํ™˜ํ•˜๋ ค๊ณ  ํ•œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  Entity ์™€ DTO ๋Š” ๋ถ„๋ฆฌํ•˜์—ฌ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค๊ณ  ํ•œ๋‹ค. ์™œ ์ธ์ง€๋Š” ์•„๋ž˜์—์„œ ํ™•..

๐Ÿ’ป Programming/Spring

[Spring] DDD ์„ค๊ณ„์™€ SQL ์ค‘์‹ฌ ์„ค๊ณ„

13๋ฒˆ์งธ ๊ณผ์ œ DDD (Domain Driven Design) ์„ค๊ณ„ ๊ฐœ๋… DDD ์„ค๊ณ„๋ž€ ๋ฐ์ดํ„ฐ ์ค‘์‹ฌ ์„ค๊ณ„์—์„œ ๋ฒ—์–ด๋‚˜ ๋„๋ฉ”์ธ์„ ๊ธฐ๋ฐ˜์œผ๋กœ ์„ค๊ณ„ํ•˜๋Š” ๋ฐฉ์‹์„ ์˜๋ฏธํ•œ๋‹ค. ๋ฐฐ๋‹ฌ ์„œ๋น„์Šค๋ฅผ ๊ฐœ๋ฐœํ•œ๋‹ค๊ณ  ์ƒ๊ฐํ•˜๋ฉด, ์Œ์‹, ํšŒ์›, ์ฃผ๋ฌธ ๋“ฑ์œผ๋กœ ๋น„์ฆˆ๋‹ˆ์Šค ๋„๋ฉ”์ธ์„ ๋‚˜๋ˆ„์–ด ์„ค๊ณ„๋ฅผ ์‹œ์ž‘ํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ์Œ์‹์„ ์ƒ์œ„ ๋„๋ฉ”์ธ์ด๋ผ๊ณ  ํ•œ๋‹ค๋ฉด ์Œ์‹์˜ ์ด๋ฆ„, ๊ฐ€๊ฒฉ, ์˜ต์…˜ ๋“ฑ์€ ํ•˜์œ„ ๋„๋ฉ”์ธ์œผ๋กœ ์ƒ๊ฐํ•ด ๋ณผ ์ˆ˜ ์žˆ๋‹ค. ์ด์ฒ˜๋Ÿผ ๋น„์ฆˆ๋‹ˆ์Šค ๋„๋ฉ”์ธ์„ ๋จผ์ € ์ƒ๊ฐํ•ด๋ณด๊ณ  ์ดํ•ด๋ฅผ ํ•˜๋ฉฐ ์„ค๊ณ„ํ•˜๋Š” ๊ฒƒ์ด DDD ์„ค๊ณ„์ด๋‹ค. ํŠน์ง• ๋„๋ฉ”์ธ ๋ชจ๋ธ๊ณผ ๋กœ์ง์— ์ดˆ์ ์„ ๋‘”๋‹ค. ๋†’์€ ์‘์ง‘๋„์™€ ๋‚ฎ์€ ๊ฒฐํ•ฉ๋„๋ฅผ ์ง€ํ–ฅํ•˜๋ฏ€๋กœ ์œ ์ง€๋ณด์ˆ˜์™€ ํ™•์žฅ์„ฑ์ด ์šฉ์ดํ•˜๋‹ค ์†Œํ”„ํŠธ์›จ์–ด์˜ ๋ณต์žก์„ฑ์„ ์ตœ์†Œํ™”ํ•˜๋Š” ๊ฒƒ์„ ๋ชฉ์ ์œผ๋กœ ํ•œ๋‹ค SQL ์ค‘์‹ฌ ์„ค๊ณ„ ๊ฐœ๋… SQL ์ค‘์‹ฌ ์„ค๊ณ„๋Š” ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๊ตฌ์กฐ์™€ ๋ฐ์ดํ„ฐ ํ๋ฆ„์„ ์ค‘์‹ฌ์œผ๋กœ ํ•˜๋Š”..

๐Ÿ’ป Programming/Spring

[Spring] Entity, DTO, VO ์˜ ๊ฐœ๋…๊ณผ ํŠน์ง•

12๋ฒˆ์งธ ๊ณผ์ œ Entity - ์‹ค์ œ DB์˜ ํ…Œ์ด๋ธ”๊ณผ ๋งคํ•‘๋˜๋Š” ๊ฐ์ฒด - id๋ฅผ ํ†ตํ•ด ๊ฐ๊ฐ์˜ Entity๋ฅผ ๊ตฌ๋ถ„ํ•œ๋‹ค - ๋กœ์ง์„ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋‹ค import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; @Entity public class User { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String username; private String email; public User() { } public User..

๐Ÿ’ป Programming/Spring

[Spring] Service์™€ Repository ๊ตฌํ˜„ํ•˜๊ธฐ & 3๊ณ„์ธต ์—ฐ๊ฒฐ

10๋ฒˆ์งธ ๊ณผ์ œ 1. ์Šคํ”„๋ง ๋นˆ(Spring Bean) ๋“ฑ๋ก ๋ฐฉ๋ฒ• ์Šคํ”„๋ง IoC ์ปจํ…Œ์ด๋„ˆ๋Š” ํด๋ž˜์Šค์—์„œ ์–ด๋…ธํ…Œ์ด์…˜์„ ๋ณด๊ณ  ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค๋ฅผ ๋งŒ๋“ ๋‹ค. ์ด๋Ÿฌํ•œ ์ธ์Šคํ„ด์Šค๋ฅผ ๋นˆ(Bean)์ด๋ผ๊ณ  ํ•œ๋‹ค. ์Šคํ”„๋ง ๋นˆ์„ ๋“ฑ๋กํ•˜๋Š” ๋ฐฉ๋ฒ•์—๋Š” 2๊ฐ€์ง€๊ฐ€ ์žˆ๋‹ค. ์ฒซ ๋ฒˆ์งธ, @Configuration + @Bean ์กฐํ•ฉ ๋‘ ๋ฒˆ์งธ, @Component ์Šค์บ” ๋‘ ๋ฒˆ์งธ ๋ฐฉ๋ฒ•์ธ ์ปดํฌ๋„ŒํŠธ ์Šค์บ”์€ Service์™€ Repository ์— ์–ด๋…ธํ…Œ์ด์…˜์œผ๋กœ ์Šคํ”„๋ง ๋นˆ์„ ๋“ฑ๋กํ•ด์ฃผ๊ณ  Autowired๋ฅผ ์„ค์ •ํ•ด์ฃผ๋ฉด, ์ž๋™์œผ๋กœ ์˜์กด ๊ด€๊ณ„๋ฅผ ์ฃผ์ž…ํ•ด์ฃผ๋Š” ๋ฐฉ์‹์ด๋‹ค. ์Šคํ”„๋ง ๋นˆ์œผ๋กœ ๋“ฑ๋ก๋˜๋ฉด DI๋ฅผ ์š”์ฒญํ•  ์ˆ˜ ์žˆ๊ณ , DI๋ฅผ ์š”์ฒญํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ํ•„๋“œ ์œ„์— @Autowired๋ฅผ ์ ์–ด์ฃผ๋ฉด ๋œ๋‹ค. @Controller class Controller{ @Autowired Se..

๐Ÿ’ป Programming/Spring

[Spring] Controller ํด๋ž˜์Šค ๊ตฌํ˜„ํ•˜๊ธฐ

9๋ฒˆ์งธ ๊ณผ์ œ 1. start.spring.io ํ™œ์šฉํ•ด์„œ ์Šคํ”„๋ง ๋ถ€ํŠธ ํ”„๋กœ์ ํŠธ ์ƒ์„ฑํ•˜๊ธฐ start.spring.io์— ๋“ค์–ด๊ฐ€์„œ ์„ค์ • ํ›„ Generate ๋ฒ„ํŠผ์„ ๋ˆŒ๋Ÿฌ ํ”„๋กœ์ ํŠธ zip ํŒŒ์ผ์„ ๋‹ค์šด๋ฐ›๋Š”๋‹ค. ํŒŒ์ผ์˜ ์••์ถ•์„ ํ’€๊ณ  IntelliJ์—์„œ Open Project๋ฅผ ํ•ด์ฃผ๋ฉด ๋! 2. Controller ํด๋ž˜์Šค ๊ตฌํ˜„ํ•˜๊ธฐ - ์š”๊ตฌ์‚ฌํ•ญ : ์›น๋ธŒ๋ผ์šฐ์ €์— ‘localhost:8080/test’ ์ฃผ์†Œ๋ฅผ ์ž…๋ ฅํ•˜๋ฉด ์ฒจ๋ถ€์™€ ๊ฐ™์ด TEST ๊ธ€์ž ์ถœ๋ ฅํ•˜๊ธฐ controller ํด๋”๋ฅผ ๋งŒ๋“ค๊ณ  ๊ทธ ์•ˆ์— TestController ํŒŒ์ผ์„ ์ƒ์„ฑํ•˜์˜€๋‹ค. package com.example.demo.controller; import org.springframework.stereotype.Controller; import org.spring..

๐Ÿ’ป Programming/Spring

[Spring] ์Šคํ”„๋ง์˜ 3๊ณ„์ธต - Controller, Service, Repository

8๋ฒˆ์งธ ๊ณผ์ œ ์Šคํ”„๋ง์˜ ๊ฝƒ, 3๊ณ„์ธต [ Client -- Controller ๐Ÿ‘‰๐Ÿป Service ๐Ÿ‘‰๐Ÿป Repository -- DB ] Client์™€ DB ์‚ฌ์ด 3๊ณ„์ธต์˜ ์—ญํ• ์€ ๊ฐ๊ฐ ๋ฌด์—‡์ผ๊นŒ? Controller - ์—ญํ•  : ํด๋ผ์ด์–ธํŠธ์—์„œ ์š”์ฒญ๋œ ์š”๊ตฌ์‚ฌํ•ญ์„ ์ฒ˜๋ฆฌํ•˜๊ณ  ์ง€์ •๋œ ๋ทฐ์— ๋ชจ๋ธ ๊ฐ์ฒด๋ฅผ ๋„˜๊ฒจ์ค€๋‹ค. - ๊ธฐ๋Šฅ : ์š”๊ตฌ์‚ฌํ•ญ ์š”์ฒญ์— ๋”ฐ๋ผ ์–ด๋–ค ์ฒ˜๋ฆฌ๋ฅผ ํ• ์ง€ Service์— ๊ฒฐ์ •์„ ๋„˜๊ฒจ์ฃผ๊ณ  ์ „๋‹ฌ๋œ ๊ฐ’์„ ๋‹ค์‹œ ํด๋ผ์ด์–ธํŠธ์— ๋ฐ˜ํ™˜ํ•ด์ค€๋‹ค. Service - ์—ญํ•  : ์‹ค์ œ๋กœ ์ž‘์—…์ด ์ผ์–ด๋‚˜๋Š” ๋‹จ๊ณ„. ์ฆ‰, ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง์ด ์ฒ˜๋ฆฌ๋˜๋Š” ๊ณณ์ด๋‹ค - ๊ธฐ๋Šฅ : ์„œ๋ฒ„์—์„œ ์‚ฌ์šฉ์ž์˜ ์š”๊ตฌ์‚ฌํ•ญ์„ ์ฒ˜๋ฆฌํ•œ๋‹ค. DB ์ •๋ณด๊ฐ€ ํ•„์š”ํ•˜๋ฉด Repository์— ์š”์ฒญํ•œ๋‹ค. Repository - ์—ญํ•  : Service์—์„œ ๋‚˜์˜จ ๊ฒฐ๊ณผ๊ฐ’์„ ์ €์žฅํ•˜๊ฑฐ๋‚˜ ๊ธฐ์กด์˜ ์ €..

๐Ÿ’ป Programming/Spring

[Spring] DI์™€ IoC

7๋ฒˆ์งธ ๊ณผ์ œ 1. DI DI(Dependency Injection) : ์˜์กด์„ฑ ์ฃผ์ž… - ์Šคํ”„๋ง์€ ๊ธฐ๋ณธ์ ์œผ๋กœ ์˜์กด ๊ด€๊ณ„ ์ฃผ์ž… ํ”„๋ ˆ์ž„์›Œํฌ์ด๋‹ค. - ๊ฐ์ฒด ๊ฐ„ ์˜์กด๊ด€๊ณ„๋ฅผ ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ๊ฐœ๋ฐœ์ž๊ฐ€ ์ •์˜ํ•œ Bean ๋“ฑ๋ก ์ •๋ณด๋ฅผ ๋ฐ”ํƒ•์œผ๋กœ ์ž๋™์œผ๋กœ ์ฃผ์ž…ํ•ด์ฃผ๋Š” ๊ฒƒ์ด๋‹ค. - ๋‹จ๋‹จํ•˜๊ฒŒ ๊ฒฐํ•ฉ๋œ ํด๋ž˜์Šค๋“ค์„ ๋Š์Šจํ•œ ๊ฒฐํ•ฉ์œผ๋กœ ์—ฐ๊ฒฐ๋˜๊ฒŒ ํ•ด์ค€๋‹ค. DI ๋ฐฉ๋ฒ• 1) ํ•„๋“œ ์ฃผ์ž… : ๊ฐ์ฒด๋ฅผ ํด๋ž˜์Šค์— ํ•„๋“œ๋กœ ์„ ์–ธํ•˜๊ณ  @Autowired๋ฅผ ๋ถ™์ธ๋‹ค 2) ์ƒ์„ฑ์ž ์ฃผ์ž… : ์ƒ์„ฑ์ž์— @Autowired๋ฅผ ๋ถ™์ธ๋‹ค (๊ฐ€์žฅ ๊ถŒ์žฅ!) 3) Setter ์ฃผ์ž… : Setter ๋ฉ”์„œ๋“œ์— @Autowired๋ฅผ ๋ถ™์ธ๋‹ค 2. IoC IoC(Inversion of Control) : ์ œ์–ด์˜ ์—ญ์ „ - ์ง์ ‘ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๊ณ  ์ง์ ‘ DI ์ž‘์—…์„ ์ง„ํ–‰ํ•˜์ง€ ์•Š๊ณ  ์Šคํ”„..

๐Ÿ’ป Programming/Spring

[Spring] ์Šคํ”„๋ง ์ž…๋ฌธ - ์ฝ”๋“œ๋กœ ๋ฐฐ์šฐ๋Š” ์Šคํ”„๋ง ๋ถ€ํŠธ, ์›น MVC, DB ์ ‘๊ทผ ๊ธฐ์ˆ 

๊น€์˜ํ•œ - ์Šคํ”„๋ง ์ž…๋ฌธ : ์ฝ”๋“œ๋กœ ๋ฐฐ์šฐ๋Š” ์Šคํ”„๋ง ๋ถ€ํŠธ, ์›น MVC, DB ์ ‘๊ทผ ๊ธฐ์ˆ  [๋ฌด๋ฃŒ] ์Šคํ”„๋ง ์ž…๋ฌธ - ์ฝ”๋“œ๋กœ ๋ฐฐ์šฐ๋Š” ์Šคํ”„๋ง ๋ถ€ํŠธ, ์›น MVC, DB ์ ‘๊ทผ ๊ธฐ์ˆ  - ์ธํ”„๋Ÿฐ | ๊ฐ•์˜ ์Šคํ”„๋ง ์ž…๋ฌธ์ž๊ฐ€ ์˜ˆ์ œ๋ฅผ ๋งŒ๋“ค์–ด๊ฐ€๋ฉด์„œ ์Šคํ”„๋ง ์›น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๊ฐœ๋ฐœ ์ „๋ฐ˜์„ ๋น ๋ฅด๊ฒŒ ํ•™์Šตํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค., ์Šคํ”„๋ง ํ•™์Šต ์ฒซ ๊ธธ์žก์ด! ๊ฐœ๋ฐœ ๊ณต๋ถ€์˜ ๊ธธ์„ ์žƒ์ง€ ์•Š๋„๋ก ๋„์™€๋“œ๋ฆฝ๋‹ˆ๋‹ค. ๐Ÿ“ฃ ํ™•์ธํ•ด์ฃผ์„ธ www.inflearn.com ์‹œ์ž‘ 9์›” 4์ผ ์ˆ˜๊ฐ• ์‹œ์ž‘ - ๋ชฉํ‘œ๋Š” 2์ฃผ ์ด๋‚ด ์ˆ˜๊ฐ• ์™„๋ฃŒํ•˜๊ธฐ ๋ณต์Šตํ•  ๊ฒธ ์Šคํ”„๋ง ์ž…๋ฌธ ๊ฐ•์˜๋ถ€ํ„ฐ ๋‹ค์‹œ ๊ณต๋ถ€ํ•ด๋ณด๊ธฐ๋กœ ํ–ˆ๋‹ค. ์ด๋ฒˆ ๊ฐ•์˜ ์ˆ˜๊ฐ• ๋๋‚˜๋ฉด ๋กœ๋“œ๋งต ์ˆœ์„œ๋Œ€๋กœ ์ˆ˜๊ฐ•ํ•  ์˜ˆ์ •์ด๋‹ค. ๊ทธ๋™์•ˆ ์ƒ๊ฐ๋งŒ ํ–ˆ๋˜ ์•„์ด๋””์–ด๋ฅผ ๊ฐœ์ธ ํ”„๋กœ์ ํŠธ๋ผ๋„ ๊ตฌํ˜„ํ•ด๋ณด๊ณ  ์‹ถ๋‹ค. ๋ฐฐํฌ๊นŒ์ง€ ํ•˜๋Š”๊ฒŒ ์˜ฌํ•ด ๋ชฉํ‘œ! Section 1. ํ”„๋กœ์ ํŠธ ํ™˜๊ฒฝ์„ค์ • ..

๐Ÿ’ป Programming/Spring

[Spring] Q&A ๊ฒŒ์‹œํŒ ๋งŒ๋“ค๊ธฐ (1) - DB ๋ฐ DTO, DAO, Mapper

Q&A ๊ฒŒ์‹œํŒ ๋งŒ๋“ค๊ธฐ 1. DB - QNA ํ…Œ์ด๋ธ” ์ƒ์„ฑ CREATE TABLE QNA ( NUM NUMBER CONSTRAINT QNA_NUM_PK PRIMARY KEY, TITLE VARCHAR2(100) NOT NULL, CONTENTS VARCHAR2(4000), WRITER VARCHAR2(100) NOT NULL, REGDATE DATE, HIT NUMBER, REF NUMBER, STEP NUMBER, DEPTH NUMBER ); - ์ƒ์„ฑ๋œ QNA ํ…Œ์ด๋ธ” - SEQUENCE ์ƒ์„ฑ : num์— ์‚ฌ์šฉ CREATE SEQUENCE QNA_SEQ INCREMENT BY 1 START WITH 1; 2. ํŒŒ์ผ ์ถ”๊ฐ€ QnaDTO class ์ƒ์„ฑ QnaDAO class ์ƒ์„ฑ qnaMapper.xml ์ƒ์„ฑ ..

soozkim
'๐Ÿ’ป Programming/Spring' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๊ธ€ ๋ชฉ๋ก