Spring 페이징 처리 중 MyBatis 에러

728x90

 

//컨트롤러
List<User> userList = userService.findAllPaging(startIndex, page);
...// 서비스 , 레포지토리
...

//매퍼
@Select("SELECT * FROM user limit #{startIndex}, #{pageSize}")
    List<User> selectPaging(int startIndex, int pageSize);

DB는 MySQL을 사용하여 limit 을 통해 0, 5 로 5개의 row만 가져오고 싶었다.

실행을 해보니

 

nested exception is org.apache.ibatis.binding.BindingException: Parameter 'startIndex' not found. Available parameters are [arg1, arg0, param1, param2] 

라는 에러가 발생하였다.

 

startIndex를 찾을 수가 없다라고 한다.

 

log.info("pagination = {}", pagination);

로그를 찍어보았다.

pagination = Pagination(pageSize=5, blockSize=10, page=1, block=1, totalListCnt=28, totalPageCnt=6, totalBlockCnt=1, startPage=1, endPage=6, startIndex=0, prevBlock=1, nextBlock=6)

데이터가 아주 잘 들어있다.

 

에러를 그대로 구글에 검색해보니, MyBatis를 이용한 SQL Mapper는 메서드당 하나의 파라미터를 읽는다고 한다...

 

 

그래서 

//컨트롤러
List<User> userList = userService.findAllPaging(pagination);
...// 서비스 , 레포지토리
...

//매퍼
@Select("SELECT * FROM user limit #{startIndex}, #{pageSize}")
    List<User> selectPaging(Pagination pagination);

그냥 Pagination 객체를 직접 넘겨서 해결했다.

 

객체를 넘겨주거나 Map으로 하나로 뭉쳐서 해야 된다.

반응형