์๋น์ค๊ฐ ํ์ํ ์ด์
โ
๋ชจ๋ํ
์๋ฅผ๋ค์ด ์ด๋ค ์ปจํธ๋กค๋ฌ๊ฐ ์ฌ๋ฌ๊ฐ์ ๋ฆฌํฌ์งํฐ๋ฆฌ๋ฅผ ์ฌ์ฉํ์ฌ ๋ฐ์ดํฐ๋ฅผ ์กฐํํํ ๊ฐ๊ณตํ์ฌ ๋ฆฌํดํ๋ค๊ณ ๊ฐ์ ํด ๋ณด์. ์ด๋ฌํ ๊ธฐ๋ฅ์ ์๋น์ค๋ก ๋ง๋ค์ด ๋๋ฉด ์ปจํธ๋กค๋ฌ์์๋ ํด๋น ์๋น์ค๋ฅผ ํธ์ถํ์ฌ ์ฌ์ฉํ๋ฉด ๋๋ค. ํ์ง๋ง ์๋น์ค๋ก ๋ง๋ค์ง ์๊ณ ์ปจํธ๋กค๋ฌ์์ ๊ตฌํํ๋ ค ํ๋ค๋ฉด ํด๋น ๊ธฐ๋ฅ์ ํ์๋ก ํ๋ ๋ชจ๋ ์ปจํธ๋กค๋ฌ๊ฐ ๋์ผํ ๊ธฐ๋ฅ์ ์ค๋ณต์ผ๋ก ๊ตฌํํด์ผ ํ๋ค. ์ด๋ฌํ ์ด์ ๋ก ์๋น์ค๋ ๋ชจ๋ํ๋ฅผ ์ํด์ ํ์ํ๋ค.
โ
๋ณด์
์ปจํธ๋กค๋ฌ๋ ๋ฆฌํฌ์งํฐ๋ฆฌ ์์ด ์๋น์ค๋ฅผ ํตํด์๋ง ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ์ ๊ทผํ๋๋ก ๊ตฌํํ๋ ๊ฒ์ด ๋ณด์์ ์์ ํ๋ค. ์ด๋ ๊ฒ ํ๋ฉด ์ด๋ค ํด์ปค๊ฐ ํดํน์ ํตํด ์ปจํธ๋กค๋ฌ๋ฅผ ์ ์ดํ ์ ์๊ฒ ๋๋๋ผ๋ ๋ฆฌํฌ์งํฐ๋ฆฌ์ ์ง์ ์ ๊ทผํ ์๋ ์๊ฒ ๋๋ค.
โ
์ํฐํฐ ๊ฐ์ฒด์ DTO ๊ฐ์ฒด์ ๋ณํ
์ฐ๋ฆฌ๊ฐ ์์ฑํ Question, Answer ํด๋์ค๋ ์ํฐํฐ(Entity) ํด๋์ค์ด๋ค. ์ํฐํฐ ํด๋์ค๋ ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ์ง์ ๋ง๋ฟ์ ์๋ ํด๋์ค์ด๊ธฐ ๋๋ฌธ์ ์ปจํธ๋กค๋ฌ๋ ํ์๋ฆฌํ ๊ฐ์ ํ ํ๋ฆฟ ์์ง์ ์ ๋ฌํ์ฌ ์ฌ์ฉํ๋ ๊ฒ์ ์ข์ง ์๋ค. ์ปจํธ๋กค๋ฌ๋ ํ์๋ฆฌํ์์ ์ฌ์ฉํ๋ ๋ฐ์ดํฐ ๊ฐ์ฒด๋ ์์ฑ์ ๋ณ๊ฒฝํ์ฌ ๋น์ฆ๋์ค์ ์ธ ์๊ตฌ๋ฅผ ์ฒ๋ฆฌํด์ผ ํ๋ ๊ฒฝ์ฐ๊ฐ ๋ง์๋ฐ ์ํฐํฐ๋ฅผ ์ง์ ์ฌ์ฉํ์ฌ ์์ฑ์ ๋ณ๊ฒฝํ๋ค๋ฉด ํ ์ด๋ธ ์ปฌ๋ผ์ด ๋ณ๊ฒฝ๋์ด ์๋ง์ด ๋ ์๋ ์๊ธฐ ๋๋ฌธ์ด๋ค.
์ด๋ฌํ ์ด์ ๋ก Question, Answer ๊ฐ์ ์ํฐํฐ ํด๋์ค๋ ์ปจํธ๋กค๋ฌ์์ ์ฌ์ฉํ ์ ์๊ฒ๋ ์ค๊ณํ๋ ๊ฒ์ด ์ข๋ค. ๊ทธ๋ฌ๊ธฐ ์ํด์๋ Question, Answer ๋์ ์ฌ์ฉํ DTO(Data Transfer Object) ํด๋์ค๊ฐ ํ์ํ๋ค. ๊ทธ๋ฆฌ๊ณ Question, Answer ๋ฑ์ ์ํฐํฐ ๊ฐ์ฒด๋ฅผ DTO ๊ฐ์ฒด๋ก ๋ณํํ๋ ์์ ๋ ํ์ํ๋ค. ๊ทธ๋ฌ๋ฉด ์ํฐํฐ ๊ฐ์ฒด๋ฅผ DTO ๊ฐ์ฒด๋ก ๋ณํํ๋ ์ผ์ ์ด๋์ ์ฒ๋ฆฌํด์ผ ํ ๊น? ๊ทธ๋ ๋ค. ๋ฐ๋ก ์๋น์ค์ด๋ค. ์๋น์ค๋ ์ปจํธ๋กค๋ฌ์ ๋ฆฌํฌ์งํฐ๋ฆฌ์ ์ค๊ฐ์์ ์ธ ์ ์ฅ์์ ์ํฐํฐ ๊ฐ์ฒด์ DTO ๊ฐ์ฒด๋ฅผ ์๋ก ๋ณํํ์ฌ ์๋ฐฉํฅ์ ์ ๋ฌํ๋ ์ญํ ์ ํ๋ค.

