내가 보려고 정리하는/Spring

웹 : 페이징, 검색: 0328

보동이용용 2023. 3. 28. 09:01
반응형

🚩오늘할일

1. 페이징과 검색기능

2. 스키마 구조(E/R/C) 중에서  constraint(제약조건)을 적용하여 검증이라는 code cost를 줄여보자.

3.어떻게 검증을 없애고, H/V을 사용할 것인가


<mapper namespace="kr.or.ddit.mybatis.mappers.Buyer"></mapper>

어떤 인터페이스를 사용할지 알려주기 위해서  프록시 생성

미션

1. 단순목록조회


++ 페이징, 검색 3

1.알고리즘 만들고

<페이징에 필요한 조건(9개) >

  • 토탈레코드(전체) / 스크린사이즈(한페이지의 글) /  ->페이징 전체 몇페이지인지 구하기
  • currentpage(현재페이지) : startrow(시작글)~endrow(끝글) ->스크린사이즈로 연산
  • blocksize(한화면에 나오는 페이지수) : startPage(시작페이지)~endPage(끝페이지) ->currentPage로 연산

>> 설정 방법에 따라 3종류로 나뉨.

- TotalRecord : DB다녀와야함. (토탈레코드 쿼리문 추가)

- ScreenSize, BlockSize : 개발자가 지정

- CurrentPage : Client의 Parameter로 결정

미션 : 연산식을 찾아내라

 

totalPage ( TotalRecord  + (ScreenSize - 1) ) / ScreenSize

TotalRecord 가 ScreenSize의 배수라면 , 그렇지 않으면 보수 찾기 

n의 최대 보수는 n-1

startPage  endPage  - (  BlockSize  - 1 )

endPage  CurrentPage + ( BlockSize  - 1 ) / BlockSize  *  BlockSize 

startRow  endRow  - (ScreenSize -1)

endRow  CurrentPage  * ScreenSize 


1.설계

2. 쿼리문 작성하고

3.member.xml , memberDAO, memberDAOImpl 수정한다.

//DB
private int totalRecord;	

//개발자 지정
private int screenSize=10; 
private int blockSize=5;    			

//parameter
private int currentPage;	

//연산
private int totalPage;   

private int startRow;
private int endRow;

private int startPage;
private int endPage;

페이징에 필요한 것들을 Pagination에 정의해놓는다.

개발자가 지정하는 것은 setter를 만들지 않았다. 대신 필요할 때는 생성자를 통해 수정할 것이다.

연산을 통해 받아오는 것은 getter를 만들지 않는다. 왜냐하면, 

totalPage는 db에서 totalRecord 를 받아오면 연산을 수행할 수 있기 때문이고,

startRow, endRow, startPage, endPage도 currentPage를 파라미터로 받아와야 연산을 수행할 수 있기 때문이다.

public void setTotalRecoard(int totalRecoard) {
    this.totalRecoard = totalRecard;
    totalPage = (totalRecoard + (screenSize-1)) / screenSize;
}

public void setCurrentPage(int currentPage) {
    this.currentPage = currentPage;
    endRow = currentPage * screenSize;
    startRow = endRow - ( screenSize - 1 );
    endPage = ((currentPage + ( blockSize - 1 )) / blockSize) * blockSize;
    startPage = endPage - ( blockSize - 1 );
}

첫번째 setter

전체 글 수를 db에서 받아오면 totalRecord 를 연산하여 설정하고

두번째 setter

currentPage파라미터를 받아오면 startRow, endRow, startPage, endPage 를 연산하여 설정할 수 있다.


public interface PaginationRenderer {
	public String renderPagination(Pagination pagination);
}

PaginationRenderer 인터페이스를 생성하고 

public class DefaultPaginationRenderer implements PaginationRenderer {

	private final String PATTERN="<a href=':;' onclick='return fn_paging(%d, event);'>%s</a>";
	
	@Override
	public String renderPagination(Pagination pagination) {
		int startPage = pagination.getStartPage();
		int endPage = pagination.getEndPage();
		int totalPage = pagination.getTotalPage();
		int lastPage = endPage > totalPage ? totalPage : endPage;
		int blockSize = pagination.getBlockSize();
		
		StringBuffer html = new StringBuffer();
		if(startPage > blockSize) {
			html.append(
				String.format(PATTERN, startPage - blockSize , "이전")
			);
		}
		for(int page = startPage; page <= lastPage; page++) {
			if(page == pagination.getCurrentPage()) {
				html.append(String.format("<span>%d</sapn>", page));
			}else {
				html.append(
						String.format(PATTERN, page, page)
				);
			}
		}
		
		if(lastPage < totalPage) {
			html.append(
				String.format(PATTERN, lastPage + 1 , "다음")
			);
		}
		
		return html.toString();
	}

}

구현체를 만들어서 페이징 UI를 만든다. 이것은 동적으로 변화되어야해서 java로 작성하였다.

 


검색

검색조건 : 이름, 사는지역, 이름 + 사는지역

https://commons.apache.org/proper/commons-ognl/language-guide.html

 

OGNL - Apache Commons OGNL - Language Guide

<!-- Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You under the Apa

commons.apache.org

 

스태틱 메서드는 객체가 아닌 클래스가 갖고 있는 메서드 이다.

 

미션

거래처 분류의 이름으로 검색하기

 

 

반응형

'내가 보려고 정리하는 > Spring' 카테고리의 다른 글

웹 : 필터와 이진데이터, 보안 : 0330  (0) 2023.03.30
웹 : H/V로 검증하기 : 0329  (0) 2023.03.29
웹 : DomainLayer myBatis 적용 : 0327  (0) 2023.03.27
웹 : 0324  (0) 2023.03.24
웹 : MAVEN : 0323(2)  (2) 2023.03.23