๊ทธ๋ฃน์Šคํ„ฐ๋””์—์„œ '๋ฌด์ž‘์ • ๊ฒŒ์‹œํŒ ๋งŒ๋“ค๊ธฐ'ํ”„๋กœ์ ํŠธ๋ฅผ ์ง„ํ–‰์ค‘์ด๋‹ค. 
์‚ฌ์‹ค ๋‚˜๋Š” ์ž๋ฐ”๋กœ ํ† ์ดํ”„๋กœ์ ํŠธ๊นŒ์ง€๋Š” ํ•ด๋ณธ ๊ฒฝํ—˜์ด์žˆ์ง€๋งŒ Spring, SpringBoot๋Š” ์ „ํ˜€ ์‚ฌ์šฉ๊ฒฝํ—˜์ด ์—†์–ด 
ํ•˜๋‚˜ํ•˜๋‚˜ ๋ชจ๋‘ ๋ง‰๋ง‰ํ–ˆ์ง€๋งŒ ์Šคํ„ฐ๋”” ํŒ€์žฅ๋‹˜์˜ ์—„์ฒญ๋‚œ ์บ๋ฆฌ๋กœ ์กฐ๊ธˆ์”ฉ ์ดํ•ดํ•˜๊ณ  ์žˆ๋Š” ์ค‘์ด๋‹ค. ๊ฐ์‚ฌํ•จ๋‹ˆ๋‹ค.. 

๋ฌด์ž‘์ • JPA๋ฅผ ํ™œ์šฉํ•ด CRUD๊ฒŒ์‹œํŒ์„ ๋งŒ๋“ค๊ณ  ๊ธฐ๋Šฅ๊ตฌํ˜„๊นŒ์ง€ ํ•ด๋ณด์•˜๋‹ค !
๊นŒ๋จน์ง€ ์•Š๊ธฐ ์œ„ํ•ด ํฌ์ŠคํŒ…์„ ๋‚จ๊ธด๋‹ค. :) 

ํ”„๋กœ์ ํŠธ ํŒจํ‚ค์ง€ ๊ตฌ์กฐ

ํ”„๋กœ์ ํŠธ์˜ ํŒจํ‚ค์ง€๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

config


: Swagger์˜ ์„ค์ •ํŒŒ์ผ์ด ๋‹ด๊ธด ํŒจํ‚ค์ง€์ด๋‹ค.

controller


: ์›น MVC์˜ ์ปจํŠธ๋กค๋Ÿฌ ์—ญํ• ์„ ํ•˜๋Š” ํด๋ž˜์Šค๋“ค์ด ๋‹ด๊ฒจ์žˆ๋‹ค.

data > Request / Response


: Request(์š”์ฒญ), Response(์‘๋‹ต)DTO๋ฅผ ์ •์˜ํ•œ ํด๋ž˜์Šค์ด๋‹ค.

domain


: ํ…Œ์ด๋ธ”๊ณผ ๋งํฌ๋  ํด๋ž˜์Šค์ž„์„ ๋‚˜ํƒ€๋‚ธ๋‹ค.
Entity ํด๋ž˜์Šค ๋˜๋Š” ๊ฐ€์žฅ Coreํ•œ ํด๋ž˜์Šค๋ผ๊ณ  ๋ถ€๋ฅธ๋‹ค.

