研修54日目


検索


ページング-データ・パーティション



ページ開始:(現在のp-1)*個数+1を表示
ページ終了:現在のp*表示数
最大p個:投稿総数%(残り)表示する数
  • 合計投稿数が0の場合:合計投稿数/表示された投稿数
  • 0の場合:(合計投稿数/表示数)+1
  • 0時:1ページ目に残ります.
    現在のPからP現在%表示のP 24579182
  • 0時:現在表示されているP+1
  • 0の場合:(現在P/表示のP)+表示のP+1
    現在のP条件Pを終了
    P+表示開始P-1
    ただし、求める値が最大Pより大きい場合、P=最大P
  • を終了する.

    ページング手順



    難しすぎます...馴染みがない.慣れるまで勉強を繰り返します!!
    コード#コード#
    TestController
    package com.spring.sample.web.test.controller;
    
    import java.util.HashMap;
    import java.util.List;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.servlet.ModelAndView;
    
    import com.spring.sample.common.bean.PagingBean;
    import com.spring.sample.common.service.IPagingService;
    import com.spring.sample.web.test.service.ITestService;
    
    @Controller
    public class TestController {
    
    	//객체주입 받겠다.
    	@Autowired
    	public IPagingService iPagingService;
    	
    	@Autowired
    	public ITestService iTestService;
    	
    	@RequestMapping(value="/test1")
    	
    	public ModelAndView test1(
    			@RequestParam HashMap<String, String> params,
    			ModelAndView mav) throws Throwable {
    		
    		//현재 페이지
    		
    		int page = 1; 
    		if(params.get("page") != null) {
    			page = Integer.parseInt(params.get("page"));
    		}
    		// 총 게시글 수
    		int cnt = iTestService.getBCnt(params);
    		
    		//페이징 정보 취득
    		PagingBean pb = iPagingService.getPagingBean(page, cnt);
    		//글번호 P 없으면 글번호
    		//게시글 시작번호, 종료 번호 할당
    		params.put("startCnt", Integer.toString(pb.getStartCount()));
    		params.put("endCnt" , Integer.toString(pb.getEndCount()));
    		
    		//목록 취득
    		List<HashMap<String, String>>list
    					= iTestService.getBList(params);
    		
    		mav.addObject("list", list);
    		mav.addObject("pb", pb);
    		mav.addObject("page", page);
    		mav.addObject("cnt", cnt);
    		mav.setViewName("test/test1");
    	
    		return mav;
    		
    	}
    	
    	
    //	@RequestMapping(value="/testMList")
    //	public ModelAndView testMList(ModelAndView mav) throws Throwable{
    //		
    //		List<HashMap<String, String>>list1
    //							= iTestService.getMList();
    //		
    //		mav.addObject("list", list1);
    //		
    //		mav.setViewName("test/testMList");
    //		
    //		return mav;
    //	}
    	//상세보기
    	@RequestMapping(value="/test2")
    	public ModelAndView tes2(
    			@RequestParam HashMap<String, String> params,
    			ModelAndView mav) throws Throwable{
    		if(params.get("bNo") !=null) {
    			//단일 컬럼은 해쉬맵으로 갖고온다
    			HashMap<String, String> data
    			=iTestService.getB(params);
    			
    			mav.addObject("data", data);
    			
    			mav.setViewName("test/test2");
    			
    		} else {
    			//redirect: 주소 => 해당주소로 이동. 즉, 컨트롤러에서 컨트롤러로 이동
    			//					get방식만 적용됨
    			mav.setViewName("redirect:test1");
    		}
    		
    		return mav;
    	}
    
    	@RequestMapping(value="/ajaxTest")
    	public  ModelAndView ajaxtest(ModelAndView mav) {
    		
    		mav.setViewName("test/ajaxTest");
    		return mav;
    	}
    	//등록
    	@RequestMapping(value="/test3")
    	public ModelAndView test3(ModelAndView mav) {
    		mav.setViewName("test/test3");
    		
    		return mav;
    	}
    	
    	@RequestMapping(value="/test3s")
    	public ModelAndView test3s(
    			@RequestParam HashMap<String, String> params,
    			ModelAndView mav) {
    	
    			
    			try {
    				int cnt = iTestService.addB(params);
    				
    				if(cnt > 0 ) {
    					 mav.setViewName("redirect:test1");
    				} else {
    					 mav.addObject("msg", "등록실패");
    					 mav.setViewName("test/test3s");
    				}
    				
    			} catch (Throwable e) {
    				e.printStackTrace();
    				 mav.addObject("msg", "오류발생");
    				 mav.setViewName("test/test3s");
    			}
    			
    		
    		return mav;
    	}
    	//수정
    	@RequestMapping(value="/test4")
    	public ModelAndView test4(
    			@RequestParam HashMap<String, String> params,
    			ModelAndView mav) throws Throwable{
    		if(params.get("bNo") !=null) {
    			//단일 컬럼은 해쉬맵으로 갖고온다
    			HashMap<String, String> data=iTestService.getB(params);
    			
    			mav.addObject("data", data);
    			
    			mav.setViewName("test/test4");
    			
    		} else {
    			//redirect: 주소 => 해당주소로 이동. 즉, 컨트롤러에서 컨트롤러로 이동
    			//					get방식만 적용됨
    			mav.setViewName("redirect:test1");
    		}
    		
    		return mav;
    	}
    	
    	@RequestMapping(value="/test4s")
    	public ModelAndView test4s(
    			@RequestParam HashMap<String, String> params,
    			ModelAndView mav) {
    	
    			
    			try {
    				int cnt = iTestService.updateB(params);
    				
    				mav.addObject("cnt", cnt);
    					
    			} catch (Throwable e) {
    				e.printStackTrace();
    				mav.addObject("msg", "오류발생");
    			}
    			
    			mav.setViewName("test/test4s");
    		
    		return mav;
    	}
    	
    	@RequestMapping(value="/test5s")
    	public ModelAndView test5s(
    			@RequestParam HashMap<String, String> params,
    			ModelAndView mav) {
    	
    			
    			try {
    				int cnt = iTestService.deleteB(params);
    				
    				if(cnt > 0 ) {
    					 mav.setViewName("redirect:test1");
    				} else {
    					 mav.addObject("msg", "등록실패");
    					 mav.setViewName("test/test5s");
    				}
    				
    			} catch (Throwable e) {
    				e.printStackTrace();
    				 mav.addObject("msg", "오류발생");
    				 mav.setViewName("test/test5s");
    			}
    			
    		
    		return mav;
    	}
    }
    
    ITestService
    package com.spring.sample.web.test.service;
    
    import java.util.HashMap;
    import java.util.List;
    
    public interface ITestService {
    
    	public List<HashMap<String, String>> getBList(HashMap<String, String> params) throws Throwable;
    
    	public HashMap<String, String> getB(HashMap<String, String> params) throws Throwable;
    
    	public int addB(HashMap<String, String> params) throws Throwable;
    
    	public int updateB(HashMap<String, String> params) throws Throwable;
    
    	public int deleteB(HashMap<String, String> params) throws Throwable;
    
    	public int getBCnt(HashMap<String, String> params) throws Throwable;
    }
    
    TestService
    import java.util.HashMap;
    import java.util.List;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    
    import com.spring.sample.web.test.dao.ITestDao;
    
    @Service
    public class TestService implements ITestService {
    
    	//객체 주입 받겠다.
    	@Autowired
    	public ITestDao iTestDao;
    	
    	@Override
    	public List<HashMap<String, String>> getBList(HashMap<String, String> params) throws Throwable {
    		
    		return iTestDao.getBList(params);
    	}
    
    	@Override
    	public HashMap<String, String> getB(HashMap<String, String> params) throws Throwable {
    		
    		return iTestDao.getB(params);
    	}
    
    	@Override
    	public int addB(HashMap<String, String> params) throws Throwable {
    		
    		return iTestDao.addB(params);
    	}
    
    	@Override
    	public int updateB(HashMap<String, String> params) throws Throwable {
    		// TODO Auto-generated method stub
    		return iTestDao.updateB(params);
    	}
    
    	@Override
    	public int deleteB(HashMap<String, String> params) throws Throwable {
    		return iTestDao.deleteB(params);
    	}
    
    	@Override
    	public int getBCnt(HashMap<String, String> params) throws Throwable {
    		return iTestDao.getBCnt(params);
    	}
    
    }
    
    ITestDao
    package com.spring.sample.web.test.dao;
    
    import java.util.HashMap;
    import java.util.List;
    
    public interface ITestDao {
    	
    	public List<HashMap<String, String>> getBList(HashMap<String, String> params) throws Throwable;
    
    	public HashMap<String, String> getB(HashMap<String, String> params) throws Throwable;
    
    	public int addB(HashMap<String, String> params) throws Throwable;
    
    	public int updateB(HashMap<String, String> params) throws Throwable;
    
    	public int deleteB(HashMap<String, String> params) throws Throwable;
    
    	public int getBCnt(HashMap<String, String> params)throws Throwable;
    }
    
    TestDao
    package com.spring.sample.web.test.dao;
    
    import java.util.HashMap;
    import java.util.List;
    
    import org.apache.ibatis.session.SqlSession;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Repository;
    
    //저장소에 접근한다.
    @Repository
    public class TestDao implements ITestDao {
    
    	@Autowired
    	public SqlSession sqlSession;
    
    	@Override
    	public List<HashMap<String, String>> getBList(HashMap<String, String> params) throws Throwable {
    		
    		return sqlSession.selectList("B.getBList", params);
    	}
    
    	@Override
    	public HashMap<String, String> getB(HashMap<String, String> params) throws Throwable {
    		//단일row selectOne
    		return sqlSession.selectOne("B.getB",params);
    	}
    
    	@Override
    	public int addB(HashMap<String, String> params) throws Throwable {
    		
    		return sqlSession.insert("B.addB",params);
    	}
    
    	@Override
    	public int updateB(HashMap<String, String> params) throws Throwable {
    		return sqlSession.update("B.updateB", params);
    	}
    
    	@Override
    	public int deleteB(HashMap<String, String> params) throws Throwable {
    		return sqlSession.delete("B.deleteB", params);
    	}
    
    	@Override
    	public int getBCnt(HashMap<String, String> params) throws Throwable {
    		return sqlSession.selectOne("B.getBCnt", params);
    	}
    	
    }
    
    B_SQL
    <?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="B"><!-- namespace: 클래스명과동일 -->
    	<!-- id: 메소드명과 동일 -->
    	<!-- resultType: row 1줄의 형태를 지정 -->
    	<!-- 쿼리 작성 시 ; 이 들어가면 실행 되지 않음 -->
    	<select id="getBCnt" parameterType="hashmap" resultType="Integer">
    	SELECT COUNT(*) AS CNT
    	FROM B
    	<!-- where은 무조건 붙이고 시작, 조건에 따라서 쿼리가 바뀌기 때문에 동적sql -->
    	WHERE 1 = 1
    	<if test="searchTxt !=null and searchTxt != ''">
    		<choose>
    			<when test="searchGbn == 0">
    				AND B_TITLE LIKE '%' || #{searchTxt} || '%'
    			</when>
    			<when test="searchGbn == 1">
    				AND B_WRITER LIKE '%' ||  #{searchTxt} || '%'
    			</when>
    			<when test="searchGbn == 2">
    				AND B_NO LIKE '%' ||  #{searchTxt} || '%'
    			</when>
    		</choose>
    	</if>
    	</select>
    	
    	<select id="getBList" parameterType="hashmap" resultType="hashmap">
    		SELECT B.B_NO, B.B_TITLE,B.B_WRITER,B.B_DT
    		FROM (SELECT B_NO, B_TITLE, B_WRITER, TO_CHAR(B_DT, 'YYYY-MM-DD')AS B_DT,
    	             ROW_NUMBER() OVER(ORDER BY B_NO DESC) AS RNUM
    	     	  FROM B
    	    WHERE 1 = 1
    	<if test="searchTxt != null and searchTxt != ''">
    		<choose>
    			<when test="searchGbn == 0">
    				AND B_TITLE LIKE '%' || #{searchTxt} || '%'
    			</when>
    			<when test="searchGbn == 1">
    				AND B_WRITER LIKE '%' || #{searchTxt} || '%'
    			</when>
    			<when test="searchGbn == 2">
    				AND B_NO LIKE '%' || #{searchTxt} || '%'
    			</when>
    		</choose>
    	</if>
    	  ) B
    	  WHERE B.RNUM BETWEEN #{startCnt} AND #{endCnt}
    	</select>
    	
    	<!-- parameterType은 받는 값타입에 대한 것 resultType: 쿼리결과타입에 대한 것 -->
    	<select id="getB" parameterType="hashmap" resultType="hashmap">
    		SELECT B_NO, B_TITLE, B_WRITER, B_CON, TO_CHAR(B_DT, 'YYYY--MM--DD') AS B_DT
    		FROM B
    		WHERE B_NO = #{bNo}
    	</select>
    	<!-- 값만 넣어주면 되기 때문에 resultType이 필요없음  -->
    	<insert id="addB" parameterType="hashmap">
    		INSERT INTO B(B_NO, B_TITLE,B_WRITER, B_CON)
    		VALUES(B_SEQ.NEXTVAL, #{bTitle}, #{bWriter}, #{bCon})
    	</insert>
    	
    	<update id="updateB" parameterType="hashmap">
    		UPDATE B SET B_TITLE= #{bTitle}, B_CON = #{bCon}
    		WHERE B_NO = #{bNo}
    	</update>
    	
    	<delete id="deleteB" parameterType="hashmap">
    		DELETE FROM B
    		WHERE B_NO = #{bNo}
    	</delete>
    </mapper>
    test1
    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <%@ taglib prefix="c"  uri="http://java.sun.com/jsp/jstl/core"%> 
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>Insert title here</title>
    <style type="text/css">
    thead{
    	background-color: orange;
    }
    </style>
    <script type="text/javascript"
    		src="resources/script/jquery/jquery-1.12.4.min.js">
    </script>
    <script type="text/javascript">
    $(document).ready(function(){
    	$("tbody").on("click", "tr", function(){
    		$("#bNo").val($(this).attr("name")); //여기서 this는 클릭한 tr
    		$("#goForm").submit();
    	}); //tbody end
    	
    	$("#addBtn").on("click", function(){
    		location.href = "test3";
    	});//addBtn end
    	
    	$("#searchBtn").on("click",function(){
    			$("#page").val(1);
    			$("#searchForm").submit();
    	});
    	
    	$("#pagingWrap").on("click", "span",function(){
    		$("#page").val($(this).attr("name"));
    		$("#searchForm").submit();
    	});
    }); //ready end
    </script>
    </head>
    <body>
    <form action="test2" id="goForm" method="post">
    	<input type="hidden" id="bNo" name="bNo"/>
    </form>
    <form action="test1" id="searchForm" method="post">
    <input type="hidden" id="page" name="page" value="${page}"/>
    <select name="searchGbn">
    	<c:choose>
    		<c:when test="${param.searchGbn eq 0 }">
    			<option value="0" selected="selected">제목</option>
    		</c:when>
    		<c:otherwise>
    			<option value="0">제목</option>
    		</c:otherwise>
    	</c:choose>
    	<c:choose>
    		<c:when test="${param.searchGbn eq 1 }">
    			<option value="1" selected="selected">작성자</option>
    		</c:when>
    		<c:otherwise>
    			<option value="1">작성자</option>
    		</c:otherwise>
    	</c:choose>
    	<c:choose>
    		<c:when test="${param.searchGbn eq 2 }">
    			<option value="2" selected="selected">글번호</option>
    		</c:when>
    		<c:otherwise>
    			<option value="2">글번호</option>
    		</c:otherwise>
    	</c:choose>
    </select>
    <input type="text" name="searchTxt" value="${param.searchTxt}"/>
    <input type="button" value="검색" id="searchBtn"/>
    <input type="button" value="작성" id="addBtn"/>
    </form>
    <table>
    	<thead>
    		<tr>
    			<th>번호</th>
    			<th>제목</th>
    			<th>작성자</th>
    			<th>작성일</th>
    		</tr>
    	</thead>
    	<tbody>
    		<c:forEach var="data" items="${list}">
    			<tr name="${data.B_NO}">
    				<td>${data.B_NO}</td>
    				<td>${data.B_TITLE}</td>
    				<td>${data.B_WRITER}</td>
    				<td>${data.B_DT}</td>
    			</tr>
    		</c:forEach>
    	</tbody>
    </table>
    <div id="pagingWrap">
    <!-- 이전페이지 -->
    <span name="1">처음</span>
    <c:choose>
    	<c:when test="${page eq 1}">
    		<span name="1">이전</span>
    	</c:when>
    	<c:otherwise>
    		<span name="${page-1}">이전</span>
    	</c:otherwise>
    </c:choose>
    <!-- 페이지들 -->
    <c:forEach var="i" begin="${pb.startPcount}" end="${pb.endPcount}" step="1">
    	<!-- 현재 페이지인 경우 볼드 처리 -->
    	<c:choose>
    		<c:when test="${i eq page}">
    			<span name="${i}"><b>${i}</b></span>
    		</c:when>
    		<c:otherwise>
    			<span name="${i}">${i}</span>
    		</c:otherwise>
    	</c:choose>
    </c:forEach>
    <!-- 다음페이지 -->
    <c:choose>
    	<c:when test="${page eq pb.maxPcount}">
    		<span name="${pb.maxPcount}"><b>다음</b></span>	
    	</c:when>
    	<c:otherwise>
    		<span name="${page+ 1}">다음</span>
    	</c:otherwise>
    </c:choose>
    <span name="${pb.maxPcount}">마지막</span>
    </div>
    </body>
    </html>
    出力結果