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

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

Spring Boot

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

Spring Boot์—์„œ ํšŒ์› ๋ชฉ๋ก ์กฐํšŒ ํ•ด๋ณด๊ธฐ!

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

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


ํšŒ์›๋ชฉ๋ก ๊ตฌ์กฐ

  • ์ˆœ์„œ

    1. html์—์„œ Controller๋กœ ์š”์ฒญ

    2. Controller์—์„œ Service๋กœ ์š”์ฒญ

    3. Service์—์„œ Repository(JPA)๋กœ ์š”์ฒญ

    4. Repository์—์„œ Entityํƒ€์ž…์œผ๋กœ ์ „๋‹ฌ

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

    6. Controller์—์„œ model์— ๋‹ด์€ ๋’ค ์‚ฌ์šฉ์ž๊ฐ€ ์ง€์ •ํ•œ ์ž‘์—… ์ˆ˜ํ–‰


index.html

์–ด์ œ ๋งŒ๋“ค์–ด ๋†“์€ index.html์— ํšŒ์›๋ชฉ๋ก์œผ๋กœ ๊ฐ€๋Š” ๋งํฌ๋ฅผ ๋งŒ๋“ญ์‹œ๋‹ค.
REST API ํ˜•์‹์œผ๋กœ ๋งŒ๋“ค๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์— /member ๋กœ ์ฃผ์†Œ๋ฅผ ์„ค์ •ํ•ด์ค๋‹ˆ๋‹ค.

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Title</title>
</head>
<body>
	<h2>index.html</h2>
	<a href="/member/save">ํšŒ์›๊ฐ€์ž…</a>
	<a href="/member/login">๋กœ๊ทธ์ธ</a>
	<!-- ํšŒ์›๋ชฉ๋ก์œผ๋กœ ๊ฐ€๋Š” ๋งํฌ ์ถ”๊ฐ€ -->
	<a href="/member/">ํšŒ์›๋ชฉ๋ก</a>
</body>
</html>

Detail์šฉ DTO ์ƒ์„ฑ

ํšŒ์› ๋ชฉ๋ก์กฐํšŒ ๋ฐ ์ƒ์„ธ์กฐํšŒ์šฉ DTO๋ฅผ ํ•˜๋‚˜ ๋งŒ๋“ค์–ด์•ผํ•ฉ๋‹ˆ๋‹ค.
๋‘˜๋‹ค ์กฐํšŒ๋ผ๋Š” ๋น„์Šทํ•œ ๊ฐœ๋…์ด๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ™์€ DTO๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

@Data
@NoArgsConstructor // ๊ธฐ๋ณธ ์ƒ์„ฑ์ž
@AllArgsConstructor // ๋ชจ๋“  ํ•„๋“œ๋ฅผ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ํ•˜๋Š” ์ƒ์„ฑ์ž
// ์ƒ์„ฑ์ž๋Š” ์“ฐ์ด๋Š” ์ผ์ด ์—†๋”๋ผ๋„ ๋งŒ๋“ค์–ด๋†“์•„๋„ ๋ฌธ์ œ ์—†์Œ.
public class MemberDetailDTO {

	private Long memberId;
	private String memberEmail;
	private String memberPassword;
	private String memberName;

}

Controller

์œ„์—์„œ ๋งํ•œ๊ฒƒ์ฒ˜๋Ÿผ ์ฃผ์†Œ๊ฐ€ ์—†๊ธฐ๋•Œ๋ฌธ์— @GetMapping ๋’ค์— ์ฃผ์†Œ๋ฅผ ์ƒ๋žต.
๋‚˜๋จธ์ง€๋Š” ์Šคํ”„๋ง ํ”„๋ ˆ์ž„์›Œํฌ์—์„œ์ฒ˜๋Ÿผ ๋ฆฌ์ŠคํŠธ๋กœ ๋ฐ›์•„์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋ฆฌ์ŠคํŠธ ์„ ์–ธ ๋ฐ Service ํ˜ธ์ถœ

