Skip to content

Nangman-corp/Spring-Style-Guide

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

18 Commits
ย 
ย 
ย 
ย 

Repository files navigation

Spring Style Guide

๐Ÿ’ก ๋ณธ๋ฌธ์€ ๋‚ญ๋งŒ(Nangman)์˜ Spring ์Šคํƒ€์ผ ๊ฐ€์ด๋“œ ์ž…๋‹ˆ๋‹ค.

Java code style์€ ์—ฌ๊ธฐ๋ฅผ ์ฐธ๊ณ ํ•ด์ฃผ์„ธ์š”.

๋ชฉ์ฐจ



1. ์†Œ๊ฐœ(Intro)


๋ณธ ๋ฌธ์„œ๋Š” Nangman์˜ Spring ์Šคํƒ€์ผ์„ ์„ค๋ช…ํ•ฉ๋‹ˆ๋‹ค.

1.1 ์šฉ์–ด(term)


  1. ๋กœ์ง(Logic) ์šฉ์–ด๋Š” ์•„๋ž˜๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.
    • ๋ฉ”์†Œ๋“œ(Method)
    • ํด๋ž˜์Šค(Class)
    • ์ธํ„ฐํŽ˜์ด์Šค(Interface)
  2. ๋„๋ฉ”์ธ(Domain) ์šฉ์–ด๋Š” ์•„๋ž˜๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.
    • ํ”„๋กœ๊ทธ๋žจ ์ƒ ํ•ด๊ฒฐํ•˜๊ณ ์ž ํ•˜๋Š” ๋ฌธ์ œ ํ˜น์€ ๊ธฐ๋Šฅ ์˜์—ญ
