MyBatis에서 parameter 여러 개일 경우

문제 상황

  • boardIdx, BoardApi api의 총 2개의 parameter를 Mapper에 넘기려고 하는데,
    MyBatis에서는 오직 parameter를 1개만 줄 수 있다.

해결 방법

1. parameter로 보내기 전,

먼저 boardIdx를 set을 해서 1개의 객체로 만들어주기

1) api에 boardIdx를 set한다.
2) copyProperties로 api -> entity로 복사해준다.
1
2
3
4
5
6
7
8
9
@Override
public void updateBoard(int boardIdx, BoardApi api) throws Exception {
// 1. api에 boardIdx를 set하고
// copyProperties로 api => entity로 복사해준다.
api.setBoardIdx(boardIdx);
BoardEntity entity = new BoardEntity();
BeanUtils.copyProperties(api, entity);
boardMapper.updateBoard(entity);
}

2. copyProperties로 api -> entity로 복사한 후,

entity에 boardIdx를 set 해주기

1) copyProperties로 api => entity로 복사한다.
2) entity에 boardIdx를 set 해준다.
1
2
3
4
5
6
7
8
9
@Override
public void updateBoard(int boardIdx, BoardApi api) throws Exception {
BoardEntity entity = new BoardEntity();
BeanUtils.copyProperties(api, entity);
// 2. copyProperties로 api => entity로 복사한 후,
// entity에 boardIdx를 set 해준다.
entity.setBoardIdx(boardIdx);
boardMapper.updateBoard(entity);
}