[Project] map -> VO -> 완전한 VO로 변환하기

이슬기·2024년 1월 21일
0

project

목록 보기
12/42

지난 번에 게시판 만들기 프로젝트를 했을 때 map으로 DB를 불러오던 걸 VO로 바꿨으나 매개변수로 VO를 보내면서 어중간한 VO 사용하기만 했었다.
하지만 이번에는 VO를 제네릭 타입으로 List< VO > 사용하도록 수정해보았다.

이렇게 수정하고 PostMan에서 단위 테스트를 해보면 기존과는 다르게 알아서 JSON 형식으로 출력되는 것을 알 수 있다. VO를 타입으로 사용하면 따로 JSON 처리를 하지 않아도 되는 것이다.

Controller, Logic, Dao를 아래와 같이 수정하면 PostMan으로 단위 테스트를 했을 때 JSON 형식을 갖춰 출력되는 것을 볼 수 있다.

완전한 VO로 변환 (1)

👇 VO

@Data
@EntityScan
@NoArgsConstructor
public class EmpVO {
    @ExcelHeader(name = "사원번호")
    private int e_code;
    @ExcelHeader(name = "이름")
    private String e_name;
    @ExcelHeader(name = "성별")
    private String e_gender;
    @ExcelHeader(name = "전화번호")
    private String e_phone;

    @Builder
    public EmpVO(int e_code, String e_name, String e_gender, String e_phone) {
        this.e_code = e_code;
        this.e_name = e_name;
        this.e_gender = e_gender;
        this.e_phone = e_phone;
    }

    @Override
    public String toString() {
        return "EmpVO{" +
                "e_code=" + e_code +
                ", e_name='" + e_name + '\'' +
                ", e_gender='" + e_gender + '\'' +
                ", e_phone='" + e_phone + '\'' +
                '}';
    }
}

👇 Controller

@RestController
@RequestMapping("/emp/*")
@RequiredArgsConstructor
public class EmpController {
    Logger logger = LoggerFactory.getLogger(EmpController.class);
    @Autowired
    private EmpLogic empLogic;

    // 직원 조회
    @GetMapping("empList")
    public List<EmpVO> empList() {
        logger.info("empList");
        List<EmpVO> eList = null;
        eList = empLogic.empList();
        return eList;
    }

👇 Logic

@Service
@Transactional
@RequiredArgsConstructor
public class EmpLogic {
    Logger logger = LoggerFactory.getLogger(EmpLogic.class);
    @Autowired
    private EmpDao empDao;

    public List<EmpVO> empList() {
        logger.info("empList");
        List<EmpVO> nList = null;
        nList = empDao.empList();
        return nList;
    }

👇 Dao

@Repository
public class EmpDao {
    Logger logger = LoggerFactory.getLogger(EmpDao.class);
    @Autowired
    private SqlSessionTemplate sqlSessionTemplate;
    public List<EmpVO> empList() {
        logger.info("empList");
        List<EmpVO> nList = sqlSessionTemplate.selectList("getEmpList");
        logger.info(nList.toString());
        return nList;
    }

👇 emp.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    <!--==========================[[ VO 사용 시작 ]]===================================-->
    <resultMap id="empVO" type="com.sg.back.lsg.vo.EmpVO">
        <id property="e_code" column="e_code" />
        <result property="e_name" column="e_name" />
        <result property="e_gender" column="e_gender" />
        <result property="e_phone" column="e_phone" />
    </resultMap>
    <select id="getEmpList" resultMap="empVO">
        SELECT e_code, e_name, e_gender, e_phone
        FROM emp240119
    </select>
    <!--==========================[[ VO 사용 끝 ]]===================================-->
</mapper>

👇 PostMan 단위 테스트

[
    {
        "e_code": 1,
        "e_name": "김자바",
        "e_gender": "남",
        "e_phone": "01012123434"
    },
    {
        "e_code": 2,
        "e_name": "권도커",
        "e_gender": "여",
        "e_phone": "01045621358"
    },
    {
        "e_code": 3,
        "e_name": "바나나",
        "e_gender": "여",
        "e_phone": "01098546213"
    }
]

완전한 VO로 변환 (2)

Controller를 좀 더 Controller의 역할만 하여 완전한 분리를 시켰다.

Contoroller

컨트롤러는 주로 HTTP 요청에 대한 응답을 처리하는 역할을 한다. 따라서 컨트롤러에서는 비즈니스 로직을 직접 수행하는 것이 아니라 로직 계층에 위임하여 비즈니스 로직을 처리하는 것이 좋다.

@RestController
@RequestMapping("/emp/*")
@RequiredArgsConstructor
public class EmpController {
    private final EmpLogic empLogic;

    Logger logger = LoggerFactory.getLogger(EmpController.class);

    // 직원 조회
    @GetMapping("empList")
    public List<EmpVO> empList() {
        logger.info("empList");
        return empLogic.empList();
    }
}

Logic

로직에서는 주로 비즈니스 로직을 수행하며, 트랜잭션을 관리하는 역할을 한다.

Dao

다오는 주로 데이터베이스와의 상호 작용을 담당한다. 여기에서는 마이바티스를 사용하여 SQL 쿼리를 실행하고 결과를 반환한다.

엑셀 파일 다운로드가 가능해짐!

이전 어설픈 VO로 변환하여 DB 값을 출력할 때는 엑셀 파일 다운로드를 하려니 제대로 실행되지 않았다. 하지만 완전한 VO로 변환했기 때문에 이제 엑셀 파일을 다운로드 받을 수 있게 되었다. 이에 대한 내용은 다음 글에서 다루고자 한다.

0개의 댓글

관련 채용 정보