๐œ† Spring Boot ์˜ˆ์ œ3 (ํšŒ์›๊ด€๋ฆฌ)

@Autowiredยท2022๋…„ 1์›” 9์ผ
0

Spring Boot

๋ชฉ๋ก ๋ณด๊ธฐ
4/11

Spring Boot์—์„œ ์ƒ์„ธ์กฐํšŒ ๋ฐ ํšŒ์› ์‚ญ์ œ ํ•ด๋ณด๊ธฐ!

  • ์‚ฌ์šฉ IDE : IntelliJ IDEA Ultimate
  • ์‚ฌ์šฉ DB : MySQL
  • ์‚ฌ์šฉ ์–ธ์–ด & SDK : Java & Amazon correto 11
  • thymeleaf ์‚ฌ์šฉ์„ ๊ฐ€๊ธ‰์  ์ค„์ธ ์˜ˆ์ œ์ž…๋‹ˆ๋‹ค.
  • ajax๋กœ ์กฐํšŒ๋Š” ํ”„๋ ˆ์ž„์›Œํฌ์™€ ๋™์ผํ•˜๋ฏ€๋กœ ๋นผ๊ฒ ์Šต๋‹ˆ๋‹ค.
  • ์ •๋ฆฌ๋ณธ์ž…๋‹ˆ๋‹ค ์ฐธ๊ณ ์šฉ์œผ๋กœ๋งŒ ๋ด์ฃผ์„ธ์š”!

์ด์ „ ๊ธ€
ฮธ Spring Boot ์˜ˆ์ œ1 (ํšŒ์›๊ด€๋ฆฌ)
๐œ… Spring Boot ์˜ˆ์ œ2 (ํšŒ์›๊ด€๋ฆฌ)


์ƒ์„ธ์กฐํšŒ ๋ฐ ์‚ญ์ œ

  • ์ƒ์„ธ์กฐํšŒ ์ˆœ์„œ

    1. findAll.html์—์„œ Controller๋กœ memberId(pk๊ฐ’)์„ ๋ณด๋ƒ„.

    2. Controller์—์„œ Service๋กœ ์ „๋‹ฌ

    3. Service์—์„œ jpa์˜ findById๋กœ ์ •๋ณด ์š”์ฒญ

    4. ๋ฐ›์€ Optionalํƒ€์ž…์˜ ์ •๋ณด๋ฅผ .get()์„ ํ†ตํ•ด Entity ํƒ€์ž…์œผ๋กœ ๋ณ€ํ™˜

    5. Entityํƒ€์ž…์„ DTOํƒ€์ž…์œผ๋กœ ๋ณ€ํ™˜ ํ›„ Controller ์— ์ „๋‹ฌ

    6. ์‚ฌ์šฉ์ž๊ฐ€ ์ง€์ •ํ•œ ํ–‰๋™ ์ˆ˜ํ–‰

  • ์‚ญ์ œ ์ˆœ์„œ

    1. findAll.html์—์„œ Controller๋กœ memberId(pk๊ฐ’)์„ ๋ณด๋ƒ„.

    2. Controller์—์„œ Service๋กœ ์ „๋‹ฌ

    3. Service์—์„œ jpa์˜ delete๋กœ ์ „๋‹ฌ

    4. ์‚ฌ์šฉ์ž๊ฐ€ ์ง€์ •ํ•œ ํ–‰๋™ ์ˆ˜ํ–‰

ํšŒ์› ์‚ญ์ œ๋Š” ์ •๋ง ๊ฐ„๋‹จํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ƒ์„ธ์กฐํšŒ๋ฅผ ์„ค๋ช…ํ•˜๋ฉด์„œ ๊ฐ™์ด ์„ค๋ช…ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.


findAll.html

findAll.html์— ์ƒ์„ธ์กฐํšŒ & ์‚ญ์ œ ๋งํฌ ์ถ”๊ฐ€.
์‚ฌ์šฉ ์–‘์‹์— ์˜คํƒ€๊ฐ€ ์•ˆ์ƒ๊ธฐ๊ฒŒ ์ฃผ์˜ํ•ด์ฃผ์„ธ์š”

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
	<meta charset="UTF-8">
	<title>Title</title>