๐Ÿ’ก ๋ณธ๋ฌธ์—์„œ ์‚ฌ์šฉํ•˜์ง„ ์•Š์œผ๋‚˜, ๋„คํŠธ์›Œํฌ ์ƒ ์‹๋ณ„๊ฐ€๋Šฅํ•œ ํ˜ธ์ŠคํŠธ์˜์—ญ์„ ์˜๋ฏธํ•˜๋Š” Domain Name๋„ ์œ„ ๋‹จ์–ด๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
  1. ์„œ๋น„์Šค(Service) ์šฉ์–ด๋Š” ์•„๋ž˜๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.
    • ๋น„์ฆˆ๋‹ˆ์Šค ์ƒ ํ•„์š”ํ•œ ๊ธฐ๋Šฅ์„ ์ˆ˜ํ–‰ํ•˜๋Š” ์˜์—ญ ํ˜น์€ ๋กœ์ง
  2. ์ปจํŠธ๋กค๋Ÿฌ(Controller) ์šฉ์–ด๋Š” ์•„๋ž˜๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.
    • API ์—”๋“œํฌ์ธํŠธ๋ฅผ ์ •์˜ํ•˜๊ณ , ์ œ๊ณตํ•˜๋Š” ์˜์—ญ ํ˜น์€ ๋กœ์ง
  3. ์—”ํ‹ฐํ‹ฐ(Entity) ์šฉ์–ด๋Š” ์•„๋ž˜๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.
    • ๋ฐ์ดํ„ฐ ๋ฒ ์ด์Šค์˜ ์—ด๊ณผ ํ–‰์„ ๋‹ค๋ฃฐ ์ˆ˜ ์žˆ๋Š” ์˜์—ญ ํ˜น์€ ๋กœ์ง
  4. ๋ฐ์ดํ„ฐ ์ „์†ก ๊ฐ์ฒด(Data Transfer Object) ์šฉ์–ด๋Š” ์•„๋ž˜๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.
    • DTO๋ผ๊ณ  ํ•˜๋ฉฐ ๋ฐ์ดํ„ฐ ์ „์†ก๋งŒ์„ ์œ„ํ•ด ์‚ฌ์šฉํ•˜๋Š” ๊ฐ์ฒด
  5. ์ €์žฅ์†Œ(Repository) ์šฉ์–ด๋Š” ์•„๋ž˜๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.
    • ๋ฐ์ดํ„ฐ ๋ฒ ์ด์Šค์— ์ ‘๊ทผํ•˜๋Š” ๋ฉ”์„œ๋“œ ํ˜น์€ ์ธํ„ฐํŽ˜์ด์Šค๊ฐ€ ์ •์˜๋œ ์˜์—ญ
  6. ๊ตฌ์„ฑ(Config)
    • ๋™์ž‘์„ ์ˆ˜ํ–‰ํ•˜๋Š”๋ฐ ํ•„์š”ํ•œ ์„ค์ •ํŒŒ์ผ ์˜์—ญ ํ˜น์€ ๋กœ์ง
  7. ํƒ€์ž…(Type) ์šฉ์–ด๋Š” ์•„๋ž˜๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.
    • Enum Class๋กœ ์ •์˜๋œ Type ํด๋ž˜์Šค
    • Type์„ DB์— ์ €์žฅํ•  ๋•Œ๋Š” String์œผ๋กœ ์ €์žฅํ•˜๊ณ , ํด๋ž˜์Šค๋‚˜ ์ปฌ๋Ÿผ ๋ช… ๋˜ํ•œ ์ ‘๋ฏธ์‚ฌ๋กœ Type์„ ๋ถ™์ž…๋‹ˆ๋‹ค.
    • Response์˜ ๊ฒฝ์šฐ, Type์€ String ํ˜•์‹์œผ๋กœ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
  8. ์ƒํƒœ(State)
    • Enum Class๋กœ ์ •์˜๋œ State ํด๋ž˜์Šค
    • State๋Š” Integer๋กœ ๊ตฌ์„ฑ๋œ state number์™€, String์œผ๋กœ ๊ตฌ์„ฑ๋œ Description์„ ํ•ญ์ƒ ๊ฐ–์Šต๋‹ˆ๋‹ค.
    • State๋ฅผ DB์— ์ €์žฅํ•  ๋•Œ๋Š” ์ƒ์ˆ˜๋กœ ์ €์žฅํ•˜๊ณ , ํด๋ž˜์Šค๋‚˜ ์ปฌ๋Ÿผ ๋ช… ๋˜ํ•œ ์ ‘๋ฏธ์‚ฌ๋กœ State๋ฅผ ๋ถ™์ž…๋‹ˆ๋‹ค.
    • Response์˜ ๊ฒฝ์šฐ state๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ Integer Type์œผ๋กœ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
    • ์ƒํ™ฉ์— ๋”ฐ๋ผ FrontEnd ์ธก์—์„œ State์— ๋Œ€ํ•œ ์„ค๋ช…(String)์ด ํ•„์š”ํ•œ ๊ฒฝ์šฐ State์— ๋Œ€ํ•œ ์„ค๋ช…์„ ํ•จ๊ป˜ ๋ฐ˜ํ™˜ํ•˜๋ฉฐ, ์ด ๋•Œ JSON Field ์ด๋ฆ„์€ ~~StateDescription์˜ ํ˜•์‹์„ ๊ฐ€์ง‘๋‹ˆ๋‹ค.
  9. ๊ฒ€์ฆ(Validation) ์šฉ์–ด๋Š” ์•„๋ž˜๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.
    • ์œ ํšจ์„ฑ์„ ๊ฒ€์ฆํ•˜๋Š” ์˜์—ญ ํ˜น์€ ๋กœ์ง
  10. ์ฃผ์„ ํ…œํ”Œ๋ฆฟ(Comment(s) Template) ์šฉ์–ด๋Š” ์•„๋ž˜๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.
    • ํด๋ž˜์Šค ๋ฐ”๋กœ ์œ„ ์ •์˜๋˜๋Š” ์ฝ”๋“œ ์ž‘์„ฑ์ž์˜ ๊ธฐ๋ณธ์ •๋ณด ์–‘์‹

