페이징 조건
total 전체글 수
currentPage 현재 페이지 번호
totalPages 전체 페이지수
startPage 블록의 시작 페이지 번호
endPage 블록의 종료 페이지 번호
keyword 검색어
url 요청URL
content select 결과 데이터
pagingArea 페이징 처리
값 받기
@RequestParam(value = "",required=false,defaultValue="")
required=false ⇒ 있어도 되고 없어도 된다
defaultValue ⇒ 기본값
자동으로 타입변환 되어서 String → int 변경
만약 값이 없으면
defaultValue ="1" 설정으로 currentPage="1"로 온다
Map 으로 검색어, 페이지번호 저장
Map<String, Object> map = new HashMap<String, Object>();
map.put("keyword", keyword);
map.put("currentPage",currentPage);
전체 행의 수 구하기
controller.java
int total = this.lprodService.getTotal(map);
ServiceImpl.java
//DI,Ioc
@Inject
LprodMapper lprodMapper;
//전체 행의 수 mapper 로 요청함
@Override
public int getTotal(Map<String, Object> map) {
return this.lprodMapper.getTotal(map);
}
Mapper.java
public interface LprodMapper {
//전체 행의 수
public int getTotal(Map<String, Object> map);
}
SQL.xml
<mapper namespace="kr.or.ddit.mapper.LprodMapper">
<sql id="where">
and (LPROD_ID like
'%'||#{keyword}||'%'
or LPROD_GU like '%'||#{keyword}||'%'
or LPROD_NM like '%'||#{keyword}||'%' )
</sql>
<!-- //전체 행의 수
map:{keyword= 알탄, currentPage=1}
public int getTotal(); -->
<select id="getTotal" parameterType="hashMap" resultType="int">
SELECT COUNT(*)
FROM LPROD
WHERE 1=1
<include refid="where"></include>
</select>
</mapper>
mav.addObject("articlePage", new ArticlePage<LprodVO>(total,currentPage,10,lprodVOList,keyword));
jsp
<div class="row">
<div class="col-sm-12 col-md-7">
<div class="dataTables_paginate paging_simple_numbers"
id="example2_paginate">
<ul class="pagination">
<li class="paginate_button page-item previous
<c:if test="${articlePage.startPage lt 6}">disabled</c:if>
"
id="example2_previous"><a href="/employee/list?currentPage=${articlePage.startPage-5}" aria-controls="example2"
data-dt-idx="0" tabindex="0" class="page-link">Previous</a></li>
<c:forEach var="pNo" begin="${articlePage.startPage}" end="${articlePage.endPage}">
<li class="paginate_button page-item
<c:if test="${pNo==param.currentPage}">active</c:if>
"><a href="/employee/list?currentPage=${pNo}"
aria-controls="example2" data-dt-idx="1" tabindex="0"
class="page-link">${pNo}</a></li>
</c:forEach>
<li class="paginate_button page-item next
<c:if test="${articlePage.endPage==articlePage.totalPages}">disabled</c:if>
" id="example2_next"><a
href="/employee/list?currentPage=${articlePage.startPage+5}" aria-controls="example2" data-dt-idx="7" tabindex="0"
class="page-link">Next</a></li>
</ul>
</div>
</div>
</div>
원본
ArticlePage.java
package kr.or.ddit.utils;
import java.util.List;
public class ArticlePage<T> {
//전체글 수
private int total;
// 현재 페이지 번호
private int currentPage;
// 전체 페이지수
private int totalPages;
// 블록의 시작 페이지 번호
private int startPage;
//블록의 종료 페이지 번호
private int endPage;
//검색어
private String keyword = "";
//요청URL
private String url = "";
//select 결과 데이터
private List<T> content;
//페이징 처리
private String pagingArea = "";
//생성자(Constructor) : 페이징 정보를 생성
// 753 1 10 select결과10행
public ArticlePage(int total, int currentPage, int size, List<T> content, String keyword) {
//size : 한 화면에 보여질 목록의 행 수
this.total = total;//753
this.currentPage = currentPage;//1
this.content = content;
this.keyword = keyword;
//전체글 수가 0이면?
if(total==0) {
totalPages = 0;//전체 페이지 수
startPage = 0;//블록 시작번호
endPage = 0; //블록 종료번호
}else {//글이 있다면
//전체 페이지 수 = 전체글 수 / 한 화면에 보여질 목록의 행 수
//3 = 31 / 10
totalPages = total / size;//75
//나머지가 있다면, 페이지를 1 증가
if(total % size > 0) {//나머지3
totalPages++;//76
}
//페이지 블록 시작번호를 구하는 공식
// 블록시작번호 = 현재페이지 / 페이지크기 * 페이지크기 + 1
startPage = currentPage / 5 * 5 + 1;//1
//현재페이지 % 페이지크기 => 0일 때 보정
if(currentPage % 5 == 0) {
startPage -= 5;
}
//블록종료번호 = 시작페이지번호 + (페이지크기 - 1)
//[1][2][3][4][5][다음]
endPage = startPage + (5 - 1);//5
//종료페이지번호 > 전체페이지수
if(endPage > totalPages) {
endPage = totalPages;
}
}
pagingArea += "<div class='col-sm-12 col-md-7'>";
pagingArea += "<div class='dataTables_paginate paging_simple_numbers' id='example2_paginate'>";
pagingArea += "<ul class='pagination'>";
pagingArea += "<li class='paginate_button page-item previous ";
if(this.startPage<6) {
pagingArea += "disabled ";
}
pagingArea += "'";
pagingArea += "id='example2_previous'>";
pagingArea += "<a href='"+this.url+"?currentPage="+(this.startPage-5)+"&keyword="+this.keyword+"' aria-controls='example2' data-dt-idx='0' tabindex='0' ";
pagingArea += "class='page-link'>Previous</a></li>";
for(int pNo=this.startPage;pNo<=this.endPage;pNo++) {
pagingArea += "<li class='paginate_button page-item ";
if(this.currentPage == pNo) {
pagingArea += "active";
}
pagingArea += "'>";
pagingArea += "<a href='"+this.url+"?currentPage="+pNo+"&keyword="+this.keyword+"' aria-controls='example2' data-dt-idx='1' tabindex='0' ";
pagingArea += "class='page-link'>"+pNo+"</a>";
pagingArea += "</li>";
}
pagingArea += "<li class='paginate_button page-item next ";
if(this.endPage>=this.totalPages) {
pagingArea += "disabled";
}
pagingArea += "' id='example2_next'><a ";
pagingArea += "href='"+this.url+"?currentPage="+(this.startPage+5)+"&keyword="+this.keyword+"' aria-controls='example2' data-dt-idx='7' ";
pagingArea += "tabindex='0' class='page-link'>Next</a></li>";
pagingArea += "</ul>";
pagingArea += "</div>";
pagingArea += "</div>";
}//end 생성자
public int getTotal() {
return total;
}
public void setTotal(int total) {
this.total = total;
}
public int getCurrentPage() {
return currentPage;
}
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
public int getTotalPages() {
return totalPages;
}
public void setTotalPages(int totalPages) {
this.totalPages = totalPages;
}
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 String getKeyword() {
return keyword;
}
public void setKeyword(String keyword) {
this.keyword = keyword;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public List<T> getContent() {
return content;
}
public void setContent(List<T> content) {
this.content = content;
}
//전체 글의 수가 0인가?
public boolean hasNoArticles() {
return this.total == 0;
}
//데이터가 있나?
public boolean hasArticles() {
return this.total > 0;
}
public void setPagingArea(String pagingArea) {
this.pagingArea = pagingArea;
}
//페이징 블록을 자동화
public String getPagingArea() {
return this.pagingArea;
}
}
controller.java
package kr.or.ddit.controller;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.ObjectError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import kr.or.ddit.service.EmployeeService;
import kr.or.ddit.service.impl.EmployeeServiceImpl;
import kr.or.ddit.utils.ArticlePage;
import kr.or.ddit.vo.EmployeeVO;
import lombok.extern.slf4j.Slf4j;
@Slf4j
@RequestMapping("/employee")
@Controller
public class EmployeeController {
// @Autowired
// EmployeeVO myEmepVO;
@Autowired
EmployeeService employeeService;
@RequestMapping(value = "/list", method = RequestMethod.GET)
public ModelAndView list(ModelAndView mav
,@RequestParam(value="currentPage",required = false,defaultValue = "1") int currentPage
,@RequestParam(value = "keyword",required = false,defaultValue = "") String keyword) {
log.info("list");
log.info("keyword:"+keyword);
Map<String, Object> map = new HashMap<String, Object>();
map.put("keyword",keyword);
map.put("currentPage",currentPage);
//전체행 구하기
int total = this.employeeService.getTotal(map);
log.info("total >"+total);
List<EmployeeVO> employeeVOList = this.employeeService.list(map);
log.info("employeeVOList"+employeeVOList);
mav.addObject("articlePage", new ArticlePage<EmployeeVO>(total, currentPage, 10, employeeVOList, keyword));
mav.setViewName("employee/list");
return mav;
}
}
SQL.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="employee">
<sql id="where">
<if test ="keyword!=null and keyword!=''">
and (emp_no like '%'||#{keyword}||'%'
or emp_name like '%'||#{keyword}||'%'
or emp_address like '%'||#{keyword}||'%'
or emp_telno like '%'||#{keyword}||'%'
or filename like '%'||#{keyword}||'%' )
</if>
</sql>
<select id="list" parameterType="employeeVO" resultType="employeeVO">
SELECT T.*
FROM
(
SELECT ROW_NUMBER() OVER(ORDER BY EMP_NO) RNUM
,EMP_NO, EMP_NAME, EMP_ADDRESS, EMP_TELNO, EMP_SALARY, FILENAME
FROM EMPLOYEE
WHERE 1=1
<include refid="where"></include>
)T
WHERE T.RNUM BETWEEN ((#{currentPage}*10)-(10-1)) AND (#{currentPage}*10)
</select>
<select id="getTotal" parameterType="hashMap" resultType="int">
SELECT COUNT(*)
FROM EMPLOYEE
WHERE 1=1
<include refid="where"></include>
</mapper>
jsp
<div class="row">
<div class="col-sm-12 col-md-7">
<div class="dataTables_paginate paging_simple_numbers"
id="example2_paginate">
<ul class="pagination">
<li class="paginate_button page-item previous
<c:if test="${articlePage.startPage lt 6}">disabled</c:if>
"
id="example2_previous"><a href="/employee/list?currentPage=${articlePage.startPage-5}" aria-controls="example2"
data-dt-idx="0" tabindex="0" class="page-link">Previous</a></li>
<c:forEach var="pNo" begin="${articlePage.startPage}" end="${articlePage.endPage}">
<li class="paginate_button page-item
<c:if test="${pNo==param.currentPage}">active</c:if>
"><a href="/employee/list?currentPage=${pNo}"
aria-controls="example2" data-dt-idx="1" tabindex="0"
class="page-link">${pNo}</a></li>
</c:forEach>
<li class="paginate_button page-item next
<c:if test="${articlePage.endPage==articlePage.totalPages}">disabled</c:if>
" id="example2_next"><a
href="/employee/list?currentPage=${articlePage.startPage+5}" aria-controls="example2" data-dt-idx="7" tabindex="0"
class="page-link">Next</a></li>
</ul>
</div>
</div>
</div>
'JAVA > spring' 카테고리의 다른 글
security(임시) (0) | 2024.05.20 |
---|---|
SQL.xml 에서 join쿼리 사용하는 경우 (resultMap) (0) | 2024.05.15 |
파일업로드 (0) | 2024.05.11 |
중첩된 자바빈 (0) | 2024.05.11 |
어노테이션 (0) | 2024.05.04 |