</head>
<body>
	<h2>findAll.html</h2>
	<table>
		<thead>
			<tr>
				<th>๋ฒˆํ˜ธ</th>
				<th>์ด๋ฉ”์ผ</th>
				<th>๋น„๋ฐ€๋ฒˆํ˜ธ</th>
				<th>์ด๋ฆ„</th>
				<th>์กฐํšŒ</th>
				<th>์‚ญ์ œ(get)</th>
				<!-- ์•„๋ž˜๋Š” ์ฐจํ›„์— ๋ฐฐ์šฐ๋ฉด ์ˆ˜์ •ํ•ด์„œ ์˜ฌ๋ฆฌ๊ฒ ์Šต๋‹ˆ๋‹ค. -->
				<th>์‚ญ์ œ(delete)</th>
			</tr>
		</thead>
		<tbody>
			<tr th:each="member: ${memberList}">
				<td th:text="${member.memberId}">๋ฒˆํ˜ธ</td>
				<td th:text="${member.memberEmail}">์ด๋ฉ”์ผ</td>
				<td th:text="${member.memberPassword}">๋น„๋ฐ€๋ฒˆํ˜ธ</td>
				<td th:text="${member.memberName}">์ด๋ฆ„</td>
				<!-- detail ์ฃผ์†Œ ์ฒ˜๋ฆฌ๋ฐฉ์‹ @{|/์ฃผ์†Œ/${๊ฐ™์ด ๋ณด๋‚ผ ๋ฐ์ดํ„ฐ}|} -->
				<td><a th:href="@{|/member/${member.memberId}|}">์กฐํšŒ</a></td>
				<td><a th:href="@{|/member/delete/${member.memberId}|}">์‚ญ์ œ</a></td>
				<td></td>
			</tr>
		</tbody>
	</table>
</body>
</html>

Controller

findAll.html์— ์ฃผ์†Œ๋ฅผ ๋งŒ๋“ค์—ˆ์œผ๋‹ˆ ์ด๋ฅผ ๋ฐ›๋Š” ๋ฉ”์„œ๋“œ๋ฅผ ๋งŒ๋“ค์–ด๋ด…์‹œ๋‹ค!

// ์ƒ์„ธ์กฐํšŒ
// /member/2, /member/15 => /member/{memberId}
// @PathVariavle : ๊ฒฝ๋กœ์ƒ์— ์žˆ๋Š” ๋ณ€์ˆ˜๋ฅผ ๊ฐ€์ ธ์˜ฌ ๋•Œ ์‚ฌ์šฉ
@GetMapping("{memberId}")
//public String findById(@PathVariable("memberId") Long memberId, Model model) {
// @PathVarialbe์—์„œ ๋ฐ›๋Š” ๊ฐ’์˜ ์ด๋ฆ„๊ณผ ๋งค๊ฐœ๋ณ€์ˆ˜์˜ ๊ฐ’์˜ ์ด๋ฆ„์ด ๊ฐ™๋‹ค๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ์ƒ๋žต๊ฐ€๋Šฅ
public String findById(@PathVariable Long memberId, Model model) {
	MemberDetailDTO memberDetailDTO = ms.findById(memberId);
	model.addAttribute("member", memberDetailDTO);
	return "member/findById";
}

// ํšŒ์›์‚ญ์ œ(/member/delete/5)
@GetMapping("delete/{memberId}")
public String deleteById(@PathVariable("memberId") Long memberId) {
	ms.deleteById(memberId);
	return "redirect:/member/";
}

Service

  • ์ƒ์„ธ์กฐํšŒ ์ˆœ์„œ

    1. jpa์˜ findByID ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ (๋ฉ”์„œ๋“œ ์ด๋ฆ„ ์ž„์˜๋ณ€๊ฒฝ ๊ธˆ์ง€!)

    2. Entity๋กœ ๋ฐ›์€ ๊ฐ’์„ DTO๋กœ ๋ณ€ํ™˜ (์—ฌ๊ธฐ์„œ๋Š” ๋ณ€ํ™˜ํ•˜์ง€๋งŒ, Entityํƒ€์ž…์œผ๋กœ๋„ ์‚ฌ์šฉ๊ฐ€๋Šฅ)

    3. Controller๋กœ ์ „๋‹ฌ.

  • ์‚ญ์ œ ์ˆœ์„œ

    1. jpa์˜ deleteById ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ๋งŒ ํ•˜๋ฉด ์ž๋™์œผ๋กœ ์‚ญ์ œ ์ˆ˜ํ–‰ ๋ฐ ์ข…๋ฃŒ.