2. ํŒจํ‚ค์ง€ ํฌ๋งท(Package format)


๐Ÿ’ก Nangman์€ ๋„๋ฉ”์ธ ๋ณ„๋กœ ํŒจํ‚ค์ง€๋ฅผ ๊ตฌ๋ถ„ํ•ฉ๋‹ˆ๋‹ค.

2.1 ๋„๋ฉ”์ธ ํŒจํ‚ค์ง€(domain package)


  • ์ด๋ฆ„์— ๋Œ€๋ฌธ์ž๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
๐Ÿ’ก ๋„๋ฉ”์ธ(domain)์€ API ๊ฒฝ๋กœ ์ค‘ ํ•˜๋‚˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ตฌ๋ถ„ ์ง“์Šต๋‹ˆ๋‹ค.
  • ๋„๋ฉ”์ธ ํŒจํ‚ค์ง€๋Š” ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๋„๋ฉ”์ธ ํŒจํ‚ค์ง€๋ฅผ ํฌํ•จํ•˜๋ฉฐ ํ•ด๋‹น ํŒจํ‚ค์ง€์˜ ๊ตฌ์„ฑ์€ ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.
    • ์ปจํŠธ๋กค๋Ÿฌ ํŒจํ‚ค์ง€(controller package) : ์ปจํŠธ๋กค๋Ÿฌ ๋กœ์ง์„ ํฌํ•จ
    • ์„œ๋น„์Šค ํŒจํ‚ค์ง€(service package) : ์„œ๋น„์Šค ๋กœ์ง์„ ํฌํ•จ
    • DTO ํŒจํ‚ค์ง€(dto package) : DTO ๋กœ์ง์„ ํฌํ•จ
    • ์—”ํ‹ฐํ‹ฐ ํŒจํ‚ค์ง€(entity package) : ์—”ํ‹ฐํ‹ฐ ๋กœ์ง๊ณผ ์ €์žฅ์†Œ ๋กœ์ง์„ ํฌํ•จ

2.2 ๊ธ€๋กœ๋ฒŒ ํŒจํ‚ค์ง€(global package)


  • ์ด๋ฆ„์— ๋Œ€๋ฌธ์ž๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
  • ๊ธ€๋กœ๋ฒŒ : ์–ด๋Š ํ•œ ๋„๋ฉ”์ธ์— ์†ํ•˜์ง€ ์•Š๊ณ , ๋„๋ฉ”์ธ ๋‚ด์—์„œ ๊ด‘๋ฒ”์œ„ํ•˜๊ฒŒ ์‚ฌ์šฉํ•˜๋Š” ๋กœ์ง์˜ ๊ตฌ๋ถ„
  • ๊ธ€๋กœ๋ฒŒ ํŒจํ‚ค์ง€๋Š” ์•„๋ž˜ ํŒจํ‚ค์ง€๋ฅผ ํฌํ•จํ•ฉ๋‹ˆ๋‹ค.
    • ๊ตฌ์„ฑ(config) : ๊ตฌ์„ฑ ๋กœ์ง์„ ํฌํ•จ
    • ์˜ˆ์™ธ(exception) : ์˜ˆ์™ธ์ฒ˜๋ฆฌ ๋กœ์ง์„ ํฌํ•จ
    • ํƒ€์ž…(type) : ํƒ€์ž… ๋กœ์ง์„ ํฌํ•จ
    • ์œ ํ‹ธ(util) : ์ด๋กœ์šด ๊ธฐ๋Šฅ์ธ ์œ ํ‹ธ ๋กœ์ง์„ ํฌํ•จ

