
글 상세 이전글, 다음글 정보를 쿼리

보통의 게시판 경우 위 그림과 같이 게시판의 글 상세 페이지에 이전글, 다음글의 글제목, 첨부파일 여부 아이콘 까지 나타내주는 경우가 많다 … 게시글 상세는 해당 게시글의 key값으로 데이터를 조회하는 것이 당연하고 이전, 다음 게시글의 정보 또한 이전 게시글 key값과, 다음 게시글 key값으로 해당 row를 조회하게 될것이다.
1. Mybatis sql 작성
<sql id="board_column">
<![CDATA[
X.write_nm,
X.write_no,
X.title,
REPLACE(X.contents, '\n', '<BR>') AS contents_br,
X.attach1,
DATE_FORMAT(X.write_dt,'%Y-%m-%d') as createTime
]]>
</sql>
<select id="selectNoticeView" resultType="RMap">
<![CDATA[
/* 이전 글 */
(SELECT 'befo' as ptype
,<include refid="board_column"></include>
FROM board_write X
WHERE write_no < #{writeNo}
ORDER BY write_no DESC LIMIT 1)
/* 현재 글 */
UNION ALL
(SELECT 'cur' as ptype
,<include refid="board_column"></include>
FROM board_write X
WHERE write_no = #{writeNo})
/* 다음 글 */
UNION ALL
(SELECT 'next' as ptype
,<include refid="board_column"></include>
FROM board_write X WHERE write_no > #{writeNo}
ORDER BY write_no ASC LIMIT 1)
]]>
</select>
sql_id : board_column 공통 컬럼을 설정한 후, sql_id : selectNoticeView 쿼리에서 이전 글, 현재 글, 다음 글을 UNION ALL로 한번에 가져온다. 이때 각 글의 PK는 현재 글 PK보다 작거나(이전 글), 크면(다음 글) 될 것이다. ptype 명시컬럼은 jsp등에서 이전, 다음, 현재 글의 row을 구분할 key값이다.
2. Servlet, Service 작성
List<RMap> posts = yourSerivce.selectNoticeView
for ( RMap each : posts) {
String type = (String)posts.get(0).get("ptype");
requset.addAttribute(type, each);
}
return posts;
selectNoticeView 쿼리에서 3개의 row를 받아왔을 것이다. 이 3개의 row를 반복문을 돌면서 front에 전달할 key(ptype) : value(각row데이터)로 매핑해주고 리턴해주면 된다. befo, cur, next 변수로 각각의 해당하는 리스트가 생기는 것이다.
3. Front 코드
<!-- 현재글 -->
<!-- 현재글의 제목, 작성자, 내용등은 <c:out value="${cur.xxx}"/> -->
<!-- 이전글 -->
<c:choose>
<c:when test="${!empty befo}">
<a href="/customer/noticeView.do?writeNo=<c:out value="${befo.writeNo}"/>">
<c:out value="${befo.title}"/>
</a>
<c:when test="${!empty befo.attach1}">
📁
</c:when>
</c:when>
<c:otherwise>
이전 게시글이 없습니다.
</c:otherwise>
</c:choose>
<!-- 다음글 -->
<c:choose>
<c:when test="${!empty next }">
<a href="/your/noticeView.do?writeNo=<c:out value="${next.writeNo}"/>">
<c:out value="${next.title}"/>
</a>
<c:when test="${!empty befo.attach1}">
📁
</c:when>
</c:when>
<c:otherwise>
다음 게시글이 없습니다.
</c:otherwise>
</c:choose>
jsp로 예를들자면, 현재글은 cur, 다음 글은 next, 이전 글은 befo key값으로 접근하면 된다.
'DB&SQL' 카테고리의 다른 글
[Oracle] 쿼리로 테이블컬럼의 이름, 타입, 길이, NULLABLE, 코멘트 추출 (0) | 2022.08.29 |
---|---|
[Tibero][Oralce] 테이블 컬럼 순서 변경 (0) | 2022.08.29 |
댓글