모델-뷰-컨트롤러(model–view–controller, MVC)는 소프트웨어 공학에서 사용되는 소프트웨어 디자인 패턴이다. 이 패턴을 성공적으로 사용하면, 사용자 인터페이스로부터 비즈니스 로직을 분리하여 애플리케이션의 시각적 요소나 그 이면에서 실행되는 비즈니스 로직을 서로 영향 없이 쉽게 고칠 수 있는 애플리케이션을 만들 수 있다. MVC에서 모델은 애플리케이션의 정보(데이터)를 나타내며, 뷰는 텍스트, 체크박스 항목 등과 같은 사용자 인터페이스 요소를 나타내고, 컨트롤러는 데이터와 비즈니스 로직 사이의 상호동작을 관리한다.
출처: MVC - wikipedia

| 단계 | 데이터 형태 | 설명 |
|---|---|---|
| 클라이언트 → Controller | 요청 DTO | JSON으로 요청, DTO로 매핑 |
| Controller → Service | 요청 DTO | Service에 전달 |
| Service → Repository | Entity | DTO를 Entity로 변환 |
| Repository ↔ DB | Entity ↔ 테이블 | JPA가 자동 매핑 |
| Service ← Repository | Entity | DB 결과 반환 |
| Service → Controller | 응답 DTO | Entity를 DTO로 변환 |
| Controller → 클라이언트 | JSON | 응답 DTO를 JSON으로 반환 |
DB 테이블과 매핑되는 비즈니스 객체
Controller ↔ Service 간 데이터 전달 객체
도메인별 패키지 구조 + 하위에 역할별 패키지 구조
user
┣ 📂controller
┣ 📂service
┣ 📂model
┣ 📂entity
┣ 📂repository
board
┣ 📂controller
┣ 📂service
┣ 📂model
┣ 📂entity
┣ 📂repository


package com.example.board.controller;
@WebServlet("/board/create")
public class BoardCreateController extends HttpServlet {
private final BoardService boardService = new BoardService();
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
BoardDto.Create dto = BoardDto.Create.from(req);
Boolean result = boardService.create(dto);
}
}
package com.example.board.model;
public class BoardDto {
public static class Create {
private String title;
private String contents;
public static Create from(HttpServletRequest req) throws IOException {
ObjectMapper objectMapper = new ObjectMapper();
BoardDto.Create dto = objectMapper.readValue(req.getReader(), Create.class);
return dto;
}
public String getTitle() {
return title;
}
public String getContents() {
return contents;
}
}
}
package com.example.board.service;
public class BoardService {
private final BoardRepository boardRepository = new BoardRespositoryImplJdbc();
public Boolean create(BoardDto.Create dto) throws SQLException {
return boardRepository.save(dto) > 0;
}
}
package com.example.board.repository;
public interface BoardRepository {
Integer save(BoardDto.Create dto) throws SQLException;
BoardDto.BoardList findAll() throws SQLException;
BoardDto.Read findById(Integer idx) throws SQLException, IOException;
}
package com.example.board.repository;
public class BoardRespositoryImplJdbc implements BoardRepository {
@Override
public Integer save(BoardDto.Create dto) throws SQLException {
Statement stmt = DBUtil.getStatement();
Integer result = stmt.executeUpdate(
"INSERT INTO board (title, contents) VALUES ('"
+ dto.getTitle() + "', '"
+ dto.getContents() + "')");
return result;
}
}