2.3 ์ธํ”„๋ผ ํŒจํ‚ค์ง€(Infra package)


  • ์ด๋ฆ„์— ๋Œ€๋ฌธ์ž๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
  • ์ธํ”„๋ผ : ์™ธ๋ถ€ API๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋กœ์ง์˜ ๊ตฌ๋ถ„
  • ์ธํ”„๋ผ ํŒจํ‚ค์ง€์˜ ๊ตฌ์„ฑ์€ ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.
    • ์„œ๋น„์Šค ํŒจํ‚ค์ง€(service package) : ์„œ๋น„์Šค ๋กœ์ง์„ ํฌํ•จ
    • DTO ํŒจํ‚ค์ง€(dto package) : DTO ๋กœ์ง์„ ํฌํ•จ
    • ์—”ํ‹ฐํ‹ฐ ํŒจํ‚ค์ง€(entity package) : ์—”ํ‹ฐํ‹ฐ ๋กœ์ง๊ณผ ์ €์žฅ์†Œ ๋กœ์ง์„ ํฌํ•จ
๐Ÿ’ก ๊ธ€๋กœ๋ฒŒ ํŒจํ‚ค์ง€๋Š” ๋„๋ฉ”์ธ ํŒจํ‚ค์ง€์™€ ๋‹ฎ์•„ ์žˆ์ง€๋งŒ, ์ปจํŠธ๋กค๋Ÿฌ๋ฅผ ์ƒ์„ฑํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ ์—ฌ๋Ÿฌ ํ”„๋กœ์ ํŠธ์— ์ค‘๋ณต ์ž‘์„ฑ๋  ๊ฐ€๋Šฅ์„ฑ์ด ๋†’์Šต๋‹ˆ๋‹ค.

2.4 ํŒจํ‚ค์ง€ ๊ตฌ์กฐ(package structure)

(์˜ˆ์‹œ)

โ”œโ”€โ”€ ๐Ÿ—‚com.nangman
   โ”œโ”€โ”€ domain
   |   โ”œโ”€โ”€ user
   |   |   โ”œโ”€โ”€ controller
   |   |   โ”œโ”€โ”€ dto
   |   |   โ”œโ”€โ”€ entity
   |   |   โ””โ”€โ”€ service
   |   |
   |   โ””โ”€โ”€ post
   |       โ”œโ”€โ”€ controller
   |       โ”œโ”€โ”€ dto
   |       โ”œโ”€โ”€ entity
   |       โ””โ”€โ”€ service
   |   
   โ””โ”€โ”€ global
       โ”œโ”€โ”€ config
       โ”œโ”€โ”€ exception
       โ”œโ”€โ”€ type
       โ””โ”€โ”€ util

3. ์ด๋ฆ„(Name)


๐Ÿ’ก ๋กœ์ง์— ๋Œ€ํ•œ ๋ช…๋ช… ๊ทœ์น™์„ ์„ค๋ช…ํ•ฉ๋‹ˆ๋‹ค.

3.1 ๋ณต์ˆ˜, ๋‹จ์ˆ˜ ๊ทœ์น™(Plural and singular rules)


  • ๋ณต์ˆ˜๋Š” ๋กœ์ง์˜ ๋ฐ˜ํ™˜ ๊ฒฐ๊ณผ๊ฐ€ ์—ฌ๋Ÿฌ ๊ฐœ์ธ ๊ฒฝ์šฐ๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.
  • ๋‹จ์ˆ˜๋Š” ๋กœ์ง์˜ ๋ฐ˜ํ™˜ ๊ฒฐ๊ณผ๊ฐ€ ํ•œ ๊ฐœ์ธ ๊ฒฝ์šฐ๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.
  • ๋ณต, ๋‹จ์ˆ˜์™€ ๊ด€๊ณ„์—†์ด ๋กœ์ง์„ ์„ค๋ช…ํ•  ์ˆ˜ ์žˆ๋Š” ์ •ํ™•ํ•œ ์ด๋ฆ„์„ ์ž‘์„ฑํ•ฉ๋‹ˆ๋‹ค.
(์˜ˆ์‹œ)

(ํ•˜๋‚˜์˜ ๊ฒŒ์‹œ๊ธ€)
(O) BoardDetailDto
(X) BoardDto

