가자공부하러!

페이징 + 검색 정리(JSP) 본문

공부/웹

페이징 + 검색 정리(JSP)

오피스엑소더스 2019. 9. 3. 12:31

1. 요약

  > DB에서 조건에 맞는 값만 가져온다

- 검색 조건에 맞는 값 중 페이징 조건에 맞는 값만 가져오도록

- ex_ 작성자 ID가 admin인 모든행에 RNUM을 분배하고 현재 페이지에 맞는 RNUM만 가져온다

  > 네비게이션

- 현재 페이지에 맞는 네비게이션 요소만 보이게끔 설정

  > 소스코드

- github


2. 필요한 요소

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/** 검색조건. 0=제목, 1=내용 2=id*/
private int cond;    
 
/** 쿼리에 보낼 검색조건 문자열  */
private String condquery = "";
 
/** 검색어 */
private String keyword;
 
/** 현재페이지 */
private int pageNum = 1;
 
/** 페이지에 보여줄 데이터 갯수 */
private int recordCountPerPage = 10;
    
/** 현재 페이지의 시작 seq     */
private int startSeq = 1;
 
/** 현재 페이지의 끝 seq */
private int endSeq = 10;
    
/**총 페이지 수 */
private int totalPage = 1;
 
/** 현재 페이지 네비게이션 사이즈 */
private int pageNavSize = 5;
 
/** 최대 페이지 네비게이션 사이즈 */
private int maxNavSize = 10;
 
/** 페이지 네비게이션 첫 인덱스 */
private int firstNavIndex = 1;
 
/** 페이지 네비게이션 마지막 인덱스 */
private int lastNavIndex = 1;    
cs


3. 쿼리 조건(값)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<!-- 페이징 규칙과 검색 규칙에 따라 데이터를 가져오는 쿼리 -->
<select id="getAllOrderAndSearch" parameterType="bit.com.a.bbs.model.SearchDto" resultType="bit.com.a.bbs.model.BbsDto">
    SELECT RNUM, SEQ, ID, REF, STEP, DEPTH, TITLE, CONTENT, WDATE, PARENT, DEL, READCOUNT
    FROM ( 
            SELECT ROWNUM AS RNUM, SEQ, ID, REF, STEP, DEPTH, TITLE, CONTENT, WDATE, PARENT, DEL, READCOUNT
            FROM ( SELECT SEQ, ID, REF, STEP, DEPTH, TITLE, CONTENT, WDATE, PARENT, DEL, READCOUNT FROM BBS190729 
                WHERE DEL = 0                    
                <if test="cond != '4'">
                    AND #{condquery} LIKE '%'||#{keyword}||'%'
                </if>                
                ORDER BY SEQ DESC ) 
            ORDER BY RNUM
        )
    WHERE RNUM BETWEEN #{startSeq} AND #{endSeq}    
</select>
 
cs


4. 네비게이션

  > 현재 페이지에 맞는 네비게이션 값을 보여준다



5. 상세

  > bbsmain.jsp(뷰)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<%@page import="bit.com.a.bbs.model.BbsDto"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<% pageContext.setAttribute("crcn""\r\n"); %>
