Java/Spring Framework

[ Spring ] Mybatis 페이징 처리

Mungwang 2023. 9. 14. 09:50

💎 RowBounds

- RowBounds row = new RowBounds(offset , limit);

offset : 데이터를 가져오는 시작점에서 얼마나 떨어진 데이터인지를 적어주는것.

limit : 한페이지에 몇개의 값을 가져오는지 적어주는것.

public List<Member> selectManagementList(Pagination pagination) {

	int offset
	=(pagination.getCurrentPage() -1) * pagination.getLimit();
		
	RowBounds rowBounds = new RowBounds(offset,pagination.getLimit());

	return sqlSession.selectList("AdministratorMapper.selectManagementList", null,rowBounds);
}

내가 작성한 코드에서는 Pagination이라는 클래스를 미리 만들어둬서 쓴다!!currentPage : 현재페이지번호 Limit : 한 페이지에 보여질 게시글의 수 10개로 만들어뒀다!

   private int currentPage;      // 현재 페이지 번호
   private int listCount;         // 전체 게시글 수
   
   private int limit = 10;         // 한 페이지에 보여질 게시글의 수
   private int pageSize = 10;      // 목록 하단 페이지 번호의 노출 개수
      
   private int maxPage;         // 제일 큰 페이지 번호 == 마지막 페이지 번호
   private int startPage;         // 목록 하단에 노출된 페이지의 시작 번호
   private int endPage;         // 목록 하단에 노출된 페이지의 끝 번호
   
   private int prevPage;         // 목록 하단에 노출된 번호의 이전 목록 끝 번호
   private int nextPage;         // 목록 하단에 노출된 번호의 다음 목록 시작 번호

int offset
= (pagination.getCurrentPage() -1) * pagination.getLimit();

int offset = 1페이지라고 가정했을때 1-1 * 10 = 0 

가져온데이터에서 시작점이 0 이고 한페이지 10개씩으로 제한을 걸어뒀기때문에 10개씩보여진다.

RowBounds rowBounds = new RowBounds( offset =0 , pagination.getLimit()=10

또, 현재 페이지가 2 라고 치면 2-1 *10 = 10 이여서 2페이지에서는 시작점 10 보여질갯수 10 이렇게 되어

정상적으로 RowBounds를 이용한 페이징처리가 완료된다!  

📣 RowBounds 위치

public List<Member> selectManagementList(Pagination pagination) {

	int offset
	=(pagination.getCurrentPage() -1) * pagination.getLimit();
		
	RowBounds rowBounds = new RowBounds(offset,pagination.getLimit());

	return sqlSession.selectList("AdministratorMapper.selectManagementList",rowBounds);
}

이렇게 사용하려고했더니 결과값은다불러오고 다 잘되는데 Pagination 처리가 되지않았다... 이유를 찾아보던중파라미터값으로 rowBounds 하나만 보내면 정상적으로 작동하지 않고 만약에 보낼 데이터가없다면!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! null이라도 넣어서 rowBounds가 3번째위치에 있어야 정상작동하는걸 확인하였다

public List<Member> selectManagementList(Pagination pagination) {

	int offset
	=(pagination.getCurrentPage() -1) * pagination.getLimit();
		
	RowBounds rowBounds = new RowBounds(offset,pagination.getLimit());

	return sqlSession.selectList("AdministratorMapper.selectManagementList", null,rowBounds);
}

 

🔔 간단하게 해결할수있는문제인데 나같이 다잘되고있던걸 뜯어보면서 왜안되지하는 분들이 없었으면좋겠다..