(๊ฒŒ์‹œ๊ธ€ ๋ชฉ๋ก)
(O) BoardListDto
  • ํ•˜๋‚˜์˜ ๋กœ์ง์—์„œ ๋ณต์ˆ˜์™€, ๋‹จ์ˆ˜ ๋กœ์ง์„ ๋™์‹œ์— ํฌํ•จํ•˜๋Š” ๊ฒฝ์šฐ ๋กœ์ง์ด ๊ตฌ๋ถ„๋˜๋Š” ๊ณณ์—์„œ ์ด๋ฅผ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค.
(๊ฒŒ์‹œ๊ธ€์„ ์ฒ˜๋ฆฌํ•˜๋Š” ์„œ๋น„์Šค ๋กœ์ง)
//BoardService.java

void findAllBoard() { //๊ฒŒ์‹œ๊ธ€ ๋ชฉ๋ก ๋ฐ˜ํ™˜
	......
}
void findDetailBoard() { //ํ•œ ๊ฐœ์˜ ๊ฒŒ์‹œ๊ธ€ ๋ฐ˜ํ™˜
	......
}

3.2 ์„œ๋น„์Šค(Service)


  • ์ผ๋ฐ˜์ ์œผ๋กœ ๋ช…์‚ฌ์™€ ๋ช…์‚ฌ๊ตฌ๋กœ ์ž‘์„ฑํ•ฉ๋‹ˆ๋‹ค.
  • Service ๋กœ์ง์˜ Class ์ด๋ฆ„์€ Service๋กœ ๋๋‚ฉ๋‹ˆ๋‹ค.
(์˜ˆ์‹œ) 
(O) CompanyService

3.3 ์ปจํŠธ๋กค๋Ÿฌ(Controller)


  • ์‘๋‹ต ํ˜•ํƒœ์— ๋”ฐ๋ผ ์ด๋ฆ„์„ ๊ตฌ๋ถ„ ์ง“์Šต๋‹ˆ๋‹ค.
  • ์ผ๋ฐ˜์ ์œผ๋กœ ๋ช…์‚ฌ์™€ ๋ช…์‚ฌ๊ตฌ๋กœ ์ž‘์„ฑํ•ฉ๋‹ˆ๋‹ค.
  • Controller ๋กœ์ง์˜ Class ์ด๋ฆ„์€ Controller๋กœ ๋๋‚ฉ๋‹ˆ๋‹ค.
  • RestController ๋กœ์ง์˜ Class ์ด๋ฆ„์€ ApiController๋กœ ๋๋‚ฉ๋‹ˆ๋‹ค.
(์˜ˆ์ œ)
(O) CompanyApiController
(O) UserController

3.4 ์—”ํ‹ฐํ‹ฐ(Entity)


  • ์ผ๋ฐ˜์ ์œผ๋กœ ๋ช…์‚ฌ์™€ ๋ช…์‚ฌ๊ตฌ๋กœ ์ž‘์„ฑํ•ฉ๋‹ˆ๋‹ค.
  • ๋ช…์‚ฌ ํ˜น์€ ๋ช…์‚ฌ ๊ตฌ๋กœ ์ด๋ค„์ง„ ์ ‘๋ฏธ์‚ฌ๋ฅผ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
(์˜ˆ์ œ)
(O) Company
(O) AwsS3

