Spring boot / Oracle 11g / JSP
@Service
public interface BoardService {
public List<BoardVO> getList();
...
//paging
public int getTotal();
public List<BoardVO> getListWithPaging(Criteria cri);
}
@Slf4j
@Service
public class BoardServiceImpl implements BoardService{
@Autowired
private BoardMapper boardMapper;
...
@Override
public int getTotal() {
log.info("getTotal()..");
return boardMapper.getTotalCount();
}
@Override
public List<BoardVO> getListWithPaging(Criteria cri) {
log.info("getListWithPaging()..");
return boardMapper.getListWithPaging(cri);
}
}
@ToString
@Getter
@Setter
public class PageVO {
private int startPage;
private int endPage;
private boolean prev, next;
private int total;
private Criteria cri;
public PageVO(Criteria cri, int total) {
this.cri = cri;
this.total = total; // 전체 데이타 수
// ceil : 올림함수
// Math.ceil(.95); // 1
// Math.ceil(7.004); // 8
// Math.ceil(-0.95); // -0
// Math.ceil(-7.004); // -7
// endPage는 현재의 페이지 번호를 기준으로 계산함.
// 현재 페이지가 3일 경우 : Math.ceil(3/10) * 10 = 10
// 현재 페이지가 1일 경우 : Math.ceil(1/10) * 10 = 10
// 현재 페이지가 11일 경우 : Math.ceil(11/10) * 10 = 20 1.1을 올림함
// 현재 페이지가 20일 경우 : Math.ceil(20/10) * 10 = 20
// 현재 페이지가 21일 경우 : Math.ceil(21/10) * 10 = 30
this.endPage = (int) (Math.ceil(cri.getPageNum() / 10.0)) * 10;
this.startPage = this.endPage - 9;
// Total을 통한 endPage의 재계산
// 10개씩 보여주는 경우, 전체 데이터 수가 80개라고 가정하면 끝번호는 10이 아닌 8이 됨
int realEnd = (int) (Math.ceil((total * 1.0) / cri.getAmount()));
if (realEnd <= this.endPage) {
this.endPage = realEnd;
}
// 시작번호가 1보다 큰경우 존재
this.prev = this.startPage > 1; // <<
// realEnd가 끝번호(endPage)보다 큰 경우에만 존재
this.next = this.endPage < realEnd; // >>
}
public String makeQuery(int page) {
UriComponents uriComponentsBuilder = UriComponentsBuilder.newInstance().queryParam("pageNum", page) // pageNum = 3 // 3
.queryParam("amount", cri.getAmount()) // pageNum=3&amount=10
.build(); // ?pageNum=3&amount=10
return uriComponentsBuilder.toUriString(); // ?pageNum=3&amount=10 리턴
}
}
@Slf4j
@Controller
@RequestMapping("/board/*")
public class BoardController {
@Autowired
private BoardService boardService;
...
@GetMapping("/list2")
public String list2(Criteria cri, Model model) {
log.info("list2().. / Criteria: " + cri);
model.addAttribute("boardList", boardService.getListWithPaging(cri));
int total = boardService.getTotal();
log.info("total: " + total);
model.addAttribute("pageMaker", new PageVO(cri, total));
return "/board/list2";
}
}
package edu.global.ex.mapper;
import edu.global.ex.page.Criteria;
import edu.global.ex.vo.BoardVO;
@Mapper
public interface BoardMapper {
public List<BoardVO> getList();
public BoardVO read(int bid);
public int update(BoardVO board);
...
//paging
public int getTotalCount();
public List<BoardVO> getListWithPaging(Criteria cri);
}
<?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="edu.global.ex.mapper.BoardMapper">
<select id="getList" resultType="BoardVO">
<![CDATA[
select * from mvc_board order by bGroup desc, bStep asc
]]>
</select>
...
<select id="getTotalCount" resultType="int">
<![CDATA[
select count(*) from mvc_board
]]>
</select>
<select id="getListWithPaging" resultType="BoardVO">
<![CDATA[
SELECT * FROM (
SELECT ROWNUM AS RNUM, A.* FROM (
SELECT
*
FROM
mvc_board
order by bGroup desc, bStep asc
) A WHERE ROWNUM <= #{pageNum} * #{amount}
) WHERE RNUM > (#{pageNum}-1) * #{amount}
]]>
</select>
</mapper>
<body>
...
<c:if test="${pageMaker.prev}">
<a href="list2${pageMaker.makeQuery(pageMaker.startPage - 1) }">«</a>
</c:if>
<c:forEach begin="${pageMaker.startPage }" end="${pageMaker.endPage }"
var="idx">
<%-- <c:out value="${pageMaker.cri.page == idx?'':''}" /> --%>
<a href="list2${pageMaker.makeQuery(idx)}">${idx}</a>
</c:forEach>
<c:if test="${pageMaker.next && pageMaker.endPage > 0}">
<a href="list2${pageMaker.makeQuery(pageMaker.endPage+1)}">»</a>
</c:if>
</body>
잘 봤습니다. 좋은 글 감사합니다.