Entity ํด๋ž˜์Šค์™€ DTO ํด๋ž˜์Šค๋ฅผ ๋ถ„๋ฆฌํ•˜๋Š” ์ด์œ 

  • View Layer ์™€DB Layer์˜ ์—ญํ• ์„ ์ฒ ์ €ํ•˜๊ฒŒ ๋ถ„๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด์„œ
    ํ…Œ์ด๋ธ”๊ณผ ๋งคํ•‘๋˜๋Š” Entity ํด๋ž˜์Šค๊ฐ€ ๋ณ€๊ฒฝ๋˜๋ฉด ์—ฌ๋Ÿฌ ํด๋ž˜์Šค์— ์˜ํ–ฅ์„ ๋ผ์น˜๊ฒŒ ๋œ๋‹ค. ํ•˜์ง€๋งŒ View์™€ ํ†ต์‹ ํ•˜๋Š” DTO ํด๋ž˜์Šค(Request / Response ํด๋ž˜์Šค)๋Š” ์ž์ฃผ ๋ณ€๊ฒฝ๋˜๋ฏ€๋กœ ๋ถ„๋ฆฌํ•ด์•ผ ํ•œ๋‹ค.
  • Domain Model์„ ์•„๋ฌด๋ฆฌ ์ž˜ ์„ค๊ณ„ํ–ˆ๋‹ค๊ณ  ํ•ด๋„ ๊ฐ View ๋‚ด์—์„œ Domain Model์˜ getter๋งŒ์„ ์ด์šฉํ•ด์„œ ์›ํ•˜๋Š” ์ •๋ณด๋ฅผ ํ‘œ์‹œํ•˜๊ธฐ๊ฐ€ ์–ด๋ ค์šด ๊ฒฝ์šฐ๊ฐ€ ์ข…์ข… ์žˆ๋‹ค.
    ์ด๋Ÿฐ ๊ฒฝ์šฐ Domain Model ๋‚ด์— Presentation์„ ์œ„ํ•œ ํ•„๋“œ๋‚˜ ๋กœ์ง์„ ์ถ”๊ฐ€ํ•˜๊ฒŒ ๋˜๋Š”๋ฐ, ์ด๋Ÿฌํ•œ ๋ฐฉ์‹์ด ๋ชจ๋ธ๋ง์˜ ์ˆœ์ˆ˜์„ฑ์„ ๊นจ๊ณ  Domain Model ๊ฐ์ฒด๋ฅผ ๋ง๊ฐ€๋œจ๋ฆฌ๊ฒŒ ๋œ๋‹ค.
  • ๋˜ํ•œ Domain Model์„ ๋ณต์žกํ•˜๊ฒŒ ์กฐํ•ฉํ•œ ํ˜•ํƒœ์˜ Presentation ์š”๊ตฌ์‚ฌํ•ญ๋“ค์ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์— Domain Model์„ ์ง์ ‘ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์€ ์–ด๋ ต๋‹ค.
  • ์ฆ‰ DTO๋Š” Domain Model์„ ๋ณต์‚ฌํ•œ ํ˜•ํƒœ๋กœ, ๋‹ค์–‘ํ•œ Presentation Logic์„ ์ถ”๊ฐ€ํ•œ ์ •๋„๋กœ ์‚ฌ์šฉํ•˜๋ฉฐ Domain Model ๊ฐ์ฒด๋Š” Persistent๋งŒ์„ ์œ„ํ•ด์„œ ์‚ฌ์šฉํ•œ๋‹ค.

repository


: JPA์—์„œ์˜ DB Layer ์ ‘๊ทผ์ž๋ฅผ ์˜๋ฏธํ•œ๋‹ค.
์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ƒ์„ฑ ํ›„ JpaRepository<Entity ํด๋ž˜์Šค, PK ํƒ€์ž…>์„ ์ƒ์†ํ•˜๋ฉด ๊ธฐ๋ณธ์ ์ธ CRUD ๋ฉ”์†Œ๋“œ๊ฐ€ ์ž๋™์œผ๋กœ ์ƒ์„ฑ๋œ๋‹ค.
@Repository๋ฅผ ์ถ”๊ฐ€ํ•  ํ•„์š”๊ฐ€ ์—†๋‹ค.

sevice


: ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง์„ ์ˆ˜ํ–‰ํ•œ๋‹ค. Service์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๋‘๊ณ  ServiceImpl๋กœ ์ด๋ฅผ ๊ตฌํ˜„ํ•˜์˜€๋‹ค.
interface์™€ ๊ตฌํ˜„์ฒด๋กœ ๋‚˜๋ˆ„๋Š” ์ด์œ ๋Š” ์—ฌ๊ธฐ์— ๋ณด๋‹ค ์ž์„ธํžˆ ๋‚˜์™€์žˆ๋‹ค !