3.5 ๋ฐ์ดํ„ฐ ์ „์†ก ๊ฐ์ฒด(DTO)


  • ์ผ๋ฐ˜์ ์œผ๋กœ ๋ช…์‚ฌ์™€ ๋ช…์‚ฌ๊ตฌ๋กœ ์ž‘์„ฑํ•ฉ๋‹ˆ๋‹ค.

  • ์•„๋ž˜์™€ ๊ฐ™์€ ํ˜•ํƒœ๋ฅผ ๋”ฐ๋ฆ…๋‹ˆ๋‹ค.

    (Name)(Behavior)(Request/Response)(Dto)

  • Name : ์†ํ•œ ๋„๋ฉ”์ธ ํ˜น์€ ์ž˜ ์„ค๋ช…ํ•  ์ˆ˜ ์žˆ๋Š” ๋ช…์‚ฌ ๋ฐ ๋ช…์‚ฌ๊ตฌ

  • Behavior : ํ•ด๋‹น DTO๊ฐ€ ์ˆ˜ํ–‰ํ•˜๋Š” ๋™์ž‘

  • Request/Response : ํ•ด๋‹น DTO์˜ ์šฉ๋„

  • DTO ๋กœ์ง์˜ Class ์ด๋ฆ„์€ Dto๋กœ ๋๋‚ฉ๋‹ˆ๋‹ค.

(๋ชจ๋“  Company Behavior์— ๋Œ€ํ•˜์—ฌ ์‚ฌ์šฉํ•˜๋Š” DTO)
(O) CompanyRequestDto

(Boss ์ƒ์„ฑ์„ ์š”์ฒญํ•  ๋•Œ ์‚ฌ์šฉํ•˜๋Š” DTO)
(O) BossCreateRequestDto

(Boss ์ˆ˜์ •์„์— ๋Œ€ํ•œ ๊ฒฐ๊ณผ๋ฅผ ์‘๋‹ตํ•  ๋•Œ ์‚ฌ์šฉํ•˜๋Š” DTO)
(O) BossEditResponseDto

3.6 ์ €์žฅ์†Œ(Repository)


  • Repository ๋กœ์ง์˜ Class ์ด๋ฆ„์€ Entity์— ์ด๋ฆ„์œผ๋กœ ์‹œ์ž‘ํ•˜๊ณ , Repository๋กœ ๋๋‚ฉ๋‹ˆ๋‹ค.
  • ๋‹จ Repository ํ˜•ํƒœ์— ๋”ฐ๋ผ Custom ๋ฐ Impl ์ ‘๋ฏธ์‚ฌ๋ฅผ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
(Company Entity์— ๋Œ€ํ•˜์—ฌ ์‚ฌ์šฉํ•˜๋Š” Repository)
(O) CompanyRepository
(O) CompanyRepositoryCustom
(O) CompanyRepositoryImpl

3.7 ํƒ€์ž…(Type)


  • ์ผ๋ฐ˜์ ์œผ๋กœ ๋ช…์‚ฌ์™€ ๋ช…์‚ฌ๊ตฌ๋กœ ์ž‘์„ฑํ•ฉ๋‹ˆ๋‹ค.
  • Type ํด๋ž˜์Šค์˜ ๊ฒฝ์šฐ Type์œผ๋กœ ๋๋‚ฉ๋‹ˆ๋‹ค.
(์˜ˆ์‹œ)
(O) DomainNameType
(O) RoleType
(O) TargetType

3.8 ๊ตฌ์„ฑ(Config)


  • ์ผ๋ฐ˜์ ์œผ๋กœ ๋ช…์‚ฌ์™€ ๋ช…์‚ฌ๊ตฌ๋กœ ์ž‘์„ฑํ•ฉ๋‹ˆ๋‹ค.
  • Config ๋กœ์ง์˜ Class ์ด๋ฆ„์€ Config๋กœ ๋๋‚ฉ๋‹ˆ๋‹ค.
(์˜ˆ์‹œ)
(O) KakaoConfig
(O) AwsS3Config

4. ์ˆœ์„œ(Ordinary)


์–ด๋…ธํ…Œ์ด์…˜๊ณผ ๋ฉ”์†Œ๋“œ์˜ ์ˆœ์„œ๋ฅผ ๋‹ค๋ฃน๋‹ˆ๋‹ค.

