본문 바로가기

Code Analysis/daangn-market-used-trading

[#33] 회원 엔티티 및 DTO 관련 설정 변경, [#25] 데이터베이스 이중화 적용

https://github.com/f-lab-edu/daangn-market-used-trading/pull/34/files/c6b9b5c1e8bce74f3fe4211d825a81a9a41b8aee

 

[#33] 회원 엔티티 및 DTO 관련 설정 변경 by ssibongee · Pull Request #34 · f-lab-edu/daangn-market-used-trading

This repository has been archived by the owner. It is now read-only. Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull re

github.com

 

 

=====

 

@RequestBody 의 작동 방식

https://kim-jong-hyun.tistory.com/60

 

[Spring] - @RequestBody 어노테이션의 동작방식

Spring으로 Rest Api를 개발할때 클라이언트와 데이터를 JSON으로 주고받고 하는데 이때 사용되는 어노테이션중 하나가 @RequestBody이다. 클라이언트측에서 요청데이터를 body에 담고 content-type을 applicat

kim-jong-hyun.tistory.com

setter로 값 넣는게 아니다. ( 맨 마지막 줄에 나옴 )

 

요약하자면 getter 또는 setter 메서드의 'get', 'set' 부분을 때어내고 앞의 영문을 소문자로 바꿔서 body에 들어온
key와 같을때 매핑한다고 한다

 

By default Jackson maps the fields of a JSON object to fields in a Java object by matching the names of the JSON field to the getter and setter methods in the Java object.

 

Jackson 기본 설정 : getter, setter 이름으로 json필드, 엔티티필드 파싱

=====

 

https://github.com/f-lab-edu/daangn-market-used-trading/pull/34/files/c6b9b5c1e8bce74f3fe4211d825a81a9a41b8aee#diff-01ed06cfc7519a9e8b99d0719ed1f85ff015aeed3bb5ffb5cef7557702605376R9

 

이해가 안됨.

-> 220906 이해는 됨.

 

=====

 

 

[#25] 데이터베이스 이중화 적용

 

마지막 줄 : 조회쿼리는 slaveDB에서 처리하게 하자.

 

 

[#25] MASTER와 SLAVE 데이터베이스에 대한 타입을 상수로 정의하는 클래스 생성

 

enum 한번씩 읽어보자. type을 선언 후 들어오는 값 "MASTER", "SLAVE" 를 생성자에 넣고 getType으로 가져올 수 있다.

 

 

 

 

# Transaction이 readOnly인지 여부에 따라서 데이터베이스를 선택할 수 있는 RoutingDataSource 클래스 생성

AbstractRoutingDataSource 상속 후 determineCurrentLookupKey 메서드 오버라이드. Transaction이 readOnly 면 SLAVE DB 선택.

 

 

 

 

# [#25] MASTER와 SLAVE DataSource 설정 및 RoutingDataSource를 적용한 LazyConnectionDataSourceProxy를 DataSource로 사용하도록 설정

 

https://github.com/f-lab-edu/daangn-market-used-trading/pull/35/commits/5b1a58c80d0c6e355ea2866139acfbf0dd1e0080

 

[#25] 데이터베이스 이중화 적용 by ssibongee · Pull Request #35 · f-lab-edu/daangn-market-used-trading

This repository has been archived by the owner. It is now read-only. Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull re

github.com

 

 

https://github.com/f-lab-edu/daangn-market-used-trading/pull/35/commits/73cc9c1c148b3264b981c851bb6392d190f11b11

이거부터 3개가 데이터베이스 이중화 적용

 

220906 이해함

 

=====

DTO 기본생성자, getter, setter

 

https://github.com/f-lab-edu/daangn-market-used-trading/pull/35/commits/9a9d29d260203ff75e067e682383dd77e58080bb

 

[#25] 데이터베이스 이중화 적용 by ssibongee · Pull Request #35 · f-lab-edu/daangn-market-used-trading

This repository has been archived by the owner. It is now read-only. Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull re

github.com

 

https://velog.io/@conatuseus/RequestBody%EC%97%90-%EC%99%9C-%EA%B8%B0%EB%B3%B8-%EC%83%9D%EC%84%B1%EC%9E%90%EB%8A%94-%ED%95%84%EC%9A%94%ED%95%98%EA%B3%A0-Setter%EB%8A%94-%ED%95%84%EC%9A%94-%EC%97%86%EC%9D%84%EA%B9%8C-3-idnrafiw

 

@RequestBody에 왜 기본 생성자는 필요하고, Setter는 필요 없을까? #3

이전 글에서는 RestController에서 @RequestBody 바인딩을 Jackson 라이브러리의 ObjectMapper가 하는 것을 확인했습니다.그리고 RequestBody를 생성할 때, DTO가 Property기반이 아니거나 Delegate를 한 상태가 아니라

velog.io

이 글 다 읽어보기.

 

# 결론

  • @RestController에서 @RequestBody의 바인딩은 Jackson라이브러리의 ObjectMapper가 해준다.
  • (JacksonMessageConverter 는 ObjectMapper) 
  • ObjectMapper는 @RequestBody가 생성을 위임한 경우가 아니라면 기본 생성자로 생성한다.
  • 따라서 위 상황이 아니라면 기본 생성자는 꼭 필요하다. (@NoArgsConstructor가 필요한 이유)
  • ObjectMapper는 Setter와 Getter로 DTO의 필드를 가져온다. (set, get 빼고 필드명 같은 거 가져오는 방식)
  • 그리고 setter를 사용하는 것이 아니라 reflection을 사용해서 필드 값들을 넣어준다.
  • 따라서 DTO에 사용할 필요가 거의 없는 Setter보다 프로덕션 코드에 사용되거나 테스트에서 필요한 Getter를 넣는게 좋다. (쉽게 말해, Setter 또는 Getter 둘 중 하나만 있으면 되는데 Getter를 넣자!)

 

Recent Posts
Popular Posts
Recent Comments