관리 메뉴

moozi

Controller 본문

Spring Boot JPA

Controller

moozi 2022. 5. 30. 15:27
package com.mycompany.memo.controller;

import com.mycompany.memo.dto.MemoDTO;
import com.mycompany.memo.service.MemoService;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
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.PostMapping;

import java.util.List;

@Controller
@Log4j2
@RequiredArgsConstructor
public class MemoController {

    //자동주입
     private final MemoService service;

    //목록
    @GetMapping("/list")
    public void list(Model model){
        //서비스호출
        List<MemoDTO> list=service.getList();
        // view페이지로 전달
        model.addAttribute("result", list); // result 라는 이름으로 전달
    }
    //등록
    @GetMapping("/register")
    public void register(){};
    @PostMapping("/register")
    public String register(MemoDTO dto){
        log.info("@@@@@@@@@@ MemoDTO @@@@@@@@@@@@@ : "+dto); // view에서 전달된 값이 dto에 자동수집됨.
        //서비스호출
        service.register(dto);
        return "redirect:/list";
    }
    //상세보기
    @GetMapping({"/read","/modify"})
    public void read(Long mno,Model model){
        log.info("@@@@@@@@@@@@@@@ mno @@@@@@@@@@@@@@@ : " + mno);
        //서비스호출
        MemoDTO dto=service.read(mno);
        model.addAttribute("dto",dto);
    }
    //수정하기
    @PostMapping("/modify")
    public String modify(MemoDTO dto){
        //서비스호출
        service.modify(dto);
        return "redirect:/read?mno="+dto.getMno();
    }
    //삭제하기
    @GetMapping("/remove")
    public String remove(Long mno){
        //서비스호출
        service.remove(mno);
        return "redirect:/list";
    }
}

'Spring Boot JPA' 카테고리의 다른 글

VS Code live reload  (0) 2022.06.24
View  (0) 2022.05.30
ServiceImpl  (0) 2022.05.30
Service  (0) 2022.05.30
Repository  (0) 2022.05.30
0 Comments
댓글쓰기 폼