백앤드 이야기/JAVA&Spring

[JAVA] Paging 모델 및 소스 (Mybatis)

한희성 2019. 7. 7.
반응형

1. 모델 PageNavigation.java



@Data

public class PageNavigation {


private int pageNo = 1;

private int pageSize = 20;

private int blockSize = 10;

private int firstPageNo = 1;

private int lastPageNo;

private List<Integer> pageList = new ArrayList<Integer>();

private int totalCount;

private int previousPage = 1;

private int nextPage;


public PageNavigation() {

}


/**

* 1. 리스트의 전체크기 필요 

* 2. pageSize 필요 

* 3. lastPageNo = totalPage 

* 4. 전체가 0일 때 : pageSize = 1 

* 5. 전체가 나누어 안떨어질 때 : totalPage에 1개를 더해야함 

* 6. 나누어 떨어질 때 : totalPage

* @param totalCount : list의 전체 크기

* @param pageNo : 페이지 번호 ex : 1 2 3 

* @param pageSize : 한페이지당 보여주는 크기

*/

public PageNavigation(int totalCount, int pageNo, int pageSize) {


// 1. 현재 페이지 설정

this.pageNo = pageNo;

int totalPage = 1;


// 2. pageSize설정

this.setPageSize(pageSize);


totalPage = totalCount / this.pageSize;

// 나누어 떨어지지 않는 경우 1를 더해서 꽉 차지 않은 페이지도 보여야함

int extraPages = totalCount % pageSize;

if (extraPages > 0) {

totalPage += 1;

}


this.totalCount = totalCount;


// 3.전체 페이지가 0인 경우 1로 하여 빈 페이지라도 표출

if (totalCount == 0) {

totalPage = 1;

}


// 4. 버튼 설정 <<, < , > , >>

this.setLastPageNo(totalPage);

this.setFirstPageNo(pageNo, blockSize);

this.setPreviousPage(this.firstPageNo);

this.setNext(this.firstPageNo, this.lastPageNo, blockSize);


// 5. block의 시작페이지를 값으로 paging list생성

this.setPageList();


}


private void setPageSize(int pageSize) {

// 0으로 나눌 수 없으니 0일경우 1로 변경

if (pageSize <= 0) {

this.pageSize = 1;

} else {

this.pageSize = pageSize;

}

}


private void setLastPageNo(int totalPage) {

this.lastPageNo = totalPage;

}


private void setFirstPageNo(int pageNo, int blockSize) {

this.firstPageNo = (pageNo - 1) / blockSize;

this.firstPageNo *= blockSize;

this.firstPageNo += 1;

}


private void setPreviousPage(int firstPageNo) {

if (firstPageNo > 1) {

this.previousPage = firstPageNo - blockSize;

}

}


private void setNext(int firstPageNo, int lastPageNo, int blockSize) {

this.nextPage = firstPageNo + blockSize;

if (this.nextPage > lastPageNo) {

this.nextPage = lastPageNo;

}

}


private void setPageList() {

int endOfBlock = this.firstPageNo + this.blockSize;

if (endOfBlock > this.lastPageNo) {

endOfBlock = this.lastPageNo + 1;

}

for (int i = this.firstPageNo; i < endOfBlock; i++) {

this.pageList.add(i);

}

}


}


2. 컨트롤러 SiteController.java


@GetMapping("/home")

public ModelAndView index(@ModelAttribute AccountUserSearchOption accountUserSearchOption) {

ModelAndView mav = new ModelAndView("htmlstudy");


if(accountUserSearchOption.getPageNo()==0) {

accountUserSearchOption.setPageNo(1);

}

if(accountUserSearchOption.getPageSize()==0) {

accountUserSearchOption.setPageSize(20);

}

AccountUserCollection accountUserCollection = accountUserService.getAccountUserCollection(accountUserSearchOption);

mav.addObject("accountUserCollection",accountUserCollection);


return mav;

}


3. 서비스 AccountUserService.java

/**

* parameter AccountUserSearchOption

*/

public AccountUserCollection getAccountUserCollection(AccountUserSearchOption accountUserSearchOption) {

AccountUserCollection accountUserCollection = new AccountUserCollection();

//accountUserCountByPaing

int accountUserCount = accountUserMapper.selectAccountUserCount(accountUserSearchOption);

//accountUserListByPaging

List<AccountUser> accountUserList = accountUserMapper.seelctAccountUserList(accountUserSearchOption);

//pageNation

PageNavigation pageNavigation = new PageNavigation(accountUserCount, accountUserSearchOption.getPageNo(), accountUserSearchOption.getPageSize());

accountUserCollection.setAccountUserList(accountUserList);

accountUserCollection.setPageNavigation(pageNavigation);

return accountUserCollection;

}



4. 맵퍼 AccountUserMapper.java


//xml

public int selectAccountUserCount(AccountUserSearchOption accountUserSearchOption);

//xml

public List<AccountUser> seelctAccountUserList(AccountUserSearchOption accountUserSearchOption);



5. XML AccountUserMapper.xml


<select id="selectAccountUserCount" parameterType="AccountUserSearchOption" resultType="int">

SELECT COUNT(*)

  FROM account_user

WHERE 1=1

<include refid="searchOption"></include>

</select>


<select id="seelctAccountUserList" parameterType="AccountUserSearchOption" resultType="AccountUser">

SELECT *

  FROM account_user

WHERE 1=1

<include refid="searchOption"></include>

ORDER BY idx DESC

LIMIT #{limitStartNum} , #{pageSize}

</select>


<sql id="searchOption">

<if test='userId !=null and userId !=""'>

AND userId LIKE concat('%',#{userId},'%')

</if>

</sql>




반응형

댓글

💲 추천 글