๐ก
JPA๋ฅผ ํ์ฉํ์ฌ ๊ฒ์๊ธ ์์ฑ ๊ธฐ๋ฅ์ ๊ตฌํํ๋ ๋ฐฉ๋ฒ์ ํ์ตํฉ๋๋ค. ์ด ๊ณผ์ ์์๋๋ฐ์ดํฐ ์ ์ฅ, DTO ์ฌ์ฉ, ์ปจํธ๋กค๋ฌ ๊ตฌํ ๋ฑ์ ๋ค๋ฃจ๋ฉฐ, ORM์ ์์์ฑ ๊ด๋ฆฌ์ ๋ํ ์ดํด๋ฅผ ์ฌํํฉ๋๋ค.
BoardRepository save ๋ฉ์๋ ๊ตฌํํ๊ธฐ
๋ชฉํ: Board ์ํฐํฐ๋ฅผ ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ์ ์ฅํ๋ save ๋ฉ์๋๋ฅผ ๊ตฌํํฉ๋๋ค.
BoardRepository ์ฝ๋ ์ถ๊ฐ (save)
@RequiredArgsConstructor
@Repository // IoC
public class BoardRepository {
private final EntityManager em;
/**
* ๊ฒ์๊ธ ์กฐํ ๋ฉ์๋
* @param id ์กฐํํ ๊ฒ์๊ธ Id
* @return ์กฐํ๋ Board ์ํฐํฐ, ์กด์ฌํ์ง ์์ผ๋ฉด null ๋ฐํ
*/
public Board findById(int id){
return em.find(Board.class, id);
}
/**
* JPQL์ FETCH ์กฐ์ธ ์ฌ์ฉ - ์ฑ๋ฅ ์ต์ ํ
* ํ๋ฐฉ์ ์ฟผ๋ฆฌ๋ฅผ ์ฌ์ฉํด์ ์ฆ, ์ง์ ์กฐ์ธํด์ ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ ์ต๋๋ค.
* @param id
* @return
*/
public Board findByIdJoinUser(int id){
// JPQL -> Fetch join ์ ์ฌ์ฉํด ๋ณด์.
String jpql = " SELECT b FROM Board b JOIN FETCH b.user WHERE b.id = :id ";
return em.createQuery(jpql, Board.class)
.setParameter("id", id)
.getSingleResult();
}
/**
* ๋ชจ๋ ๊ฒ์๊ธ ์กฐํ
* @return ๊ฒ์๊ธ ๋ฆฌ์คํธ
*/
public List<Board> findAll(){
TypedQuery<Board> jpql = em.createQuery(" SELECT b FROM Board b ORDER BY b.id DESC ", Board.class);
return jpql.getResultList();
}
// em.persist(board); -> ๋น์์ ์ํ์ธ ์ํฐํฐ๋ฅผ ์์์ํ๋ก ์ ํ
@Transactional
public Board save(Board board){
em.persist(board);
return board;
}
BoardDTO
package com.tenco.blog_v1.board;
import com.tenco.blog_v1.user.User;
import lombok.Data;
public class BoardDTO {
@Data
public static class SaveDTO {
private String title;
private String content;
public Board toEntity(User user) {
return Board.builder()
.title(title)
.content(content)
.user(user)
.build();
}
}
}
BoardController ์ฝ๋ ์์ (save)
package com.tenco.blog_v1.board;
import com.tenco.blog_v1.user.User;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@Slf4j
@RequiredArgsConstructor
@Controller
public class BoardController {
// ๋ค์ดํฐ๋ธ ์ฟผ๋ฆฌ ์ฐ์ต
private final BoardNativeRepository boardNativeRepository;
// JPA API, JPQL
private final BoardRepository boardRepository;
private final HttpSession session;
// ์ฃผ์ ์ค๊ณ: http://localhost:8080/board/1
// ํน์ ๊ฒ์๊ธ ์์ฒญ ํ๋ฉด
@GetMapping("/board/{id}")
public String detail(@PathVariable (name = "id" ) Integer id, HttpServletRequest request){
// JPA API ์ฌ์ฉ
// Board board = boardNativeRepository.findById(id);
// JPQL FETCH join ์ฌ์ฉ
Board board = boardRepository.findByIdJoinUser(id);
request.setAttribute("board", board);
return "board/detail";
}
@GetMapping("/")
public String index(Model model){
List<Board> boardList = boardRepository.findAll();
model.addAttribute("boardList", boardList);
return "index";
}
// ์ฃผ์ ์ค๊ณ: http://localhost:8080/board/save-form
// ๊ฒ์๊ธ ์์ฑ ํ๋ฉด
@GetMapping("/board/save-form")
public String saveForm(){
return "board/save-form";
}
// ๊ฒ์๊ธ ์ ์ฅ
// ์ฃผ์ ์ค๊ณ: http://localhost:8080/board/save
@PostMapping("/board/save")
public String save(@ModelAttribute BoardDTO.SaveDTO reqDto){
User sessionUser = (User) session.getAttribute("sessionUser");
if(sessionUser == null){
return "redirect:/login-form";
}
// ํ๋ผ๋ฏธํฐ๊ฐ ์ฌ๋ฐ๋ฅด๊ฒ ์ ๋ฌ ๋์๋์ง ํ์ธ
log.warn("save ์คํ: ์ ๋ชฉ-{}, ๋ด์ฉ={}", reqDto.getTitle(), reqDto.getContent());
// boardNativeRepository.save(title, content);
// SaveDTO์์ toEntity ์ฌ์ฉํด์ Board ์ํฐํฐ๋ก ๋ณํํ๊ณ ์ธ์ ๊ฐ์ผ๋ก User ์ ๋ณด๋ฅผ ๋ฃ์๋ค.
// ๊ฒฐ๊ตญ Board ์ํฐํฐ๋ก ๋ฐํ์ด ๋๋ค.
boardRepository.save(reqDto.toEntity(sessionUser));
return "redirect:/";
}
// ๊ฒ์๊ธ ์์ ํ๋ฉด ์์ฒญ
// ์ฃผ์ ์ค๊ณ: http://localhost:8080/board/id/update
@GetMapping("/board/{id}/update-form")
public String updateForm(@PathVariable(name = "id") Integer id, HttpServletRequest request){
Board board = boardNativeRepository.findById(id);
request.setAttribute("board", board);
return "board/update-form"; // src/main/resources/templates/board/update-form.mustache
}
// ๊ฒ์๊ธ ์์ ์์ฒญ ๊ธฐ๋ฅ
// ์ฃผ์ ์ค๊ณ: http://localhost:8080/board/{id}/update
@PostMapping("/board/{id}/update")
public String update(@RequestParam(name = "id") Integer id, @RequestParam(name = "title") String title, @RequestParam(name = "content") String content){
boardNativeRepository.updateById(id, title, content);
//
return "redirect:/board/" + id;
}
}
@ModelAttribute ์ @RequestBody ์ ๋ํ ์ฐจ์ด์ ์ ์ดํดํ์