[TIL] 220530

Moonยท2022๋…„ 5์›” 30์ผ

๐Ÿ’ฌ
Controller - ์ž๋™์‘๋‹ต๊ธฐ
Service - ์—…๋ฐ์ดํŠธ ์‚ฌ์šฉ
Repository - ์ง์ ‘ ์ฟผ๋ฆฌ ๋‚ ๋ฆผ

  • ์š”์ฒญ์„ ๋ฐ›์„ ๋•Œ, ์ •๋ณด๋ฅผ ๋ชฐ๊ณ  ๋‹ค๋‹ ๋•Œ DTO๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

@RequestBody : http์š”์ฒญ์˜ ๋ณธ๋ฌธ(body)์„ ๊ทธ๋Œ€๋กœ ์ „๋‹ฌํ•œ๋‹ค. json ๊ธฐ๋ฐ˜์˜ ๋ฉ”์‹œ์ง€๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์š”์ฒญ์—์„œ ์‚ฌ์šฉํ•œ๋‹ค.

@RequiredArgsConstructor
@Service
public class PersonService {

    private final PersonRepository personRepository;

    @Transactional
    public Long update(Long id, PersonRequestDto requestDto) {
        Person person = personRepository.findById(id).orElseThrow(
                () -> new IllegalArgumentException("์•„์ด๋””๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.")
        );
        person.update(requestDto);
        return id;
    }
}
    

@RequriedArgsConstructor : ์ดˆ๊ธฐํ™”๋˜์ง€ ์•Š์€ final ํ•„๋“œ์— ๋Œ€ํ•ด ์ƒ์„ฑ์ž๋ฅผ ์ƒ์„ฑํ•ด์ค€๋‹ค.

@Transactional : ๋ฉ”์†Œ๋“œ์˜ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜๋“œ์‹œ db์— ๋ฐ˜์˜ํ•ด์ค€๋‹ค.

์–ด๋…ธํ…Œ์ด์…˜์ด ๋„ˆ๋ฌด ๋งŽ์•„์„œ ํ—ท๊ฐˆ๋ฆฐ๋‹ค.๐Ÿ™


  • Repository๋ฅผ ๋งŒ๋“œ๋ ค๋ฉด ๋ญ๊ฐ€ ํ•„์š”ํ•œ์ง€ ์•Œ์•„์•ผ ํ•œ๋‹ค.
  • ๋ญ๊ฐ€ ํ•„์š”ํ•œ์ง€ ์•Œ๊ธฐ ์œ„ํ•ด์„œ๋Š” API๋ฅผ ์„ค๊ณ„ํ•ด์•ผ ํ•œ๋‹ค.

๐Ÿ‘‰ Repository๋ฅผ ๋งŒ๋“ค๊ธฐ ์ „์— API ์„ค๊ณ„ํ•˜๊ธฐ!

ํด๋ž˜์Šค๋ฅผ ์ƒ์† ๋ฐ›๋Š” ์ž์‹ ํด๋ž˜์Šค์—๊ฒŒ ๋งคํ•‘ ์ •๋ณด๋งŒ ์ œ๊ณตํ•˜๊ณ  ์‹ถ์„ ๋•Œ๋Š” @MappedSuperclass๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.
์ถ”์ƒํด๋ž˜์Šค์ด๊ธฐ ๋•Œ๋ฌธ์— ์ƒ์†์ด ๋˜์–ด์•ผ๋งŒ ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•˜๋‹ค. (new Timestamped() ๋ถˆ๊ฐ€๋Šฅ)




๋‚ด์ผ ๊ฐ•์˜ ๋” ๋“ฃ๊ณ  ๋‹ค์‹œ ์ •๋ฆฌํ•˜๊ธฐ!

profile
๋งค์ผ ์„ฑ์žฅํ•˜๋Š” ๊ฐœ๋ฐœ์ž ๋˜๊ธฐ๐Ÿ˜Š

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

comment-user-thumbnail
2022๋…„ 5์›” 31์ผ

ํ™งํŒ…!

๋‹ต๊ธ€ ๋‹ฌ๊ธฐ