[์ถ์ฒ] ์๋น์ค|์์ฑ์ drv982
drv982๋์๋ธ๋ก๊ทธ : ๋ค์ด๋ฒ ๋ธ๋ก๊ทธ
drv98
blog.naver.com
sbbํจํค์ง ์๋์ ์์ผ๋ฉด ์ปจํฌ๋ํธ ์ค์บ์ ์๋์ผ๋ก ํด์ค

package com.mysite.sbb.question;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class QuestionService {
@Autowired
private QuestionRepository qRepo;
public List<Question> getList(){
return this.qRepo.findAll(); //๋ชจ๋ ์ง๋ฌธ ๋ฆฌ์คํธ
}
}
QuestionController- ์์
package com.mysite.sbb.question;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class QuestionController {
@Autowired
private QuestionService qService;
@GetMapping("/question/list")
public String list(Model model) {
List<Question> qList = this.qService.getList();
model.addAttribute("qList", qList);
return "question_list";
}
}
ํ ์คํธํ๊ธฐ ์ด์์์

์ปจํธ๋กค๋ฌ : ์๋น์ค๊ฐ์ฒด ์ฃผ์ ํด์ getList() ๋ฉ์๋ ํธ์ถ
์๋น์ค : ๋ฆฌํฌ์งํ ๋ฆฌ ์ฃผ์ ํด์ findAll() ๋ฉ์๋ ํธ์ถ
๋ฆฌํฌ์งํ ๋ฆฌ : JPA ํ์ด๋ฒ๋ค์ดํธ ์์ ์๋์ผ๋ก ๊ตฌํ๋ findAll()๋ฉ์๋๋ก DB์์ ์ง๋ฌธ๋ฐ์ดํฐ๋ค์ ๊ฐ์ ธ์ด
html ์์
์ ๋ชฉ์ ๋๋ฅด๋ฉด ํด๋น ๊ธ์ ์์ธ๋ณด๊ธฐ๋ก ์ด๋ํ ๊ฑฐ์
th:href
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>์ง๋ฌธ ๋ฆฌ์คํธ</title>
</head>
<body>
<h2>ํฌ๋ก์ฐ ํ์๋ฆฌํ</h2>
<table>
<thead>
<tr>
<th>์ ๋ชฉ</th>
<th>์์ฑ์ผ์</th>
</tr>
</thead>
<tbody>
<tr th:each="question : ${qList}">
<td>
<!-- localhost8080/contextpath๊น์ง ์ ์ฉ -->
<a
th:href="@{/question/detail/__${question.id}__}"
th:text="${question.subject}"
></a>
</td>
<td th:text="${question.createDate}"></td>
</tr>
</tbody>
</table>
</body>
</html>