// ํšŒ์›๋ชฉ๋ก ์กฐํšŒ
@GetMapping
public String findAll(Model model) {
	List<MemberDetailDTO> memberList = ms.findAll();
	model.addAttribute("memberList", memberList);
	return "member/findAll";
}

Service

  • Service์—์„œ ์ฒ˜๋ฆฌ ์ˆœ์„œ

    1. jpa์— findAll๋ฉ”์„œ๋“œ ํ˜ธ์ถœ (๋ฉ”์„œ๋“œ ์ด๋ฆ„ ๋ฐ˜๋“œ์‹œ ์ง€์ผœ์•ผํ•จ!)

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

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

Service (Interface)

// Interface์— ์•„๋ž˜ ์ฝ”๋“œ ์ถ”๊ฐ€
List<MemberDetailDTO> findAll();

ServiceImpl

@Override
public List<MemberDetailDTO> findAll() {
	// findAll์ด๋ผ๋Š” ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ ๋ฐ Entityํƒ€์ž…์˜ List์— ํ˜ธ์ถœ ๊ฒฐ๊ณผ ์ €์žฅ
	List<MemberEntity> memberEntityList = mr.findAll();
	// Entityํƒ€์ž…์˜ List๋ฅผ DTOํƒ€์ž…์˜ List๋กœ ๋ณ€ํ™˜
	// ์œ„์—์„œ ๋งํ–ˆ๋‹ค์‹œํ”ผ ๋ฐ˜๋“œ์‹œ ๋ณ€ํ™˜ํ•ด ์ค„ ํ•„์š”๋Š” ์—†์Œ.
	List<MemberDetailDTO> memberDetailDTOList = MemberDetailDTO.change(memberEntityList);
	return memberDetailDTOList;
}

Detail์šฉ DTO์—์„œ Entity -> DTO ๋ณ€ํ™˜

// MemberEntity -> MemberDetailDTO
// ๋ฆฌํ„ดํƒ€์ž… : MemberDetailDTO
// ๋งค๊ฐœ๋ณ€์ˆ˜ํƒ€์ž… : MemberEntity
public static MemberDetailDTO toMemberDetailDTO(MemberEntity memberEntity) {
	MemberDetailDTO memberDetailDTO = new MemberDetailDTO();
	memberDetailDTO.setMemberId(memberEntity.getId());
	memberDetailDTO.setMemberEmail(memberEntity.getMemberEmail());
	memberDetailDTO.setMemberPassword(memberEntity.getMemberPassword());
	memberDetailDTO.setMemberName(memberEntity.getMemberName());
	return memberDetailDTO;
}

// Entityํƒ€์ž…์˜ List๋ฅผ DTOํƒ€์ž…์˜ List๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ๋ฉ”์„œ๋“œ.
// ์œ„์— ์žˆ๋Š” Entity๋ฅผ DTO๋กœ ๋ณ€ํ™˜ํ•ด์ฃผ๋Š” ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด ๋” ๊ฐ„๋‹จํ•˜๊ฒŒ ๊ตฌํ˜„
public static List<MemberDetailDTO> change(List<MemberEntity> memberEntityList) {
	List<MemberDetailDTO> memberDetailDTOList = new ArrayList<>();
	for (MemberEntity m: memberEntityList) {
		memberDetailDTOList.add(toMemberDetailDTO(m));
	}
	return memberDetailDTOList;
}

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>
			</tr>
		</thead>
		<tbody>
			<!-- ์Šคํ”„๋ง ํ”„๋ ˆ์ž„์›Œํฌ์— <c:forEach items="${mList}" var="m"> ์™€ ๊ฐ™์€ ์—ญํ•  -->
			<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>
			</tr>
		</tbody>
	</table>
</body>
</html>
profile
์ฆ๊ฒ๋‹ค!

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