Service (Interface)

// Interface์— ์•„๋ž˜ 2์ค„ ์ถ”๊ฐ€

MemberDetailDTO findById(Long memberId);

void deleteById(Long memberId);

ServiceImpl

@Override
public MemberDetailDTO findById(Long memberId) {

	// findById๋ฅผ ํ•˜๊ฒŒ ๋˜๋ฉด jpa์—์„œ Optionalํƒ€์ž…์œผ๋กœ ์คŒ.
	// Optional<MemberEntity> memberEntityOptional = mr.findById(memberId);
    
	// Optional ํƒ€์ž…์„ Entityํƒ€์ž…์œผ๋กœ ๋ฐ”๊พธ๊ธฐ ์œ„ํ•ด .get()์„ ๋ถ™์ž„
	// MemberEntity memberEntity = memberEntityOptional.get();
    
	// Entity ํƒ€์ž…์„ DTO๋กœ ํƒ€์ž…์œผ๋กœ ๋ฐ”๊พธ๊ธฐ ์œ„ํ•ด ์ „์— DTO์— ๋งŒ๋“ค์–ด ๋†“์€ ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•ด ๋ณ€ํ™˜
	// MemberDetailDTO memberDetailDTO = MemberDetailDTO.toMemberDetailDTO(memberEntity);
    
	// ๋ณ€ํ™˜๋œ DTOํƒ€์ž…์„ ๋ฆฌํ„ดํ•จ.
	// return memberDetailDTO;
    
	// ์œ„ ์ฝ”๋“œ๋ฅผ ์•„๋ž˜ ํ•œ์ค„๋กœ ์š”์•ฝ ๊ฐ€๋Šฅ
	return MemberDetailDTO.toMemberDetailDTO(mr.findById(memberId).get());
}

@Override
public void deleteById(Long memberId) {
	// jpa์˜ deleteBy~ ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœ๋งŒ ํ•˜๋ฉด ๋.
	mr.deleteById(memberId);
}

findById.html ์ƒ์„ฑ

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
	<meta charset="UTF-8">
	<title>Title</title>
</head>
<body>
	<h2>findById.html</h2>
	<table>
		<thead>
			<tr>
				<th>๋ฒˆํ˜ธ</th>
				<th>์ด๋ฉ”์ผ</th>
				<th>๋น„๋ฐ€๋ฒˆํ˜ธ</th>
				<th>์ด๋ฆ„</th>
			</tr>
		</thead>
		<tbody>
			<tr>
				<td th:text="${member.memberId}"></td>
				<td th:text="${member.memberEmail}"></td>
				<td th:text="${member.memberPassword}"></td>
				<td th:text="${member.memberName}"></td>
			</tr>
		</tbody>
	</table>
</body>
</html>

์—ฌ๊ธฐ๊นŒ์ง€ ์ž˜ ๋”ฐ๋ผ์˜ค์…จ์œผ๋ฉด ํšŒ์› ์ƒ์„ธ์กฐํšŒ ๋ฐ ์‚ญ์ œ๊นŒ์ง€๋Š” ๋๋‚œ๊ฒ๋‹ˆ๋‹ค.
์ถ”๊ฐ€๋กœ REST API์˜ Delete ๋ฐฉ์‹์€ ๋‹ค์Œ ์ˆ˜์—… ์ง„ํ–‰ ํ›„ ์ •๋ฆฌํ•˜๋Š”๊ฑธ๋กœ ํ•˜๊ณ  ๋‹ค์Œ์‹œ๊ฐ„์— ๋งŒ๋‚˜์š”~ :D

profile
์ฆ๊ฒ๋‹ค!

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