ํด๋ฆญํ์ ๊ฒฝ์ฐ question id ๋ค์ด๊ฐ
ํ์ธํด๋ณด๋ ์๋ง๊ฒ 2 ๋ค์ด๊ฐ ๊ฒ ํ์ธ๋จ


QuestionController์์ detail ํ์ด์ง ์ถ๊ฐ
//์์ธ๋ณด๊ธฐ ํ์ด์ง
@GetMapping("/question/detail/{id}")
public String detail(Model model ,@PathVariable("id") int id) {
return "question_detail";
}
ํ ์คํธ์ฉ
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>์ง๋ฌธ ๋ํ
์ผ</title>
</head>
<body>
<h1>์ ๋ชฉ</h1>
<div>๋ด์ฉ</div>
</body>
</html>

QuestionService
package com.mysite.sbb.question;
import java.util.List;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class QuestionService {
@Autowired
private QuestionRepository qRepo;
public List<Question> getList(){
return this.qRepo.findAll(); //๋ชจ๋ ์ง๋ฌธ ๋ฆฌ์คํธ
}
public Question getQuestion(int id) {
Optional<Question> question = qRepo.findById(id);
if(question.isPresent()) {
return question.get();
}else {
//์ ์๋ฌ๋ฅผ ์คํ
throw new DataNotFoundException("question not found");
}
}
}
DataNotFoundException

๋ฐํ์์์ธ DataNotFoundException ๋ง๋ค์ด์ id๋ก ์ง๋ฌธ์ ์ฐพ์ง ๋ชปํ๋ค๋ฉด 404 ์๋ฌ ๋ฐ์
package com.mysite.sbb.question;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;
//404 Not Found code
@ResponseStatus(value = HttpStatus.NOT_FOUND)
public class DataNotFoundException extends RuntimeException {
private static final long serialVersionUID = 1L;
public DataNotFoundException(String message) {
super(message);
}
}
QuestionController
//์์ธ๋ณด๊ธฐ ํ์ด์ง
@GetMapping("/question/detail/{id}")
public String detail(Model model ,@PathVariable("id") int id) {
Question question = qService.getQuestion(id);
model.addAttribute("question", question);
return "question_detail";
}
question_detail
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>์ง๋ฌธ ๋ํ
์ผ</title>
</head>
<body>
<h1 th:text="${question.subject}"></h1>
<div th:text="${question.content}"></div>
</body>
</html>
ํ ์คํธํ๊ธฐ

๋ง์ฝ ์๋ ์์ด๋๋ก ๊ฒ์ํ๋ฉด?

์ปจํธ๋กค๋ฌ์ requestMapping์ถ๊ฐ๋ก ์ข ๋ ๊ฒฝ๋ก ๋จ์ํ๊ฒ ์์ ํจ
package com.mysite.sbb.question;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/question")
public class QuestionController {
@Autowired
private QuestionService qService;
@GetMapping("/list")
public String list(Model model) {
List<Question> qList = this.qService.getList();
model.addAttribute("qList", qList);
return "question_list";
}
//์์ธ๋ณด๊ธฐ ํ์ด์ง
@GetMapping("/detail/{id}")
public String detail(Model model ,@PathVariable("id") int id) {
Question question = qService.getQuestion(id);
model.addAttribute("question", question);
return "question_detail";
}
}
'BACKEND > SpringBoot' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| ์คํํฑ ๋๋ ํฐ๋ฆฌ์ ์คํ์ผ์ํธ (0) | 2023.11.09 |
|---|---|
| ๋ต๋ณ ๋ฑ๋กํ๊ธฐ (0) | 2023.11.09 |
| Question ์ปจํธ๋กค๋ฌ ์์ฑ (1) | 2023.11.08 |
| ํจํค์ง ๋๋ฉ์ธ ๋ณ ๋ถ๋ฆฌํ๊ธฐ (0) | 2023.11.08 |
| ๋ต๋ณ AnswerRepository ์์ฑ ๋ฐ ๋ฐ์ดํฐ ์ ์ฅ ,๊ฒ์ (0) | 2023.11.08 |