DevKim

[refactor] ๊ฒŒ์‹œ๊ธ€๊ณผ ๋Œ“๊ธ€ ์—ฐ๊ด€๊ด€๊ณ„ ๋งคํ•‘ ๊ฐœ์„ ํ•˜๊ธฐ ๋ณธ๋ฌธ

Spring Boot

[refactor] ๊ฒŒ์‹œ๊ธ€๊ณผ ๋Œ“๊ธ€ ์—ฐ๊ด€๊ด€๊ณ„ ๋งคํ•‘ ๊ฐœ์„ ํ•˜๊ธฐ

on_doing 2021. 7. 16. 02:17
728x90

๐Ÿƒ ๊ฒŒ์‹œ๊ธ€,๋Œ“๊ธ€ ์—ฐ๊ด€๊ด€๊ณ„ ๋งคํ•‘ ๊ฐœ์„  ๐Ÿƒ

์ง€๋‚œ ๋ฒˆ์— ์—ฐ์Šต์‚ผ์•„ ๊ตฌํ˜„ํ–ˆ๋˜ ๊ฒŒ์‹œํŒ ํŽ˜์ด์ง€๋ฅผ ๊ฐœ์„ ํ•ด๋ณด๋ ค๊ณ ํ•œ๋‹ค.
๋งŒ์•ฝ ํ•˜๋‚˜์˜ ํŽ˜์ด์ง€์—์„œ ๋ชจ๋“ ๊ฒŒ ๋‹ค ์ง„ํ–‰์ด๋˜์—ˆ๋‹ค๋ฉด, ํฐ ๋ฌธ์ œ๋Š” ์•„๋‹ˆ์—ˆ๊ฒ ์ง€๋งŒ
Multi page์˜ ๊ฒŒ์‹œํŒ์ด๊ธฐ ๋•Œ๋ฌธ์— ํ™”๋ฉด์„ ๋„˜์–ด๊ฐˆ ๋•Œ,
ํ•ด๋‹น ๊ธ€์˜ id๊ฐ’์„ ์–ด๋–ป๊ฒŒ ๋„˜๊ฒจ๋ฐ›์„ ์ˆ˜ ์žˆ์„์ง€ ๊ณ ๋ฏผ์ด ์ฐธ ๋งŽ์ด ๋๋‹ค.

์š”์ฆ˜์—” SPA ๋ฐฉ์‹์„ ๋งŽ์ด ์‚ฌ์šฉํ•œ๋‹ค์ง€๋งŒ,
๊ทธ๋ ‡๋‹ค๊ณ ํ•ด์„œ ์ด๊ฑธ ๊ทธ๋ƒฅ ๋ฌด์‹œํ•˜๊ณ  ๋„˜์–ด๊ฐ€๋ฒ„๋ฆฌ๊ธฐ์—” ์ฐ์ฐํ•˜๊ธฐ๋„ํ•ด์„œ ์ฐ์ฐํ–ˆ๋˜ ๋ถ€๋ถ„์„ ํ•ด๊ฒฐํ•ด๋ณด๋ ค๊ณ ํ•œ๋‹ค.


๐Ÿ“Œ 1. ์ฒซ ๋ฒˆ์งธ ๋ฐฉ๋ฒ•

URL ํŒŒ์”ฝํ•ด์„œ id๊ฐ’ ์–ป์–ด์˜ค๊ธฐ + FK ์—ญํ• ์„ ํ•  ์ž„์˜์˜ Id๊ฐ’์„ ์ฒ˜์Œ๋ถ€ํ„ฐ Column์— ๋„ฃ์–ด๋ฒ„๋ฆฌ๊ธฐ

๋‹จ์ˆœํ•˜์ง€๋งŒ ์‚ฌ๋žŒ๋“ค์ด ๊ฐ€์žฅ ๋งŽ์ด ์“ฐ๋Š” ๋ฐฉ๋ฒ•์ธ ๊ฒƒ ๊ฐ™๋‹ค.
(JPA ์—ฐ๊ด€๊ด€๊ณ„ ๋งคํ•‘์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š์€ ๊ฒƒ๊ณผ ๊ฐ™๋‹ค.)

์˜ˆ๋ฅผ๋“ค์–ด, boardDetail?id=1 ์œผ๋กœ url์ด ๋„˜์–ด์˜จ๋‹ค๋ฉด,

Js์˜ location.search๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ =์„ ๊ธฐ์ค€์œผ๋กœ ํŒŒ์”ฝํ•˜์—ฌ id๊ฐ’์„ ๊ฐ€์ ธ์˜จ๋‹ค.

id = location.search.split('=')[1]

๊ทธ id๊ฐ’์„ ๊ฒŒ์‹œ๊ธ€์˜ id ์ปฌ๋Ÿผ ๊ฐ’์œผ๋กœ setter๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๋„ฃ์–ด์ค€๋‹ค.

์ผ๋‹จ ๋”ฐ๋กœ id๊ฐ’์„ Entity์— ์ถ”๊ฐ€ํ•ด์คฌ๋‹ค๋Š”๊ฒŒ ๋งˆ์Œ์— ๋“ค์ง€ ์•Š์•˜๊ณ ,setter๋กœ ๋„ฃ์–ด์คฌ๋‹ค๋Š” ๊ฒƒ๋„ ์ข‹์ง€ ์•Š์€ ๋ฐฉ๋ฒ•์ด๊ณ ,
๋ฌด์—‡๋ณด๋‹ค url ํŒŒ์”ฝํ•˜๋Š”๊ฑด.. ๋„ˆ๋ฌด ๋ฉ‹์ด ์—†๋‹ค..ใ…Ž


