์์ ์ ๊ต์ก ๋ Spring MVC๋ก ํ ๋๋ ์ฟผ๋ฆฌ๋ฌธ์ ์ผ์ผํ ๋ค ์์ฑํด์ SQL๋ฌธ๋ง ์ ํ๋ฉด ๋๋๋ฐ JPA์์๋ ์ฟผ๋ฆฌ๋ฌธ์ ์ง์ ์์ฑ ์ํด๋ ์์์ ์์ฑํด์ฃผ๊ธฐ ๋๋ฌธ์ ํจ์๋ฅผ ์ ์์์ผ ํ๋ค๋ ๊ฒ์ ๊นจ๋ฌ์๋ค.
๊ทธ๋์ ๋ค์์ ์ธ ๋๋ ๋์์ด ๋ ์ ์๋๋ก ์ ๋ฆฌํ๊ณ ์ ํ๋ค.
์ผ๋จ Entity ๊ฐ์ฒด์ธ Writing.java์ ๋ํ ์ฝ๋
package com.board.demo.Entity;
import org.hibernate.annotations.CreationTimestamp;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@ToString
@Entity
public class Writing {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long no;
@Column
private String title;
@Column
private String content;
@Column
private String writer;
@CreationTimestamp
@Column
private String write_date;
}
๊ฒ์ํ ํ๋ก์ ํธ๋ฅผ ์์๋ก ๋ค์๋ฉด Create๋ ๊ธ์ ์์ฑํ๋ ๊ฒ์ด๋ค.
Controller.java
@PostMapping("/writepage/write")
public String write(BoardDTO bDto) {
//DTO๋ฅผ Entity๋ก ๋ณํ
Writing writing = bDto.toEntity();
//DB์ ์ ์ฅ
writingRepo.save(writing);
return "redirect:/main";
}
DTO.java
public class BoardDTO(){
private String title;
private String content;
private String writer;
public BoardDTO(){}
//๊ธ ์ฐ๊ธฐ์ ์ฌ์ฉ
public BoardDTO(String title, String content, String writer){
this.title=title;
this.content=content;
this.writer=writer;
}
//DTO๋ฅผ Entity๋ก ๋ณํ
public Writing toEntity(){
return new Writing(null,title,content,writer,null);
}
}
โ๏ธโ๏ธ BoardDTO() ๊ธฐ๋ณธ ์์ฑ์๋ฅผ ์์ฑํ ์ด์ ๋ ์์ฑ ์ํ๋๊น ์๋ฌ๊ฐ ์๊พธ ๋ฐ์ํด์... ๊ตณ์ด ์์ฑ ์ํ๊ณ ๊ทธ๋ฅ @NoArgsConstructor๋ฅผ ์ฌ์ฉํด๋ ๋ ๋ฏ!
๊ฒ์ํ์์๋ Read๋ ๊ธ ๋ชฉ๋ก์ ๋ถ๋ฌ์ค๊ฑฐ๋ ํด๋น ๊ธ์ ์์ธํ ๋ณผ ๋!
Controller.java
@GetMapping("/main")
public ModelAndView every(Model model){
ModelAndView mav = new ModelAndView();
//1. ๋ชจ๋ ๋ฐ์ดํฐ ๊ฐ์ ธ์ค๊ธฐ
List<Writing> writingEntityList = wrService.index();
//2. ๋ชจ๋ธ์ ๋ฐ์ดํฐ ๋ฑ๋กํ๊ธฐ
model.addAttribute("writingList",writingEntityList);
mav.setViewName("main");
//3. ๋ทฐ ํ์ด์ง ์ค์ ํ๊ธฐ
return mav;
}
Repository์ธ WritingRepo.java
package com.board.demo.Repository;
import com.board.demo.Entity.Writing;
import java.util.ArrayList;
import org.springframework.data.repository.CrudRepository;
public interface WritingRepo extends CrudRepository<Writing, Long> {
@Override
ArrayList<Writing> findAll();
}
Service์ธ WritingService.java
@Service
public class WritingService {
@Autowired
private WritingRepo writingRepo;
public List<Writing> index() {
return writingRepo.findAll();
}
}
Controller.java
@GetMapping("/detail")
public ModelAndView detail(@RequestParam("no") long no){
ModelAndView mav = new ModelAndView();
//no ์นผ๋ผ์ผ๋ก ๋ฐ์ดํฐ ๊ฒ์
Writing data = writingRepo.findById(no).orElse(null);
mav.addObject("data", data);
mav.setViewName("content");
return mav;
}
WritingService.java
public Writing detail(long no){
return writingRepo.findById(no).orElse(null);
}
โ๏ธ ์ฌ๊ธฐ์๋ Service๋ฅผ ์๊ฑฐ์น๊ณ Repository์ ๋ฐ๋ก ์ ๊ทผํด์ ๊ฒ์ํ์ง๋ง Service๋ฅผ ๊ฑฐ์น๋ค๋ฉด ์๋ ์ฝ๋๋ก ์์ ํด์ผ ํ๋ค.
Writing data = wrService.detail(no);
๊ฒ์๊ธ ์์ ํ ๋
Controller.java
@PostMapping("/update")
public String update(BoardDTO bDto) {
//TODO: process POST request
Writing writed = bDto.toUpdateEntity();
Writing target = writingRepo.findById(writed.getNo()).orElse(null);
//ํน์๋ null์ด ๋ค์ด์ฌ ๋๋ฅผ ๋๋นํด์
if(target != null){
writingRepo.save(writed);
}
return "redirect:/detail?no="+writed.getNo();
}
update๋ create, read, delete์ ๋ฌ๋ฆฌ ํจ์๊ฐ ๋ฐ๋ก ์์ด์ ๋ฐ๋ ๋ฐ์ดํฐ๋ฅผ ๊ทธ๋ฅ create์ฒ๋ผ save(Entity)ํจ์๋ฅผ ์ฐ๋ฉด ๋จ
BoardDTO.java
public class BoardDTO {
โ๏ธprivate Long no;
private String title;
private String content;
private String writer;
public BoardDTO(){}
//๊ธ ์์ ์ ์ฌ์ฉ
public BoardDTO(String no, String title, String content, String writer){
this.no=Long.parseLong(no);
this.title = title;
this.content=content;
this.writer=writer;
}
public Writing toUpdateEntity(){
return new Writing(no,title,content,writer,null);
}
}
๋๋ํ๊ฐ ์์ Create์์ BoardDTO์ ๋ค๋ฅธ ์ ์ด๋ค.
Updateํ ๋ ์กฐ๊ธ ์ค๋๊ฑธ๋ฆฐ ์ด์ ๊ฐ Create ๋๋ no ์ปฌ๋ผ์ด PK์๋ค๊ฐ GeneratedValue๋ก ๊ธ์ด ์์ฑ๋ ๋๋ง๋ค ์์์ ๊ฐ์ด ์์ฑ๋์ Insert ๋๊ธฐ ๋๋ฌธ์ ๋ฐ์ดํฐ ํ์ ์ ์ ๊ฒฝ์ธ ํ์๊ฐ ์์๋๋ฐ, Update์์๋ ๋ฐ์ดํฐ ํ์ ์ ๋ง์ถฐ์ค์ผ ํ๋ค.
@PostMapping("/update") public String update(@RequestParam("no") String no) { //TODO: process POST request Long num = Long.parseLong(no); BoardDTO bDto = new BoardDTO(num,title,content,writer); Writing writed = bDto.toUpdateEntity(); Writing target = writingRepo.findById(writed.getNo()).orElse(null); if(target != null){ writingRepo.save(writed); } return "redirect:/detail?no="+writed.getNo(); }
๊ทธ๋์ ์ฒ์์ ์ด๋ฐ์์ผ๋ก @RequestParam์ผ๋ก String ํ์ ์ธ no๋ฅผ ๋ฐ์์์ Long.parseLong() ํจ์๋ฅผ ์ฌ์ฉํด Long ํ์ ์ผ๋ก ๋ณํํ ํ DTO ์์ฑ์๋ก ๋ณด๋์๋ค. (์ผ๋จ ์ด๋ ๊ฒ ํด๋ ์๋์ ํจ!!)
๊ทธ๋ฌ๋ค๊ฐ ์ธ๊ด์ ๊น๋ํ ์ฝ๋๋ฅผ ์ํด ์์ ๋ฐฉ์์ผ๋ก ๋ฐ๊พผ ๊ฒ
๊ฒ์๊ธ์ ์ญ์ ํ ๋
Controller.java
@GetMapping("delpage/delete")
public ModelAndView delete(@RequestParam("no") long no){
wrService.delete(no);
ModelAndView mav = new ModelAndView();
//1. ๋ชจ๋ ๋ฐ์ดํฐ ๊ฐ์ ธ์ค๊ธฐ
List<Writing> writingEntityList = wrService.index();
//2. ๋ชจ๋ธ์ ๋ฐ์ดํฐ ๋ฑ๋กํ๊ธฐ
mav.addObject("writingList",writingEntityList);
mav.setViewName("delpage");
return mav;
}
์ฌ๊ธฐ์ ๋ฐ์ดํฐ๋ฅผ ์ ๋ ๊ฐ์ ธ์ค๋๋๋ฉด ์ญ์ ํ ํ์ ๋ฐ๋ก ๋ค์ ์ญ์ ์ ์ฉ ํ์ด์ง๋ก ์ด๋ํด์ ์ญ์ ํ ๊ธ ๋ชฉ๋ก์ ๋ด์ผํ๊ธฐ ๋๋ฌธ.
Service.java
public void delete(long no){
writingRepo.deleteById(no);
}
Update์์ ์กฐ๊ธ ์ ๋ฅผ ๋จน์์ง๋ง ๋๋จธ์ง๋ ๊ฝค ๋ฌด๋๋ฌด๋ํ ๋์ด๋์ธ๋ฏ?!
๋ค์์ผ๋ก ๋ง๋ค์ด๋ณผ๊ฑฐ๋ ์ ๊ฐ ๋ ํฌ์ผ๋ชฌ ๋ํ๋ผ ํฌ์ผ๋ชฌ ๋๊ฐ์ ํ ๋ฒ ๋ง๋ค์ด ๋ณผ๊น ์ถ์๋ฐ ํฌ์ผ๋ชฌ ์ฌ์ง์ด๋ ์ด๋ฐ๊ฑฐ ๋ค ๊ตฌํด์์ผํด์ ๋ฐ์ดํฐ ์์ง์ด ๊ฑฑ์ ๋ผ์ ์๋๋ฅผ ๋ชป ๋ด๋ ์ค.. ๊ทธ๋๋ ์ต๋ํ ๋นจ๋ฆฌ ์๋ํด๋ณด๊ฒ ์