๐Ÿง‘โ€๐Ÿ’ป [Spring Boot] CRUD ์ •๋ฆฌ

J_m2nยท2024๋…„ 2์›” 19์ผ
0


Spring Boot ํ”„๋กœ์ ํŠธ์—์„œ JPA ์‚ฌ์šฉ ์‹œ CRUD ์ •๋ฆฌ


์˜ˆ์ „์— ๊ต์œก ๋•Œ 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;

}

1. Create

๊ฒŒ์‹œํŒ ํ”„๋กœ์ ํŠธ๋ฅผ ์˜ˆ์‹œ๋กœ ๋“ค์ž๋ฉด 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๋ฅผ ์‚ฌ์šฉํ•ด๋„ ๋  ๋“ฏ!



2. Read

๊ฒŒ์‹œํŒ์—์„œ๋Š” Read๋Š” ๊ธ€ ๋ชฉ๋ก์„ ๋ถˆ๋Ÿฌ์˜ค๊ฑฐ๋‚˜ ํ•ด๋‹น ๊ธ€์„ ์ž์„ธํžˆ ๋ณผ ๋•Œ!

main.jsp์—์„œ ์ „์ฒด ๊ธ€ ๋ชฉ๋ก์„ ๋ถˆ๋Ÿฌ์˜ฌ ๋•Œ

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);

3. Update

๊ฒŒ์‹œ๊ธ€ ์ˆ˜์ •ํ•  ๋•Œ

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 ์ƒ์„ฑ์ž๋กœ ๋ณด๋ƒˆ์—ˆ๋‹ค. (์ผ๋‹จ ์ด๋ ‡๊ฒŒ ํ•ด๋„ ์ž‘๋™์€ ํ•จ!!)
๊ทธ๋Ÿฌ๋‹ค๊ฐ€ ์™ธ๊ด€์ƒ ๊น”๋”ํ•œ ์ฝ”๋“œ๋ฅผ ์œ„ํ•ด ์œ„์˜ ๋ฐฉ์‹์œผ๋กœ ๋ฐ”๊พผ ๊ฒƒ


4. Delete

๊ฒŒ์‹œ๊ธ€์„ ์‚ญ์ œํ•  ๋•Œ

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์—์„œ ์กฐ๊ธˆ ์• ๋ฅผ ๋จน์—ˆ์ง€๋งŒ ๋‚˜๋จธ์ง€๋Š” ๊ฝค ๋ฌด๋‚œ๋ฌด๋‚œํ•œ ๋‚œ์ด๋„์ธ๋“ฏ?!

๋‹ค์Œ์œผ๋กœ ๋งŒ๋“ค์–ด๋ณผ๊ฑฐ๋Š” ์ œ๊ฐ€ ๋˜ ํฌ์ผ“๋ชฌ ๋•ํ›„๋ผ ํฌ์ผ“๋ชฌ ๋„๊ฐ์„ ํ•œ ๋ฒˆ ๋งŒ๋“ค์–ด ๋ณผ๊นŒ ์‹ถ์€๋ฐ ํฌ์ผ“๋ชฌ ์‚ฌ์ง„์ด๋ž‘ ์ด๋Ÿฐ๊ฑฐ ๋‹ค ๊ตฌํ•ด์™€์•ผํ•ด์„œ ๋ฐ์ดํ„ฐ ์ˆ˜์ง‘์ด ๊ฑฑ์ •๋ผ์„œ ์—„๋‘๋ฅผ ๋ชป ๋‚ด๋Š” ์ค‘.. ๊ทธ๋ž˜๋„ ์ตœ๋Œ€ํ•œ ๋นจ๋ฆฌ ์‹œ๋„ํ•ด๋ณด๊ฒ ์Œ

profile
์ฝ”๋”ฉ ์ดˆ์งœ์ž…๋‹ˆ๋‹ค

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