<!DOCTYPE html>
<html>
<input type="button" id="btnToggle" value="go">
<script>
$(function(){
    $("#btnToggle").click(function(){
        $(".mainContainer").toggle();
    });
});
</script>
<div class="mainContainer" align="center" style="margin:auto;">
    <h3>게시판 메인</h3>
    <div class="bbsContent">
        <div class="bbsListWrap">
            <div class="bbsTblWrap">
            <table class="tblBbs">
                <tr>
                    <th>번호</th>
                    <th>제목</th>
                    <th>작성자</th>
                    <th>작성일</th>
                    <th>조회</th>
                    <th>ref</th>
                    <th>step</th>
                    <th>depth</th>                    
                </tr>
                <c:if test="${fn:length(bbsList) < 1 }">
                    <tr>
                        <td colspan="8"><a href="showbbs.do">검색 결과가 없습니다.</a></td>
                    </tr>
                </c:if>
                <c:forEach items="${bbsList }" var="bbs">
                    <tr class="tblBbsTr">
                        <td>${bbs.seq }</td>
                        <td>
                            <c:forEach var="i" begin="1" end="${bbs.depth }">
                                <c:out value="&nbsp;" escapeXml="false"/>
                                <c:if test="${bbs.depth == i }"> 
                                    <class="fa fa-long-arrow-right"></i>
                                </c:if>
                            </c:forEach>
                            
                            ${bbs.title}                            
                        </td>
                        <td>${bbs.id} </td>
                        <td>${fn:substring(bbs.wdate,0,10) } </td>
                        <td>${bbs.readcount} </td>
                        <td>${bbs.ref }</td>
                        <td>${bbs.step }</td>
                        <td>${bbs.depth }</td>
                    </tr>                
                </c:forEach>
            </table>
            </div>
            <div class="bbsPagingNavWrap" align="center" style="margin:auto;">
            <ul class="pagingNav pagination">
                <c:choose>                 
                    <c:when test="${bbsOrderDto.cond eq '4'}">
                        <c:forEach var="i" begin="${bbsOrderDto.firstNavIndex }" end="${bbsOrderDto.lastNavIndex }" step="1">
                            <li class="pagingNavItem page-item"><class="page-link" href="#"><c:out value="${i}"/></a></li>
                        </c:forEach>
                    </c:when>                              
                    <c:otherwise>
                        <c:forEach var="i" begin="${bbsOrderDto.firstNavIndex }" end="${bbsOrderDto.lastNavIndex }" step="1">
                            <li class="pagingNavItemWithSearchCond"><c:out value="${i}"/></li>
                        </c:forEach>
                    </c:otherwise>                 
                </c:choose>
            </ul>
            </div>
        </div>        
        <div class="bbsListSearchConditions">
            <select id="cond">
                <option value="0" <c:out value="${bbsOrderDto.cond == '0'?'selected':'' }"/>>제목</option>
                <option value="1" <c:out value="${bbsOrderDto.cond == '1'?'selected':'' }"/>>내용</option>
                <option value="2" <c:out value="${bbsOrderDto.cond == '2'?'selected':'' }"/>>아이디</option>
            </select>
            <input type="text" id="keyword" value="${bbsOrderDto.keyword != ''?bbsOrderDto.keyword:'' }">
            <input type="button" id="btnSearch" value="검색">
        </div>
    </div>
</div>
 
<!-- 세션에 아이디가 있으면 버튼 활성화 -->
<c:if test="${not empty currUser.id}">
    <input type="button" value="새 글 작성" id="btnWriteNew" onclick="location.href='showwritenew.do'; "/>    
</c:if>
 
<script type="text/javascript">
 
$(function(){    
    //테이블 행 클릭 시 seq에 맞는 디테일 페이지로 이동
    $(".tblBbsTr").click(function(){
        var seq = $(this).children().eq(0).text().trim();
        location.href="showbbsdetail.do?seq=" + seq;
    });
    
    //검색 조건이 없을 때 페이징 네비게이션
    $(".pagingNavItem").click(function(){
        location.href='showbbs.do?pageNum=' + $(this).text();
    });
    
    //검색 조건이 있을 때 페이징 네비게이션
    $(".pagingNavItemWithSearchCond").click(function(){
        var cond = $("#cond option:selected").val();
        var keyword = $("#keyword").val();
        location.href='showbbs.do?pageNum=' + $(this).text() + '&cond=' + cond + '&keyword=' + keyword; 
    });
    
    //검색버튼 클릭 시 조건에 맞는 검색페이지로 이동
    $("#btnSearch").click(function(){
        var cond = $("#cond option:selected").val();
        var keyword = $("#keyword").val();
        location.href='showbbs.do?pageNum=1&cond=' + cond + '&keyword=' + keyword; 
    });
});
 
</script>
 
</html>
cs


  > bbs Controller

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/**게시글 조회 뷰로 이동
 * @param model
 * @param pageNum
 * @param cond
 * @param keyword
 * @return
 */