4.1 ์–ด๋…ธํ…Œ์ด์…˜(Annotation)


  • ์–ด๋…ธํ…Œ์ด์…˜์˜ ๊ตฌ๋ถ„

    [Lombok]
    @Getter
    @Setter
    @NoArgsConstructor
    @AllArgsConstructor
    @RequiredArgsConstructor
    @Builder
    @Data
    @ToString
    @EqualsAndHashCode
    
    [JPA - class]
    @Entity
    @MappedSuperClass
    @EntityListener
    @Table
    
    [Jpa - Column]
    @Column
    @JoinColumn
    @NToM
    @Validation (NotNull, NotBlank, min ...)
    @Custom (Merge, etc...)
    @Pattern
    
    [Spring]
    @RestController / @Controller
    @RequestMapping
    @Service
    @Transactional
    @PathVariable
    @RequestPart
    @RequestParam
    @RequestBody
    @Valid
    
    =============
    
    
๐Ÿ’ก ๋ถ€๋“ฑํ˜ธ๊ฐ€ ํด์ˆ˜๋ก ํด๋ž˜์Šค์— ๊ฐ€๊น๊ฒŒ ๋“ฑ์žฅํ•ฉ๋‹ˆ๋‹ค.

4.1.1 Entity Class / Dto Class

  • @Entity > @Constructor > (@Builder) > @Getter ์ˆœ์„œ๋ฅผ ๊ฐ€์ง‘๋‹ˆ๋‹ค.
(์˜ˆ์‹œ)

@Getter
@Setter
@Builder
@AllArgsConstructor
@NoArgsConstructor
@Entity
public class UserEditRequestDto... {

}

4.1.2 Entity ๋‚ด Column

  • @Column > @GeneratedValue > @Id ์ˆœ์„œ๋ฅผ ๊ฐ€์ง‘๋‹ˆ๋‹ค.
(์˜ˆ์‹œ)

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "QNA_ID")
private Long id;
  • @Column > @JoinColumn > @NToM > @Pattern > @Validation > @Custom ์ˆœ์„œ๋ฅผ ๊ฐ€์ง‘๋‹ˆ๋‹ค.
  • NToM : oneTomany ๋“ฑ ํ…Œ์ด๋ธ” ์—ฐ๊ด€๊ด€๊ณ„์™€ ๊ด€๋ จ๋œ ์–ด๋…ธํ…Œ์ด์…˜์ž…๋‹ˆ๋‹ค.
  • Validation : Max, Min, NotNull ๋“ฑ ๊ฐ’ ์ฒ˜๋ฆฌ์™€ ๊ด€๋ จ๋œ ์–ด๋…ธํ…Œ์ด์…˜์ž…๋‹ˆ๋‹ค.
  • Custom : ์ง์ ‘ ๊ตฌํ˜„ํ•œ ์–ด๋…ธํ…Œ์ด์…˜์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.
(์˜ˆ์‹œ)

@Custom
@Validation
@Pattern
@NToM
@JoinColumn
@Column
private User user;

4.1.3 DTO ๋‚ด ํ•„๋“œ(Field in DTO)

  • @Pattern > @Validation > @Custom ์ˆœ์„œ๋ฅผ ๊ฐ€์ง‘๋‹ˆ๋‹ค.
(์˜ˆ์‹œ)

@Custom
@Validation
@Pattern
private String phoneNumber;

4.1.4 ์„œ๋น„์Šค ํด๋ž˜์Šค(Service class)

  • @Service > @Constructor > @Transactional ์ˆœ์„œ๋ฅผ ๊ฐ€์ง‘๋‹ˆ๋‹ค.
(์˜ˆ์‹œ)

@Transactional
@RequiredArgsConstructor
@Service
public class UserService{
	...
}

4.1.5 ์ปจํŠธ๋กค๋Ÿฌ ํด๋ž˜์Šค(Controller class)

  • @Controller > @Constructor > @Mapping ์ˆœ์„œ๋ฅผ ๊ฐ€์ง‘๋‹ˆ๋‹ค.
(์˜ˆ์‹œ)

