java

์Šคํ”„๋ง ๋ ˆ๊ฑฐ์‹œ -๊ฒŒ์‹œํŒ ๋งŒ๋“ค๊ธฐ(3) ํŽ˜์ด์ง•

dev_summer 2021. 2. 27. 14:33

 

 

 

-pageMaker.java

package com.seoha.bbs.dto;

public class PageMaker {
	private int totalCount;
	private int startPage;
	private int endPage;
	private boolean prev;
	private boolean next;
	
	private int displayPageNum=10;
	
	private Criteria cri;

	public int getTotalCount() {
		return totalCount;
	}

	public void setTotalCount(int totalCount) {
		this.totalCount = totalCount;
		calcData();
	}
	private void calcData() {
		endPage=(int)(Math.ceil(cri.getPage()/(double)displayPageNum)*displayPageNum);
		startPage=(endPage-displayPageNum)+1;
		int tendPage=(int)(Math.ceil(totalCount)/(double)cri.getPerPageNum());
		
		if(endPage>tendPage) endPage=tendPage;
		prev=startPage==1?false:true;
		next=endPage*cri.getPerPageNum()>=totalCount?false:true;
	}

	public int getStartPage() {
		return startPage;
	}

	public int getEndPage() {
		return endPage;
	}
	
	public boolean isPrev() {
		return prev;
	}

	public boolean isNext() {
		return next;
	}

	public int getDisplayPageNum() {
		return displayPageNum;
	}

	public Criteria getCri() {
		return cri;
	}

	public void setCri(Criteria cri) {
		this.cri = cri;
	}
	
	
	
}

-Criteria.java

 

package com.seoha.bbs.dto;

public class Criteria {
	private int page;
	private int perPageNum;
	
	public Criteria() {

		this.page = 1;
		this.perPageNum = 10;
	}
	
	public void setPage(int page) {
		if (page <=0) {
			this.page=1;
			return;
		}
		this.page=page;
	}
	
	
	public void setPerPageNum(int perPageNum) {
		if(perPageNum <= 0 || perPageNum >100) {
			this.perPageNum = 10;
			return;
		}
		this.perPageNum =perPageNum;
	}
	
	public int getPage() {
		return page;
	}
	
	public int getPageStart() {
		return(this.page-1)*perPageNum;
	}
	
	public int getPerPageNum() {
		return this.perPageNum;
	}

	@Override
	public String toString() {
		return "Criteria [page=" + page + ", perPageNum=" + perPageNum + "]";
	}

	
	
}


 

-BoardMapper์ถ”๊ฐ€

<?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="com.seoha.bbs.mapper.BoardMapper">

	<select id="getList" resultType="com.seoha.bbs.dto.BoardVO">
			<![CDATA[
			select * from board
			]]>
	</select>

	<select id="mysqlListPaging"
		resultType="com.seoha.bbs.dto.BoardVO">
			<![CDATA[
			select * from board where bno>0 order by desc limit (#{page}-1)*#{perPageNum} ,10 
			]]>
	</select>

	<select id="oraListPaging"
		resultType="com.seoha.bbs.dto.BoardVO">
	      <![CDATA[
	      select * from (
	      select /* +INDEX_DESC(board pk_board) */
	      rownum rn, bno, title, content, writer, regDate, readCount 
	      from board where rownum <= #{page}*#{perPageNum} order by bno) 
	      where rn > (#{page}-1) * #{perPageNum}
	      ]]>
	</select>

	<select id="totalCount" resultType="int">
    <![CDATA[
    select count(*) from board where bno>0 
    ]]>
	</select>


	<insert id="insert">
		insert into board (bno, title, content, writer)
		values(board_seq.nextval, #{title}, #{content}, #{writer})
	</insert>

	<insert id="insertSelectKey">
		<selectKey keyProperty="bno" order="BEFORE" resultType="int">
			select board_seq.nextval from dual
		</selectKey>
		insert into board (bno, title, content, writer)
		values(#{bno},
		#{title}, #{content}, #{writer})
	</insert>

	<select id="read" resultType="com.seoha.bbs.dto.BoardVO">
		select * from board where bno =
		#{bno}
	</select>

	<update id="update">
		update board set title=#{title},content=#{content}
		where bno = #{bno}
	</update>

	<delete id="delete">
		delete from board where bno =#{bno}
	</delete>

</mapper>

 

 

 

- BoardMapper.java ์ถ”๊ฐ€

public List<BoardVO> oraListPaging(Criteria cri);

 

 

 

- BoardService.java ์ถ”๊ฐ€

public List<BoardVO> selectAll(Criteria cri);
public int totalCount();

 

- BoardServiceImpl ์ถ”๊ฐ€

	@Override
	public List<BoardVO> selectAll(Criteria cri) {
		return mapper.oraListPaging(cri);
	}

	@Override
	public int totalCount(Criteria cri) {
		return mapper.totalCount();
	}

 

- BoardController ์ˆ˜์ •

	@RequestMapping("/list")
	public String list(@ModelAttribute("cri") Criteria cri,Model model) {		
		int totalCount = service.totalCount(cri);
		PageMaker pageMaker=new PageMaker();
		pageMaker.setTotalCount(totalCount);
		pageMaker.setCri(cri);
		model.addAttribute("pageMaker", pageMaker);
		model.addAttribute("list", service.selectAll(cri));
		return "list";
	}

 

 

 

 

-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"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>seoha</title>
</head>
<body>

	<table border="1">

		<tr>
			<td colspan="5" align="right"><a href="input">๊ธ€์“ฐ๊ธฐ</a></td>
		<tr>
			<th>๋ฒˆํ˜ธ</th>
			<th>์ œ๋ชฉ</th>
			<th>์ž‘์„ฑ์ž</th>
			<th>๋‚ ์งœ</th>
			<th>์กฐํšŒ์ˆ˜</th>
		</tr>
		<c:forEach items="${list}" var="board">
			<tr>
				<td>${board.bno }</td>
				<td><a href="update?bno=${board.bno}">${board.title }</a></td>
				<td>${board.writer}</td>
				<td><fmt:formatDate pattern="yy.MM.dd hh:mm"
						value="${board.regdate }" /></td>
				<td>${board.readcount }</td>
			</tr>
		</c:forEach>


		<tr>
			<td colspan="5" align="center"><c:if test="${pageMaker.prev }">
					<a href="${pageMaker.startPage-1}">์ด์ „</a>
				</c:if> <c:forEach begin="${pageMaker.startPage}"
					end="${pageMaker.endPage}" var="i">
					<c:choose>
						<c:when test="${pageMaker.cri.page==i}">${i }</c:when>
						<c:otherwise>
							<a href="${i }">${i}</a>
						</c:otherwise>
					</c:choose>
				</c:forEach> <c:if test="${pageMaker.next }">
					<a href="${pageMaker.endPage+1 }">๋‹ค์Œ</a>
				</c:if></td>
		</tr>






	</table>

</body>
</html>