mapper.xml 으로 쿼리보내기
board_tbl 테이블의 pk_board를 힌트로 DESC를 걸고
조회 구문을 선언합니다.
현재 조회 중인 ${page} 변수를 집어넣기만 하면
10개의 페이지네이션이 list에 적용될 것 입니다.
물론 mapper.java에
public List<BoardVO> getList(Criteria cri);
선언해주셔야 합니다.
Service에서 mapper 호출시키기
BoardService입니다.
package com.ict.service;
import java.util.List;
import com.ict.persistence.BoardVO;
import com.ict.persistence.Criteria;
import com.ict.persistence.SearchCriteria;
public interface BoardService {
public List<BoardVO> getList(SearchCriteria cri);
그리고 BoardService를 상속받은 BoardServiceImpl입니다.
package com.ict.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ict.mapper.BoardMapper;
import com.ict.persistence.BoardVO;
import com.ict.persistence.Criteria;
import com.ict.persistence.SearchCriteria;
@Service
public class BoardServiceImpl implements BoardService {
@Autowired
private BoardMapper mapper;
@Override
public List<BoardVO> getList(SearchCriteria cri) {
return mapper.getList(cri);
}
이렇게
mapper를 호출해서 쿼리문을 실행시키는
서비스를 선언해두었습니다.
controller에서 service 호출시키기
@Controller
@RequestMapping("/board")
@Log4j
어노테이션이 선언되어있는
boardService입니다.
@RequestMapping("/list")를 통해
/board/list 주소의 url을 받았을 때 실행됩니다.
if(cri.getPage() == 0) {
cri.setPage(1);
}
위 코드는 아무런 page 값도 받지 않고 접근했을 때
자동으로 첫 페이지에서 시작하게끔 도와줍니다.
List<BoardVO> boardList = service.getList(cri);
log.info(boardList);
// 바인딩
model.addAttribute("boardList", boardList);
글 전체 목록을 가져온 후에
디버깅을 하고
model.addAttribute로 바인딩합니다.
// PageMaker 생성 및 cri주입, 그리고 바인딩해서 보내기
PageMaker pageMaker = new PageMaker();
pageMaker.setCri(cri);
pageMaker.setTotalBoard(service.getBoardCount(cri));
model.addAttribute("pageMaker", pageMaker);
pageMaker를 생성하고 cri를 주입해서
calcDate()메서드가 실행되도록 합니다.
getBoardCount메서드를 통해 setTotalBoard도 실행한 뒤
바인딩합니다.
그리고 원하는 jsp파일이 있는 주소로 보내주면 됩니다.
list.jsp파일 작성
<%@ 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>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p" crossorigin="anonymous"></script>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<table class="table table-hover">
<thead>
<tr>
<th>글번호</th>
<th>제목</th>
<th>글쓴이</th>
<th>쓴날짜</th>
<th>수정날짜</th>
</tr>
</thead>
<tbody>
<c:forEach var="board" items="${boardList }">
<tr>
<td>${board.bno }</td>
<td><a href="http://localhost:8181/board/detail?page=${pageMaker.cri.page}&bno=${board.bno }&searchType=${pageMaker.cri.searchType}&keyword=${pageMaker.cri.keyword}">${board.title }</a></td>
<td>${board.writer }</td>
<td>${board.regDate }</td>
<td>${board.updateDate }</td>
</tr>
</c:forEach>
</tbody>
</table>
<a href="/board/insert"><button class="btn btn-primary">글쓰기</button></a>
<ul class="pagination">
<c:if test="${pageMaker.prev }">
<li class="page-item">
<a class="page-link" href="http://localhost:8181/board/list?page=${pageMaker.startPage -1}&searchType=${pageMaker.cri.searchType}&keyword=${pageMaker.cri.keyword}" aria-label="Previous">
<span aria-hidden="true">«</span>
</a>
</li>
</c:if>
<c:forEach begin="${pageMaker.startPage }" end="${pageMaker.endPage }" var="pNum">
<li class="page-item ${pNum eq pageMaker.cri.page ? 'active' : '' }"><a class="page-link" href="http://localhost:8181/board/list?page=${pNum }&searchType=${pageMaker.cri.searchType}&keyword=${pageMaker.cri.keyword}">${pNum }</a></li>
</c:forEach>
<c:if test="${pageMaker.next }">
<li class="page-item">
<a class="page-link" href="http://localhost:8181/board/list?page=${pageMaker.endPage +1}&searchType=${pageMaker.cri.searchType}&keyword=${pageMaker.cri.keyword}" aria-label="Next">
<span aria-hidden="true">»</span>
</a>
</li>
</c:if>
</ul>
좋습니다.
일단은 여기까지 입니다.