@RequestMapping("/api/v1/post")
@RequiredArgsContructor
@RestController
public class UserApiController{
	...
}

4.1.6 ์ปจํŠธ๋กค๋Ÿฌ ๋งค๊ฐœ๋ณ€์ˆ˜(Controller parameter)

  • @Valid์™€ @RequestBody ๋“ฑ ํŒŒ๋ผ๋ฏธํ„ฐ์˜ ์–ด๋…ธํ…Œ์ด์…˜์˜ ์ค„๋ฐ”๊ฟˆ์€ ์ž์œจ์ ์œผ๋กœ ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค.
@PostMapping("/save")
public Long createUser(@Valid @RequestBody UserRequestDto requestDto){
	....
}

4.2 ๊ฒ€์ฆ ๋ฉ”์†Œ๋“œ(Validation Method)


  • ๋ฉ”์†Œ๋“œ A, B, C๊ฐ€ ์žˆ๊ณ  ์ด๋ฅผ ๊ฒ€์ฆํ•˜๋Š” ๋ฉ”์†Œ๋“œ๊ฐ€ ๊ฐ๊ฐ a, b, c๊ฐ€ ์žˆ์„ ๋•Œ ๊ฐ ๋ฉ”์†Œ๋“œ๊ฐ€ ์œ„์น˜ํ•˜๋Š” ์ˆœ์„œ๋Š” ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.
A
a
B
b
C
c
  • ๊ณต์šฉ์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ๋ฉ”์†Œ๋“œ ab์™€ abc๊ฐ€ ์ƒ์„ฑ๋œ ๊ฒฝ์šฐ ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.
A
a
ab
abc
B
b
C
c
๐Ÿ’ก ์ผ๋ฐ˜ ๋ฉ”์†Œ๋“œ์™€ ๊ฒ€์ฆ ๋ฉ”์†Œ๋“œ์˜ ์ˆœ์„œ๋ฅผ ์•„๋ž˜ ์˜ˆ์‹œ๋ฅผ ํ†ตํ•ด ์„ค๋ช…ํ•ฉ๋‹ˆ๋‹ค.
  • createUser์™€ editUser ๋ฉ”์†Œ๋“œ๊ฐ€ ์กด์žฌ
  • createUser๋ฅผ validationํ•˜๋Š” validationCreateUserRequest ์กด์žฌ
  • editUser๋ฅผ validationํ•˜๋Š” validationEditUserRequest ์กด์žฌ
  • createUser์™€ editUser ๋ฉ”์†Œ๋“œ์— ๊ณต์šฉ์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š” validationUserRequest ์กด์žฌ
๐Ÿ’ก ์œ„ ์ƒํ™ฉ์—์„œ ์•„๋ž˜์™€ ๊ฐ™์€ ์ˆœ์„œ๋ฅผ ๋”ฐ๋ฆ…๋‹ˆ๋‹ค.
public void createUser() {
	...
}
public void validationCreateUserRequest() {
	...
}
public void validationUserRequest() {
	...
}
public void editUser() {
	...
}
public void validationeditUserRequest() {
	...
}

์ผ๋ฐ˜ํ™” ๋œ ์ˆœ์„œ๋Š” ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.

5. ์ฃผ์„ ํ…œํ”Œ๋ฆฟ(Comments Templete)


  • class์œ„์— ์•„๋ž˜์™€ ๊ฐ™์€ ํ…œํ”Œ๋ฆฟ์„ ์ ์šฉํ•˜์—ฌ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
/**
* @date       : ${YEAR}-${MONTH}-${DAY}
* @author     : Minjun-KANG
*/

Contributors


๊ฐ•๋ฏผ์ค€๊ณผ ๋ฐ•ํ›ˆ์„ฑ์ด ๊ณต๋™ ํŽธ์ง‘ํ•˜์˜€์Šต๋‹ˆ๋‹ค.

Releases

No releases published

Packages

No packages published