@RequestMapping(value="showbbs.do")
public String showBbs(Model model, @RequestParam(value="pageNum", defaultValue="1"int pageNum,
                                        @RequestParam(value="cond", defaultValue = "4"int cond,
                                        @RequestParam(value="keyword", defaultValue = "") String keyword) {
    //logger.info("cond=" + cond + ",  keyword : " + keyword);
    
    int dbSize = bbsService.getDBCountSize();
 
    BbsOrderDto dto = new BbsOrderDto(cond, keyword, pageNum, dbSize);
    List<BbsDto> list = bbsService.getAllBbs(dto);
    for(BbsDto d : list ) {
        //System.out.println(d.toString());
    }
    //System.out.println("리스트싸이즈!" + list.size());
    model.addAttribute("bbsList", list);
    dto.setNav(bbsService.getDBCountSizeByCondition(dto));
    
    model.addAttribute("bbsOrderDto", dto);
    model.addAttribute("pagingVo", PagingVO.builder().pageNum(pageNum).totalSize(bbsService.getDBCountSizeByCondition(dto)).build());
    
    
    //return "bbs/bbsmain";
    return "bbsmain.tiles";
}
cs


  > BbsOrderDto - 검색, 페이징 조건

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package bit.com.a.bbs.model;
 
import java.io.Serializable;
 
import lombok.Builder;
import lombok.Data;
 
@Data
public class BbsOrderDto implements Serializable{
 
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
 
    /** 검색조건. 0=제목, 1=내용 2=id*/
    private int cond;    
    
    /** 쿼리에 보낼 검색조건 문자열  */
    private String condquery = "";
    
    /** 검색어 */
    private String keyword;
    
    /** 현재페이지 */
    private int pageNum = 1;
    
    /** 페이지에 보여줄 데이터 갯수 */
    private int recordCountPerPage = 10;
        
    /** 현재 페이지의 시작 seq     */
    private int startSeq = 1;
    
    /** 현재 페이지의 끝 seq */
    private int endSeq = 10;
        
    /**총 페이지 수 */
    private int totalPage = 1;
 
    /** 현재 페이지 네비게이션 사이즈 */
    private int pageNavSize = 5;
    
    /** 최대 페이지 네비게이션 사이즈 */
    private int maxNavSize = 10;
 
    /** 페이지 네비게이션 첫 인덱스 */
    private int firstNavIndex = 1;
 
    /** 페이지 네비게이션 마지막 인덱스 */
    private int lastNavIndex = 1;    
    
    /**현재 페이지 번호와 총 게시글의 수를 매개변수로 받아서 페이징 객체 생성
     * @param pageNum
     * @param totalSize
     */
    @Builder
    public BbsOrderDto(int pageNum, int totalSize) {
        super();
        this.pageNum = pageNum;
        this.setRecordCountPerPage(10);
        this.startSeq = ( ( pageNum - 1 ) * this.getRecordCountPerPage() ) + 1;
        this.endSeq = startSeq + this.getRecordCountPerPage() - 1;
        //페이지 네비게이션의 최대 사이즈
        this.maxNavSize = (totalSize % recordCountPerPage == 0) ? (totalSize / recordCountPerPage) : (totalSize / recordCountPerPage) + 1;
        this.setNav(totalSize);
    }
    
    @Builder
    public BbsOrderDto(int cond, String keyword, int pageNum, int totalSize) {
        this.pageNum = pageNum;
        this.setRecordCountPerPage(10);
        this.startSeq = ( ( pageNum - 1 ) * this.getRecordCountPerPage() ) + 1;
        this.endSeq = startSeq + this.getRecordCountPerPage() - 1;        
        //페이지 네비게이션의 최대 사이즈
        this.maxNavSize = (totalSize % recordCountPerPage == 0) ? (totalSize / recordCountPerPage) : (totalSize / recordCountPerPage) + 1;
        this.setNav(totalSize);
        this.cond = cond;
        this.keyword = keyword;
        switch( cond ) {
            case 0this.condquery="title"break;
            case 1this.condquery="content"break;
            case 2this.condquery="id"break;
            defaultthis.condquery= "all"break;
        }        
        //System.out.println("[BbsOrderDto] condquery:" + condquery + " , keyword:" + keyword);
    }
    
    /** All Args Constructor
     * @param pageIndex
     * @param recordCountPerPage
     * @param startSeq
     * @param endSeq
     * @param totalPage
     * @param pageSize
     * @param firstNavIndex
     * @param lastNavIndex
     */
    @Builder
    public BbsOrderDto(int pageNum, int recordCountPerPage, int startSeq, int endSeq, int totalPage, int pageSize,
            int firstNavIndex, int lastNavIndex) {
        super();
        this.pageNum = pageNum;
        this.recordCountPerPage = recordCountPerPage;
        this.startSeq = startSeq;
        this.endSeq = endSeq;
        this.totalPage = totalPage;
        this.pageNavSize = pageSize;
        this.firstNavIndex = firstNavIndex;
        this.lastNavIndex = lastNavIndex;
    }
    
    /**네비게이션 바 설정
     * 12345 : 1 ~ 5
     * 678910 : 6 ~ 10
     * (7/5)*5 +1
     * (3/5)*5 +1
     */
    public void setNav(int totalSize) {
        //페이지 네비게이션의 최대 사이즈
        this.maxNavSize = (totalSize % recordCountPerPage == 0) ? (totalSize / recordCountPerPage) : (totalSize / recordCountPerPage) + 1;
        this.firstNavIndex = (this.pageNum / pageNavSize) * pageNavSize + 1;
        //마지막 네비게이션 바 인덱스. 전체 글 수가 한 페이지 당 글 수로 나누어 떨어지면 -0 아니면 -1
        int temp = this.firstNavIndex + pageNavSize - 1 ;
        this.lastNavIndex = (temp > maxNavSize) ? maxNavSize : temp ;
        //System.out.println("totalSize : " + totalSize + " , recordCountPerPage : " + recordCountPerPage + " , temp : " + temp + " , ddd " +  (( (totalSize % recordCountPerPage) == 0 )?3:2 ) );
        //System.out.println("firstNavIndex : " + firstNavIndex + " , lastNavIndex : " + lastNavIndex + " , maxNavSize : " + maxNavSize);
    }
    
}
 
cs


  > 쿼리

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<?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="Bbs190729">
    <!-- DB 쿼리 작성 부분 -->
 
    <!-- parameterType : 패키지명까지 동일하게 작성해야 함 -->
    <insert id="writeNew" parameterType="bit.com.a.bbs.model.BbsDto">
        INSERT INTO BBS190729 (SEQ, ID, REF, STEP, DEPTH, TITLE, CONTENT, WDATE, PARENT, DEL, READCOUNT)
        VALUES(SEQ_BBS190729.NEXTVAL, #{id}, SEQ_BBS190729.CURRVAL, 00, #{title}, #{content}, SYSDATE, 000)
    </insert>
        
    <!-- 규칙 없이 모든 데이터를 가져오는 쿼리 -->
    <select id="getAll" resultType="bit.com.a.bbs.model.BbsDto">
        SELECT SEQ, ID, REF, STEP, DEPTH, TITLE, CONTENT, WDATE, PARENT, DEL, READCOUNT
        FROM BBS190729
        WHERE DEL=0
        ORDER BY REF DESC, STEP ASC, SEQ DESC
    </select>    
    
    <!-- 페이징 규칙에 따라 데이터를 가져오는 쿼리 -->
    <select id="getAllOrder" parameterType="bit.com.a.bbs.model.PagingVO" resultType="bit.com.a.bbs.model.BbsDto">
        SELECT RNUM, SEQ, ID, REF, STEP, DEPTH, TITLE, CONTENT, WDATE, PARENT, DEL, READCOUNT
        FROM ( 
                SELECT ROWNUM AS RNUM, SEQ, ID, REF, STEP, DEPTH, TITLE, CONTENT, WDATE, PARENT, DEL, READCOUNT
                FROM ( SELECT SEQ, ID, REF, STEP, DEPTH, TITLE, CONTENT, WDATE, PARENT, DEL, READCOUNT FROM BBS190729 WHERE DEL=0 ORDER BY SEQ DESC ) 
                ORDER BY RNUM
            )
        WHERE RNUM BETWEEN #{startSeq} AND #{endSeq}
    </select>
 
    <!-- 페이징 규칙과 검색 규칙에 따라 데이터를 가져오는 쿼리 -->
    <select id="getAllBbs" parameterType="bit.com.a.bbs.model.BbsOrderDto" resultType="bit.com.a.bbs.model.BbsDto">
        SELECT RNUM, SEQ, ID, REF, STEP, DEPTH, TITLE, CONTENT, WDATE, PARENT, DEL, READCOUNT
        FROM ( 
                SELECT ROWNUM AS RNUM, SEQ, ID, REF, STEP, DEPTH, TITLE, CONTENT, WDATE, PARENT, DEL, READCOUNT
                FROM ( SELECT SEQ, ID, REF, STEP, DEPTH, TITLE, CONTENT, WDATE, PARENT, DEL, READCOUNT FROM BBS190729 
                    <include refid="searchOption"></include>                
                    ORDER BY REF DESC, STEP ASC ) 
                ORDER BY RNUM
            )
        WHERE RNUM BETWEEN #{startSeq} AND #{endSeq}
        <!-- WHERE RNUM BETWEEN #{startSeq} AND 3 -->        
    </select>
    
    <!-- 페이징 규칙과 검색 규칙에 맞는 DB의 총 개수를 가져오는 쿼리 -->
    <select id="getDBCountSizeByCondition" parameterType="bit.com.a.bbs.model.BbsOrderDto" resultType="int">
        SELECT COUNT(*) AS CNT
        FROM ( 
                SELECT ROWNUM AS RNUM, SEQ, ID, REF, STEP, DEPTH, TITLE, CONTENT, WDATE, PARENT, DEL, READCOUNT
                FROM ( SELECT SEQ, ID, REF, STEP, DEPTH, TITLE, CONTENT, WDATE, PARENT, DEL, READCOUNT FROM BBS190729 
                    <include refid="searchOption"></include>                
                    ORDER BY SEQ DESC ) 
                ORDER BY RNUM
            )
        <!-- WHERE RNUM BETWEEN #{startSeq} AND 3 -->        
    </select>
    
    
    <!-- 게시판 DB의 총 개수를 가져오는 쿼리 -->
    <select id="getDBCountSize" resultType="int">
        SELECT COUNT(*)
        FROM BBS190729
        WHERE DEL=0
    </select>
    
    <select id="getOneBySeq" parameterType="int"
        resultType="bit.com.a.bbs.model.BbsDto">
        SELECT * FROM BBS190729 WHERE SEQ=#{seq}
    </select>
    
    <!-- delete query -->
    <delete id="delete" parameterType="int">
        DELETE FROM BBS190729
        WHERE SEQ=#{seq}
    </delete>
    
    <!-- 삭제. DEL을 1로 수정 -->
    <update id="deleteOneBySeq" parameterType="int">
        UPDATE BBS190729 SET DEL=1
        WHERE SEQ=#{seq}
    </update>
    
    <!-- 조회 수 + 1-->
    <update id="plusOneReadCntBySeq" parameterType="int">
        UPDATE BBS190729 SET READCOUNT=READCOUNT+1
        WHERE SEQ=#{seq}
    </update>
 
    <!-- 파라미터 타입을MEMBER로 받는 DELETE 쿼리 -->
    <delete id="removeBySeq" parameterType="bit.com.a.bbs.model.BbsDto">
        DELETE FROM MEMBER
        WHERE SEQ=#{seq}
    </delete>
    
    <update id="updateOne" parameterType="bit.com.a.bbs.model.BbsDto">
        UPDATE BBS190729 SET TITLE=#{title}, content=#{content}
        WHERE SEQ=#{seq}
    </update>
    
    <!-- 답글 sql 부분 -->
    
    <!-- 원본글(seq)이 갖고있는 ref와 동일한 ref를 갖는 글 중 seq보다 step이 큰 글의 step을 1씩 증가. -->
    <update id="plusOneStepBiggerThanOriginInReply" parameterType="bit.com.a.bbs.model.BbsDto">
        UPDATE BBS190729 SET STEP=STEP+1
        WHERE REF=#{ref} AND STEP > #{step}
    </update>
    
    <!-- 새로운 답글을 DB에 저장하는 쿼리, 새로운 답글. 답글 -->
    <insert id="writeNewReply" parameterType="bit.com.a.bbs.model.BbsDto">
        INSERT INTO BBS190729 (SEQ, ID, REF, STEP, DEPTH, TITLE, CONTENT, WDATE, PARENT, DEL, READCOUNT)
        VALUES(SEQ_BBS190729.NEXTVAL, #{id}, #{ref}, #{step}, #{depth}, #{title}, #{content}, SYSDATE, #{parent}, 00)
    </insert>
    
    
    <!-- 댓글(comment) sql 부분 -->
    
    <!-- 등록된 댓글을 가져오는 쿼리 -->
    <select id="getAllBbsComment" parameterType="int" resultType="bit.com.a.bbs.model.BbsCommentDto">
        SELECT * 
        FROM BBS190729_COMMENT
        WHERE PARENT=#{int}
    </select>
    
    <!-- 댓글을 DB에 저장하는 쿼리. BbsCommentDto에 포함된 parent가 게시물의 seq번호가 된다 -->
    <insert id="writeNewComment" parameterType="bit.com.a.bbs.model.BbsCommentDto">
        INSERT INTO BBS190729_COMMENT ( SEQ, PARENT, LIKECNT, REF, STEP, DEPTH, DEL, ID, CONTENT, WDATE )
        VALUES ( SEQ_BBS190729_COMMENT.NEXTVAL, #{parent}, 0, SEQ_BBS190729_COMMENT.CURRVAL, 000, #{id}, #{content}, SYSDATE )
    </insert>
            
    <!-- 댓글을 삭제하는 메소드. DEL을 0으로 변경한다. -->
    <update id="deleteComment" parameterType="int">
        UPDATE BBS190729_COMMENT SET DEL=1 WHERE SEQ=#{seq}
    </update>
    
    <!-- 댓글을 수정하는 메소드. -->
     <update id="updateComment" parameterType="bit.com.a.bbs.model.BbsCommentDto">
         UPDATE BBS190729_COMMENT SET CONTENT=#{content} WHERE SEQ=#{seq}
     </update>
 
    <!-- 삽입 sql -->
    <sql id="searchOption">
        <choose>
            <when test="condquery == 'all'">
                WHERE DEL = 0 AND ( TITLE LIKE '%'||#{keyword}||'%'
                OR content LIKE '%'||#{keyword}||'%'
                OR id LIKE '%'||#{keyword}||'%' )
            </when>
            <otherwise>
                WHERE DEL = 0 AND ${condquery} like '%'||#{keyword}||'%'
            </otherwise>
        </choose>
    </sql>
</mapper>
 
cs


최근 수정한 CommonPagingDTO
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package com.rhymes.app.common.model;
 
import java.io.Serializable;
 
import lombok.Builder;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
 
@SuppressWarnings("serial")
@Getter
@Setter
@ToString
public class CommonPagingDTO implements Serializable {
    
    /** 검색조건. 0=제목, 1=내용 2=id*/
    private int cond;    
     
    /** 쿼리에 보낼 검색조건 문자열  */
    private String condquery = "";
     
    /** 검색어 */
    private String keyword;
     
    /** 현재페이지 */
    private int pageNum = 1;
     
    /** 페이지에 보여줄 데이터 갯수 */
    private int recordCountPerPage = 10;
        
    /** 현재 페이지의 시작 seq     */
    private int startSeq = 1;
     
    /** 현재 페이지의 끝 seq */
    private int endSeq = 10;
        
    /**총 페이지 수 */
    private int totalPage = 1;
     
    /** 현재 페이지 네비게이션 사이즈 */
    private int pageNavSize = 5;
     
    /** 최대 페이지 네비게이션 사이즈 */
    private int maxNavSize = 10;
     
    /** 페이지 네비게이션 첫 인덱스 */
    private int firstNavIndex = 1;
     
    /** 페이지 네비게이션 마지막 인덱스 */
    private int lastNavIndex = 1
    
    /** 총 게시물의 수 */
    private int totalSize = 10;
    
    /**현재 페이지 번호와 총 게시글의 수를 매개변수로 받아서 페이징 객체 생성
     * @param pageNum
     * @param totalSize
     */
    @Builder
    public CommonPagingDTO(int pageNum, int totalSize) {
        this.pageNum = pageNum;
        this.setRecordCountPerPage(10);
        this.startSeq = ( ( pageNum - 1 ) * this.getRecordCountPerPage() ) + 1;
        this.endSeq = startSeq + this.getRecordCountPerPage() - 1;
        this.totalSize = totalSize;
        //페이지 네비게이션의 최대 사이즈
        this.maxNavSize = (totalSize % recordCountPerPage == 0) ? (totalSize / recordCountPerPage) : (totalSize / recordCountPerPage) + 1;
        this.setNav(totalSize);
    }
    
    @Builder
    public CommonPagingDTO(int cond, String keyword, int pageNum, int totalSize) {
        this.pageNum = pageNum;
        this.setRecordCountPerPage(10);
        this.startSeq = ( ( pageNum - 1 ) * this.getRecordCountPerPage() ) + 1;
        this.endSeq = startSeq + this.getRecordCountPerPage() - 1;        
        this.totalSize = totalSize;
        //페이지 네비게이션의 최대 사이즈
        this.maxNavSize = (totalSize % recordCountPerPage == 0) ? (totalSize / recordCountPerPage) : (totalSize / recordCountPerPage) + 1;
        this.setNav(totalSize);
        this.cond = cond;
        this.keyword = keyword;
        switch( cond ) {
            case 0: this.condquery="title"; break;
            case 1: this.condquery="content"; break;
            case 2: this.condquery="id"; break;
            default: this.condquery= "all"; break;
        }        
    }
    
    /** All Args Constructor
     * @param pageIndex
     * @param recordCountPerPage
     * @param startSeq
     * @param endSeq
     * @param totalPage
     * @param pageSize
     * @param firstNavIndex
     * @param lastNavIndex
     */
    @Builder
    public CommonPagingDTO(int pageNum, int recordCountPerPage, int startSeq, int endSeq, int totalPage, int pageSize,
            int firstNavIndex, int lastNavIndex, int totalSize) {
        super();
        this.pageNum = pageNum;
        this.recordCountPerPage = recordCountPerPage;
        this.startSeq = startSeq;
        this.endSeq = endSeq;
        this.totalPage = totalPage;
        this.pageNavSize = pageSize;
        this.firstNavIndex = firstNavIndex;
        this.lastNavIndex = lastNavIndex;
        this.totalSize = totalSize;
    }
    
    /**페이지 당 표현 개수에 변동이 생기면 네비게이션을 수정하게끔 설정
     * @param recordCountPerPage
     */
    public void setRecordCountPerPage(int recordCountPerPage) {
        this.recordCountPerPage = recordCountPerPage;
        setNav(this.totalSize);
    }
    
    /**네비게이션 바 설정
     * 12345 : 1 ~ 5
     * 678910 : 6 ~ 10
     * (7/5)*5 +1
     * (3/5)*5 +1
     */
    public void setNav(int totalSize) {
        //페이지 네비게이션의 최대 사이즈
        this.maxNavSize = (totalSize % recordCountPerPage == 0) ? (totalSize / recordCountPerPage) : (totalSize / recordCountPerPage) + 1;
        this.firstNavIndex = (this.pageNum / pageNavSize) * pageNavSize + 1;
        //마지막 네비게이션 바 인덱스. 전체 글 수가 한 페이지 당 글 수로 나누어 떨어지면 -0 아니면 -1
        int temp = this.firstNavIndex + pageNavSize - 1 ;
        this.lastNavIndex = (temp > maxNavSize) ? maxNavSize : temp ;
    }
}
 
cs



스프링부트 페이징DTO

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package com.rhymes.app.member.model.mypage;
 
import lombok.Getter;
import lombok.Setter;
 
/**주문내역 페이징, 검색조건(기간)을 갖는 DTO
 * @author minhj
 *
 */
@Getter
@Setter
public class MemberOrderPagingDTO {
    
    /** 유저아이디 ...*/
    private String userid;
    /** 검색조건. 0=전체기간, 1=1년 2=2년 ...*/
    private int cond;    
     
    /** 쿼리에 보낼 검색조건 문자열  */
    private String condquery = "";
     
    /** 검색어 */
    private String keyword;
     
    /** 현재페이지 */
    private int pageNum = 1;
     
    /** 페이지에 보여줄 데이터 갯수 */
    private int recordCountPerPage = 3;
        
    /** 현재 페이지의 시작 seq     */
    private int startSeq = 1;
     
    /** 현재 페이지의 끝 seq */
    private int endSeq = 10;
        
    /**총 페이지 수 */
    private int totalPage = 1;
     
    /** 현재 페이지 네비게이션 사이즈 */
    private int pageNavSize = 5;
     
    /** 최대 페이지 네비게이션 사이즈 */
    private int maxNavSize = 10;
     
    /** 페이지 네비게이션 첫 인덱스 */
    private int firstNavIndex = 1;
     
    /** 페이지 네비게이션 마지막 인덱스 */
    private int lastNavIndex = 1
    
    /** 총 게시물의 수 */
    private int totalSize = 10;
    
    /**매개변수 없는 생성자.
     * 최초접속에 쓰임
     */
    public MemberOrderPagingDTO() {    }
        
    public void setPageNum(int pageNum) {
        this.startSeq = ( ( pageNum - 1 ) * this.getRecordCountPerPage() ) ;
        this.endSeq = startSeq + this.getRecordCountPerPage() - 1;
        this.setNav(this.totalSize);
    }
    
    public void setTotalSize(int totalSize) {
        this.totalSize = totalSize;
        this.setNav(totalSize);
    }
 
    /**네비게이션 바 설정
     * 12345 : 1 ~ 5
     * 678910 : 6 ~ 10
     * (7/5)*5 +1
     * (3/5)*5 +1
     */
    public void setNav(int totalSize) {
        //페이지 네비게이션의 최대 사이즈
        this.maxNavSize = (totalSize % recordCountPerPage == 0) ? (totalSize / recordCountPerPage) : (totalSize / recordCountPerPage) + 1;
        this.firstNavIndex = (this.pageNum / pageNavSize) * pageNavSize + 1;
        //마지막 네비게이션 바 인덱스. 전체 글 수가 한 페이지 당 글 수로 나누어 떨어지면 -0 아니면 -1
        int temp = this.firstNavIndex + pageNavSize - 1 ;
        this.lastNavIndex = (temp > maxNavSize) ? maxNavSize : temp ;
    }
    
    @Override
    public String toString() {
        return "MemberOrderPagingDTO [userid=" + userid + ", cond=" + cond + ", condquery=" + condquery + ", keyword="
                + keyword + ", pageNum=" + pageNum + ", recordCountPerPage=" + recordCountPerPage + ", startSeq="
                + startSeq + ", endSeq=" + endSeq + ", totalPage=" + totalPage + ", pageNavSize=" + pageNavSize
                + ", maxNavSize=" + maxNavSize + ", firstNavIndex=" + firstNavIndex + ", lastNavIndex=" + lastNavIndex
                + ", totalSize=" + totalSize + "]";
    }
}
 
cs



Comments