최종 변경 : 2023.08.19


2.png


프로젝트 계층

3.png


코드

package com.example.demo.controller;

import com.example.demo.dto.SaveDTO;
import com.example.demo.service.ContentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;

@Controller
public class SaveController {

    private ContentService contentService;

    @Autowired
    public SaveController(ContentService contentService) {

        this.contentService = contentService;
    }

    @PostMapping("/save")
    public String saveLogic(SaveDTO saveDTO) {

        contentService.saveContent(saveDTO);

        return "redirect:/";
    }
}
package com.example.demo.service;

import com.example.demo.dto.SaveDTO;
import com.example.demo.entity.ContentEntity;
import com.example.demo.repository.ContentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ContentService {

    private ContentRepository contentRepository;

    @Autowired
    public ContentService(ContentRepository contentRepository) {

        this.contentRepository = contentRepository;
    }

    public void saveContent(SaveDTO saveDTO) {

        String title = saveDTO.getTitle();
        String content = saveDTO.getContent();

        ContentEntity content1 = new ContentEntity();

        content1.setTitle(title);
        content1.setContent(content);

        contentRepository.save(content1);

        return;
    }
}