DataBase/SQL
mysql 페이징 쿼리 & 자바 코드
프로그래멍
2020. 2. 21. 18:35
자바코드
public class Pager {
//Dao select문에 쓰기위함 between startRow and lastRow
private int startRow; //limit
private int lastRow; //mysql에선 필요없음
private int curPage; //게시판 하단 리스트번호
private int pageList; //게시물 목록들 //offset
private int perBlock; //게시판 하단 리스트번호를 몇개 보여줄지
private int curBlock; //게시판에 perBlock가 몇개 있는지
private int totalBlock; //게시판에 curBlock가 몇개 있는지
private int startNum; //
private int lastNum;
public Pager() {
this.pageList=10;
this.perBlock=5;
if(curPage==0){
this.curPage=1;
}
}
public void makeRow() {
this.startRow= (getCurPage()-1)*getPageList()+1;
this.lastRow= getCurPage()*getPageList(); //orcle db에선 필요
}
public void paging(int totalCount) {
//1. 전체 페이지 리스트 수
int totalList = totalCount/pageList; //전체 리스트 = 전체 개시물 / 한 페이지 게시물 수
if(totalCount%pageList>0) {
totalList++;
}
//2. 몇개의 블럭이 필요한지 ( totalBlock)
this.totalBlock = totalList/perBlock; //전체 블럭 = 전체 리스트 / 한페이지에 보여주는 블럭 수
if(totalList%perBlock>0) {
totalBlock++;
}
//3.총 curBlock 구하기
this.curBlock = curPage/perBlock;
if(curPage%perBlock>0) {
curBlock++;
}
//4.curBlock 로 startNum, lastNum 찾기
this.startNum = (curBlock-1)*perBlock+1;
this.lastNum = curBlock*perBlock;
//5. lastNum 찾아주기
if(curBlock==totalBlock) {
this.lastNum=totalList;
}
}
}
쿼리
<select id="findAll" parameterType="Pager" resultType="membersDto">
select * from members order by num desc limit #{startRow},#{pageList}
</select>
select * from 테이블명 order by 컬럼명 desc limit (offset), (limti);