DB


Attribute(์†์„ฑ)๋Š” ์ด 5๊ฐœ์ด๋‹ค.

  • id (๊ธฐ๋ณธํ‚ค)
  • brand_name
  • category
  • land_number
  • road_number

healthInfo.java

package com.example.demo.domain;

import lombok.*;

import javax.persistence.*;
import java.util.List;

@NoArgsConstructor
@AllArgsConstructor
@ToString
@Builder
@Getter
@Entity
@Table(name = "health_info")
public class HealthInfo {

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

        private String brand_name;

        private String land_number;

        private String road_number;


        private String category;

        public void modifyHealthinfo(String brand_name,String land_number,String road_number, String category){
                this.brand_name=brand_name;
                this.land_number=land_number;
                this.road_number=road_number;
                this.category=category;
        }


}

๊ด€๋ จ ์–ด๋…ธ์ผ€์ด์…˜

  • @NoArgsConstructor: ํŒŒ๋ผ๋ฏธํ„ฐ๊ฐ€ ์—†๋Š” ๊ธฐ๋ณธ ์ƒ์„ฑ์ž ์ƒ์„ฑ
  • @AllArgsConstructor: ๋ชจ๋“  ํ•„๋“œ ๊ฐ’์„ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ๋ฐ›๋Š” ์ƒ์„ฑ์ž ์ƒ์„ฑ
  • @ToString: ํด๋ž˜์Šค์˜ ๋ณ€์ˆ˜๋“ค์„ ๊ธฐ๋ฐ˜์œผ๋กœ ToString ๋ฉ”์†Œ๋“œ ์ž๋™์œผ๋กœ ์™„์„ฑ
  • @Builder: ํ•ด๋‹น ํด๋ž˜์Šค์˜ ๊ฐ์ฒด ์ƒ์„ฑ์— BuliderํŒจํ„ด ์ ์šฉ์‹œ์ผœ์คŒ, ๋ชจ๋“  ๋ณ€์ˆ˜๋“ค์— ๋Œ€ํ•ด bulidํ•˜๊ธฐ ์›ํ•œ๋‹ค๋ฉด ํด๋ž˜์Šค์œ„์— @bulid ๋ถ™์ด๊ณ  ํŠน์ •๋ณ€์ˆ˜๋งŒ bulidํ•˜๊ธฐ ์›ํ•œ๋‹ค๋ฉด ์ƒ์„ฑ์ž๋ฅผ ์ž‘์„ฑํ•˜๊ณ  ๊ทธ ์œ„์— @bulid๋ฅผ ๋ถ™์ธ๋‹ค !
  • @Getter: ๋ชจ๋“  ๋ณ€์ˆ˜๋“ค์— Getter๋ฉ”์†Œ๋“œ ์ž๋™ ์ž‘์„ฑ
  • @Entity,@Table: ๊ฐ์ฒด์™€ ํ…Œ์ด๋ธ” ๋งคํ•‘

DTO

DTO(Data Transfer Object)๋ž€ ๊ณ„์ธต๊ฐ„ ๋ฐ์ดํ„ฐ ๊ตํ™˜์„ ์œ„ํ•ด ์‚ฌ์šฉํ•˜๋Š” ๊ฐ์ฒด(Java Beans)์ด๋‹ค.
์ •๋ฆฌํ•˜๋ฉด DTO๋Š” ํด๋ผ์ด์–ธํŠธ ์š”์ฒญ์— ํฌํ•จ๋œ ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ด์•„ ์„œ๋ฒ„ ์ธก์— ์ „๋‹ฌํ•˜๊ณ , ์„œ๋ฒ„ ์ธก์˜ ์‘๋‹ต ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ด์•„ ํด๋ผ์ด์–ธํŠธ์— ์ „๋‹ฌํ•˜๋Š” ๊ณ„์ธต๊ฐ„ ์ „๋‹ฌ์ž ์—ญํ• ์ด๋‹ค.
๊ณ„์ธต๊ฐ„ ๋ฐ์ดํ„ฐ ๊ตํ™˜์„ ์œ„ํ•œ ๊ฐ์ฒด(Java Beans)์ด๋‹ค.