๐Ÿ“Œ 2. ๋‘๋ฒˆ์งธ ๋ฐฉ๋ฒ•

URL๋กœ id ํŒŒ์”ฝํ•ด์„œ id๊ฐ’ ๊ฐ€์ ธ์˜ค๊ธฐ + ๋Œ“๊ธ€ ์กฐํšŒ์‹œ id๊ฐ’ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ๋ฐ›์•„์„œ ํ•˜๋‚˜์”ฉ ๊บผ๋‚ด์„œ set์œผ๋กœ ์—ฐ๊ด€๊ด€๊ณ„ ๋งคํ•‘ํ•ด์ฃผ๊ธฐ

/** * ๋Œ“๊ธ€ ์กฐํšŒ */ @GetMapping("api/comment") public List<Comment> findComments(@RequestParam(value = "article_id") Long article_id, @AuthenticationPrincipal UserDetailsImpl userDetails) { List<Comment> commentList = commentRepository.findAllByArticle_idOrderByCreatedAtDesc(article_id); for (Comment comment : commentList) { comment.setNowUser(userDetails.getUsername()); } return commentList; }

์ง€๊ธˆ ์ฝ”๋“œ ๋ณด๋‹ˆ๊นŒ ์™œ์ด๋ ‡๊ฒŒ ํ•œ์ง€ ๋ชจ๋ฅด๊ฒ ๋‹ค..
์กธ๋ฉด์„œ ์ฝ”๋“œ ์งฐ๋‹ˆ..? ์ •์‹  ์ฐจ๋ฆฌ๊ณ  ์ฝ”๋“œ ์งœ์ž!!!


๐Ÿ“Œ 3. ๋งˆ์ง€๋ง‰ ๋ฐฉ๋ฒ•

ํ˜„์žฌ, ์ด ๋ฐฉ๋ฒ•์œผ๋กœ ์ฝ”๋“œ๋ฅผ ๊ฐœ์„ ํ–ˆ๋‹ค.

0. ๊ธฐ๋ณธ ์„ธํŒ… โœ”

- ๊ฒŒ์‹œ๊ธ€๊ณผ ๋Œ“๊ธ€์€ 1:N ๋‹จ๋ฐฉํ–ฅ ์—ฐ๊ด€๊ด€๊ณ„๋กœ ๋งคํ•‘
- ์˜์†์„ฑ ์ „์ด๋ฅผ ์ ์šฉํ•˜์—ฌ ๊ฒŒ์‹œ๊ธ€์ด ์‚ญ์ œ๋  ๋•Œ ๋Œ“๊ธ€๋„ ๋ชจ๋‘ ์‚ญ์ œ๋˜๊ฒŒํ•จ

@ManyToOne @JoinColumn private HoneyPost honeyPost;
@OneToMany(cascade = CascadeType.ALL) private List<HoneyComment> honeyComments;

1. id๊ฐ’์„ ๋ณด๋‚ด์ฃผ๋Š” ์‹œ๊ธฐ โœ”

- ๊ฒŒ์‹œ๊ธ€ ์ƒ์„ธ ์กฐํšŒ ํŽ˜์ด์ง€๋กœ ๋„˜์–ด๊ฐˆ ๋•Œ model์— ๋‹ด์•„์„œ ํ•จ๊ป˜ ๋ณด๋‚ด์ค€๋‹ค.

@GetMapping("honey/detail/{id}") public String honeyDetail(@PathVariable Long id, Model model) { model.addAttribute("ParentId",id); model.addAttribute("honeyDetail",honeyService.findOne(id)); return "honey/honey-detail"; }

2. view์—์„œ ๋ฐ›์„ ๋•Œ โœ”

mustache๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์—{{TargetId}}๋กœ ๊ฐ„๋‹จํ•˜๊ฒŒ id๊ฐ’ ๋ฐ›์„ ์ˆ˜ ์žˆ๋‹ค.

<button type="button" class="btn btn-sm btn-primary" onclick="saveComment({{ParentId}})">๋“ฑ๋ก</button>

3. ์ €์žฅ์‹œ์—, api์— id๊ฐ’ ๋‹ด์•„์„œ ๋ณด๋‚ด๊ธฐ โœ”

$.ajax({ type: 'POST', url: `/api/honeyc/${ParentId}`, contentType: 'application/json; charset=utf-8', data: JSON.stringify(data) }).done(function (success) { console.log(success) alert('๋Œ“๊ธ€์ด ๋“ฑ๋ก๋˜์—ˆ์Šต๋‹ˆ๋‹ค.'); window.location.reload(); }).fail(function (error) { console.log(error) alert(JSON.stringify(error)); });

4. ์œ ์˜๋ฏธํ•œ ๋ฉ”์†Œ๋“œ๋ฅผ ์ƒ์„ฑํ•˜์—ฌ ๋Œ“๊ธ€๊ณผ ๊ธ€(post) ์„ค์ • โœ”

 //์ƒ์„ฑ public void saveComment(Long targetId,HoneyCommentApiDto commentApiDto) { //์—ฐ๊ด€๋œ ๊ฒŒ์‹œ๊ธ€ HoneyPost honeyPost = honeyRepository.findById(targetId).orElseThrow( () -> new IllegalArgumentException("ํ•ด๋‹น ์•„์ด๋””๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.") ); HoneyComment comment = commentRepository.save(commentApiDto.toEntity()); comment.changePost(honeyPost); }
public void changePost(HoneyPost honeyPost) { this.honeyPost=honeyPost; }

DB์—๋„ FK๊ฐ’์ด ์ž˜ ๋งคํ•‘๋œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

728x90
Comments