1. spring pjt 초기화 생성 시 의존성 추가

Mapper는 생성 시 없어서 필요 시 추가
Spring 구동 시 자동생성 Annotation : @SpringBootApplication
dependencies { implementation 'org.springframework.boot:spring-boot-starter-validation' implementation 'org.springframework.boot:spring-boot-starter-web' compileOnly 'org.projectlombok:lombok' annotationProcessor 'org.projectlombok:lombok' testImplementation 'org.springframework.boot:spring-boot-starter-test' implementation 'org.mapstruct:mapstruct:1.4.2.Final' annotationProcessor 'org.mapstruct:mapstruct-processor:1.4.2.Final' }
2. 디렉토리 구조
- 생성해야 할 패키지 : controller(dto), service, mapper, entity, exception
- 클래스명 : -controller, dto(3개), 서비스 1개, mapper interface, globalexceptionadvice
- ~Service : 인터페이스
- ~ServiceImpl : 인터페이스 구현체(웹개발 한정, 자바에서는 안씀)
- : 컨트롤러의 도메인명(컨트롤러는 생략) + 메서드 타입 + Dto : ex. BorderPostDto
- 디렉토리 계층 구조
- 계층기반 패키지 구조(pachage-by-layer) : MVC + Layer 패턴 그대로
- 기능기반 패키지 구조(package-by-feature) : 도메인(엔티티) 단위

3. 클래스 생성
메인클래스 - 객체 클래스로 생성
controller : @RestController @Requestmapping(+다양한 Mapping)url 관문 , CRUD
- @RequestBody @PathValiable
- 유효성 검증 @Valid @Validated
- 서비스class, 매퍼class 생성자 주입
DTO : req → 컨트롤러 용 객체
- 컨트롤러 내 메서드 갯수 + 1 (post, patch, response)
Entity : Service 용 객체
Mapper Interface
- @Mapper(componentModel = "spring")
- @Mapping(source = "price", target = "price.value")
Service : @Service
Exception : @RestControllerAdvice @ExceptionHandler(@ResponseStatus())
- 반환 객체 새로 정의
'Coding > Back - Spring Framework' 카테고리의 다른 글
Spring Persistence(DataAccess) Layer : JPA 예제 2/3 #Day9 (0) | 2023.08.30 |
---|---|
Spring Persistence(DataAccess) Layer : JPA 개념 1/3 #Day9 (0) | 2023.08.28 |
Spring 예외처리 #Day7 (0) | 2023.08.24 |
Spring Business(Service) Layer #Day6 (0) | 2023.08.23 |
Spring DTO, Validation #Day5 (0) | 2023.08.22 |