DB์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์–ป์–ด Service๋‚˜ Controller ๋“ฑ์œผ๋กœ๋ถ€ํ„ฐ ๋ณด๋‚ผ ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ๊ฐ์ฒด๋ฅผ ๋งํ•œ๋‹ค.
์ฆ‰, DB์˜ ๋ฐ์ดํ„ฐ๊ฐ€ Presentation Logic Tier๋กœ ๋„˜์–ด์˜ค๊ฒŒ ๋  ๋•Œ๋Š” DTO์˜ ๋ชจ์Šต์œผ๋กœ ๋ฐ”๊ปด์„œ ์˜ค๊ณ ๊ฐ€๋Š” ๊ฒƒ์ด๋‹ค.
๋กœ์ง์„ ๊ฐ–๊ณ  ์žˆ์ง€ ์•Š๋Š” ์ˆœ์ˆ˜ํ•œ ๋ฐ์ดํ„ฐ ๊ฐ์ฒด์ด๋ฉฐ, getter/setter ๋ฉ”์„œ๋“œ๋งŒ์„ ๊ฐ–๋Š”๋‹ค.
ํ•˜์ง€๋งŒ DB์—์„œ ๊บผ๋‚ธ ๊ฐ’์„ ์ž„์˜๋กœ ๋ณ€๊ฒฝํ•  ํ•„์š”๊ฐ€ ์—†๊ธฐ ๋•Œ๋ฌธ์— DTOํด๋ž˜์Šค์—๋Š” setter๊ฐ€ ์—†๋‹ค. (๋Œ€์‹  ์ƒ์„ฑ์ž์—์„œ ๊ฐ’์„ ํ• ๋‹นํ•œ๋‹ค.)

  • Request์™€ Response์šฉ DTO๋Š” View๋ฅผ ์œ„ํ•œ ํด๋ž˜์Šค
  • ์ž์ฃผ ๋ณ€๊ฒฝ์ด ํ•„์š”ํ•œ ํด๋ž˜์Šค
  • Presentation Model
  • toEntity() ๋ฉ”์„œ๋“œ๋ฅผ ํ†ตํ•ด์„œ DTO์—์„œ ํ•„์š”ํ•œ ๋ถ€๋ถ„์„ ์ด์šฉํ•˜์—ฌ Entity๋กœ ๋งŒ๋“ ๋‹ค.
  • ๋˜ํ•œ Controller Layer์—์„œ Response DTO ํ˜•ํƒœ๋กœ Client์— ์ „๋‹ฌํ•œ๋‹ค.

HealthRequestDTO.java

package com.example.demo.data.request;

import com.example.demo.domain.HealthInfo;
import lombok.Getter;

@Getter
public class HealthRequestDTO {
    private Long id;
    private String brand_name;
    private String land_number;
    private String road_number;
    private String category;


    public static HealthInfo toEntity(HealthRequestDTO dto){
        return HealthInfo.builder()
                .id(dto.getId())
                .brand_name(dto.getBrand_name())
                .category(dto.getCategory())
                .land_number(dto.getLand_number())
                .road_number(dto.getRoad_number())
                .build();
    }

    
}

HealthResponseDTO.java

package com.example.demo.data.response;

import com.example.demo.domain.HealthInfo;
import lombok.Builder;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

@Builder
@Getter
@Setter
@ToString
public class HealthResponseDTO {

    private Long id;
    private String brand_name;
    private String land_number;
    private String road_number;
    private String category;

    public static HealthInfo toEntity(HealthResponseDTO healthResponseDTO){
        return HealthInfo.builder()
                .id(healthResponseDTO.getId())
                .brand_name(healthResponseDTO.getBrand_name())
                .land_number(healthResponseDTO.getLand_number())
                .road_number(healthResponseDTO.getRoad_number())
                .category(healthResponseDTO.getCategory())
                .build();

    }

}

Reference๐Ÿ’ก

0๊ฐœ์˜ ๋Œ“๊ธ€