Spring 리스트 페이징 방법 1. 환경 및 DTO / Spring list paging DTO

북마크 추가

안녕하세요 리스트 페이징 하는 방법을 설명하도록 하겠습니다.

샘플로 작성할 환경은 Spring + mybatis + mysql, 화면과 관련해서는 tiles + jsp 환경입니다.


예제에서 테이블은 아래와 같이 seq, 제목, 등록일의 간단한 구조로 만들도록 하겠습니다.

CREATE TABLE `sample` (
  `seq` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `title` varchar(255) NOT NULL DEFAULT '',
  `regdate` datetime DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`seq`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;


다음으로는  DTO를 만들어 줍니다. SampleParam.java, Sample.java, Pagination.java, SampleResult.java  총 네 개를 만들 예정입니다.

1. SampleParam.java

리스트 조회 쿼리를 실행시 페이지에 해당하는 리스트를 끊어서 가져올 parameter를 넘기기 위한 DTO 입니다.

public class SampleParam {

public int displayRow;

public int offset;

public int getDisplayRow() {

return displayRow;

}

public void setDisplayRow(int displayRow) {

this.displayRow = displayRow;

}

public int getOffset() {

return offset;

}

public void setOffset(int offset) {

this.offset = offset;

}

}


2. Sample.java

조회해온 결과 리스트를 담을 DTO 입니다.

public class Sample {

public int seq;

public String title;

public String regdate;

public int getSeq() {

return seq;

}

public void setSeq(int seq) {

this.seq = seq;

}

public String getTitle() {

return title;

}

public void setTitle(String title) {

this.title = title;

}

public String getRegdate() {

return regdate;

}

public void setRegdate(String regdate) {

this.regdate = regdate;

}

}


3. Pagination.java

페이징 정보를 담을 DTO 입니다. 화면에 페이지를 1 2 3 4 5 >> ,  <<  6 7 8 9 10 >> 와 같이 그리는 용도로도 사용합니다.

public class Pagination {

private int startPage;

private int endPage;

private int currentPage;

private int lastPage;

        private int pageCount;

private int rowCount;

private int displayRow;

private int offset;

public int getStartPage() {

return startPage;

}

public void setStartPage(int startPage) {

this.startPage = startPage;

}

public int getEndPage() {

return endPage;

}

public void setEndPage(int endPage) {

this.endPage = endPage;

}

public int getCurrentPage() {

return currentPage;

}

public void setCurrentPage(int currentPage) {

this.currentPage = currentPage;

}

public int getLastPage() {

return lastPage;

}

public void setLastPage(int lastPage) {

this.lastPage = lastPage;

}

        public int getPageCount() {

                return pageCount;

        }

        public void setPageCount(int pageCount) {

                this.pageCount = pageCount;

        }

public int getRowCount() {

return rowCount;

}

public void setRowCount(int rowCount) {

this.rowCount = rowCount;

}

public int getOffset() {

return offset;

}

public void setOffset(int offset) {

this.offset = offset;

}

public int getDisplayRow() {

return displayRow;

}

public void setDisplayRow(int displayRow) {

this.displayRow = displayRow;

}

}


4. SampleResult.java

최종 결과를 화면으로 보내기 위한 DTO로 위에서 만든 Sample과 Pagination을 갖고 있습니다.

public class SampleResult {

public List<Sample> sampleList;

public Pagination pagination;

public List<Sample> getSampleList() {

return sampleList;

}

public void setSampleList(List<Sample> sampleList) {

this.sampleList = sampleList;

}

public Pagination getPagination() {

return pagination;

}

public void setPagination(Pagination pagination) {

this.pagination = pagination;

}

}


다음 글에서는 서비스 구현을 하도록 하겠습니다.



AD
관리자
2018-10-17 12:08
SHARE