- TodoListController.java
package org.zerock.jdbcex1.controller;
import lombok.extern.log4j.Log4j2;
import org.zerock.jdbcex1.dto.TodoDTO;
import org.zerock.jdbcex1.service.TodoService;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
@WebServlet(name = "todoListController", value="/todo/list")
@Log4j2
public class TodoListController extends HttpServlet {
private TodoService todoService = TodoService.INSTANCE;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
log.info("todo list..................");
try {
List<TodoDTO> dtoList = todoService.listAll();
req.setAttribute("dtoList", dtoList);
req.getRequestDispatcher("/WEB-INF/todo/list.jsp").forward(req,resp);
} catch (Exception e) {
log.error(e.getMessage());
throw new ServletException("list error");
}
}
}
1. /todo/list로 요청이 들어온다.
2. TodoService라는 객체를 열거형 INSTANCE를 이용해서 싱글톤으로 생성한다.
3. log.info를 통해 log를 찍어낸다.
- TodoService.java
package org.zerock.jdbcex1.service;
import lombok.extern.log4j.Log4j2;
import org.modelmapper.ModelMapper;
import org.zerock.jdbcex1.dao.TodoDAO;
import org.zerock.jdbcex1.domain.TodoVO;
import org.zerock.jdbcex1.dto.TodoDTO;
import org.zerock.jdbcex1.util.MapperUtil;
import java.util.List;
import java.util.stream.Collectors;
@Log4j2
public enum TodoService {
INSTANCE;
private TodoDAO dao;
private ModelMapper modelMapper;
TodoService() {
dao = new TodoDAO();
modelMapper = MapperUtil.INSTANCE.get();
}
public void register(TodoDTO todoDTO) throws Exception {
TodoVO todoVO = modelMapper.map(todoDTO, TodoVO.class);
// System.out.println("todoVO : " + todoVO);
log.info(todoVO);
dao.insert(todoVO); // int를 반환하므로 이를 이용해서 예외처리도 가능
}
public List<TodoDTO> listAll() throws Exception {
List<TodoVO> voList = dao.selectAll();
log.info("voList.....................");
log.info(voList);
List<TodoDTO> dtoList = voList.stream()
.map(vo->modelMapper.map(vo,TodoDTO.class))
.collect(Collectors.toList());
return dtoList;
}
public TodoDTO get(Long tno) throws Exception {
log.info("tno : " + tno);
TodoVO todoVO = dao.selectOne(tno);
TodoDTO todoDTO = modelMapper.map(todoVO, TodoDTO.class);
return todoDTO;
}
public void remove(Long tno) throws Exception {
log.info("tno : " + tno);
dao.deleteOne(tno);
}
public void modify(TodoDTO todoDTO) throws Exception {
log.info("todoDTO : " + todoDTO);
TodoVO todoVO = modelMapper.map(todoDTO, TodoVO.class);
dao.updateOne(todoVO);
}
}
4. 컬렉션 List를 통해 todoService.listAll() 메서드를 호출한다.
5. listAll() 메서드는 DTO에 있는 객체를 VO로 옮긴다.
- TodoVO
package org.zerock.jdbcex1.domain;
import lombok.*;
import java.time.LocalDate;
@Getter
@Builder
@ToString
@AllArgsConstructor
@NoArgsConstructor
public class TodoVO {
private Long tno;
private String title;
private LocalDate dueDate;
private boolean finished;
}
728x90
'Programming > Spring' 카테고리의 다른 글
Spring - 정리 (2023.05.26.FRI) (0) | 2023.05.26 |
---|---|
Spring - Cookie (쿠키) (0) | 2023.05.26 |
Spring - Lombok 라이브러리 (0) | 2023.05.25 |
Spring - JDBC (0) | 2023.05.25 |
Spring - war 파일 export (0) | 2023.05.02 |