본문 바로가기

JAVA

JAVA 게시판 처리(조회, 페이징처리)-2

요청값을 받아 service단에 한번에 전달하기 위해 조회와 페이징처리를 한번에 했습니다.

 

board, boardsch의 mapper를 수정해줍니다.

mapper

<?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="board.dao.BoardDao">
<select id="totCnt" parameterType="boardsch"  resultType="int">	 
		 SELECT count(*)
		 	FROM board
		 WHERE 1=1		 
		 <if test="subject!=null">
		 AND subject LIKE '%'||#{subject}||'%'
		 </if>
		 <if test="writer!=null">
		 AND writer LIKE '%'||#{writer}||'%'
		 </if>			 	 			 
	</select>	
	<select id="boardList" resultType="board" parameterType="boardsch">
		select *
		from (
		 select rownum cnt, level, b.*  
		 from board b 
		 WHERE 1=1
		 <if test="subject!=null">
		 AND subject LIKE '%'||#{subject}||'%'
		 </if>
		 <if test="writer!=null">
		 AND writer LIKE '%'||#{writer}||'%'
		 </if>
		 start with refno = 0
		 connect by prior no = refno
		 order siblings by no desc  )
	    where cnt between #{start} and #{end} 		 	
	</select>
</mapper>

mybatis

mapper선언과 vo 별명 지정

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
	<typeAliases>
		<typeAlias alias="board" type="board.vo.Board"/>
		<typeAlias alias="BoardSch" type="board.vo.BoardSch"/>
	</typeAliases>

	<mappers>
		<mapper resource="resource/boardMapper.xml"/>
	</mappers>
</configuration>

controller

   default ModelAttribute (boardSch)로 가지고 있다.
   요청 + model
   Board sch :요청값을 받아서 service단에 전달..
   서비스에서 받아온 ArrayList<Board> 객체를 blist라는
   모델명으로 view단에 넘길 준비..

package board.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import board.service.BoardService;
import board.vo.BoardSch;

@Controller
public class BoardController {
  @Autowired(required = false)
  private BoardService service;

  // http://localhost:7080/sdas/boardList.do
  @RequestMapping("boardList.do")
  public String boardList(BoardSch sch, Model d) {

    d.addAttribute("blist", service.boardList(sch));

    return "WEB-INF\\views\\a01_boardList.jsp";
  }



}

service

조회와 페이징 처리를 한번에 했습니다.

package board.service;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import board.dao.BoardDao;
import board.vo.Board;
import board.vo.BoardSch;

@Service
public class BoardService {
  @Autowired(required = false)
  private BoardDao dao;

  public List<Board> boardList(BoardSch sch) {
    // 1. 전체 데이터 건수 설정
    sch.setCount(dao.totCnt(sch));
    System.out.println("총건수:" + sch.getCount());
    // 2. 선언한 한번에 보여줄 데이터 건수(요청값)
    // 초기화면을 대비하여 한번에 보여줄 데이터건수를 선언
    if (sch.getPageSize() == 0) {
      sch.setPageSize(5);
    }
    // 3. 총페이지수 : 데이터건수/한번에 보여주페이지 크기 [1][2][3][4]
    // ex) 18/5 ==> 3
    // ex) 18/5.0 (실수) ==> Math.ceil(3.6) : 4.0 ==> 정수형으로 변환하여 4를 총페이지수로 처리
    sch.setPageCount((int) Math.ceil(sch.getCount() / (double) sch.getPageSize()));
    // 4. 클릭한 현재 페이지 번호..(요청값)
    // 1 => 1~5
    // 2 => 6~10
    // 3 => 11~15
    // 4 ==>16~20
    // 초기에 화면은 0으로 처리되기에 default 값을 1로 처리한다.
    if (sch.getCurPage() == 0) {
      sch.setCurPage(1);
    }
    // 블럭단위로 next를 눌렀을 때, curpage가 증가되는 것을 방지.
    if (sch.getCurPage() > sch.getPageCount()) {
      sch.setCurPage(sch.getPageCount());
    }
    // 5. 마지막번호(현재페이지*한번에보여주는페이지건수)
    int end = sch.getCurPage() * sch.getPageSize();
    if (end > sch.getCount()) { // 총데이터 건수보다 크면..
      sch.setEnd(sch.getCount());
    } else {
      sch.setEnd(end);
    }
    sch.setStart((sch.getCurPage() - 1) * sch.getPageSize() + 1);
    // 1 (1-1)*5+1 : 1
    // 2 (2-1)*5+1 : 6
    // 3 (3-1)*5+1 : 11
    // 하단에 <이전 이후> 블럭 처리.
    // 1. 블럭의 크기 지정.
    sch.setBlockSize(5);
    // 2. 블럭의 번호 지정..
    // 1번블럭 [1]... [5]
    // 2번블럭 [6]... [10]
    // 3번블럭 [11]... [15]
    // [11][12][13]
    int blocknum = (int) (Math.ceil(sch.getCurPage() / (double) sch.getBlockSize()));

    int endBlock = blocknum * sch.getBlockSize();
    if (endBlock >= sch.getPageCount()) {
      endBlock = sch.getPageCount();
    }
    sch.setEndBlock(endBlock);
    sch.setStartBlock((blocknum - 1) * sch.getBlockSize() + 1);



    // 요청값을 넘기고, 객체(ArrayList<Board>)를 받는 처리.
    return dao.boardList(sch);
  }

}

dao

package board.dao;

import java.util.List;
import org.springframework.stereotype.Repository;
import board.vo.Board;
import board.vo.BoardSch;

// board.dao.BoardDao
@Repository
public interface BoardDao {
  // 번호
  public int totCnt(BoardSch sch);

  // 조회
  public List<Board> boardList(BoardSch sch);


}

vo

게시물 vo, 페이징처리 vo

Board

package board.vo;

import java.util.Date;

import org.springframework.web.multipart.MultipartFile;
// board.vo.Board
public class Board {
	private int cnt;
	private int no;
	private int refno;
	private String subject;
	private String content;
	private String writer;
	private int readcnt;
	private Date regdte;
	private Date uptdte;
	private MultipartFile report;
	private String fname;
	private int level;
	
	
	public Board() {
		// TODO Auto-generated constructor stub
	}
	public Board(int no, int refno, String subject, String content, String writer, int readcnt, Date regdte,
			Date uptdte) {
		this.no = no;
		this.refno = refno;
		this.subject = subject;
		this.content = content;
		this.writer = writer;
		this.readcnt = readcnt;
		this.regdte = regdte;
		this.uptdte = uptdte;
	}
	
	
	public int getCnt() {
		return cnt;
	}
	public void setCnt(int cnt) {
		this.cnt = cnt;
	}
	public int getNo() {
		return no;
	}
	public void setNo(int no) {
		this.no = no;
	}
	public int getRefno() {
		return refno;
	}
	public void setRefno(int refno) {
		this.refno = refno;
	}
	public String getSubject() {
		return subject;
	}
	public void setSubject(String subject) {
		this.subject = subject;
	}
	public String getContent() {
		return content;
	}
	public void setContent(String content) {
		this.content = content;
	}
	public String getWriter() {
		return writer;
	}
	public void setWriter(String writer) {
		this.writer = writer;
	}
	public int getReadcnt() {
		return readcnt;
	}
	public void setReadcnt(int readcnt) {
		this.readcnt = readcnt;
	}
	public Date getRegdte() {
		return regdte;
	}
	public void setRegdte(Date regdte) {
		this.regdte = regdte;
	}
	public Date getUptdte() {
		return uptdte;
	}
	public void setUptdte(Date uptdte) {
		this.uptdte = uptdte;
	}
	public MultipartFile getReport() {
		return report;
	}
	public void setReport(MultipartFile report) {
		this.report = report;
	}
	public String getFname() {
		return fname;
	}
	public void setFname(String fname) {
		this.fname = fname;
	}
	public int getLevel() {
		return level;
	}
	public void setLevel(int level) {
		this.level = level;
	}
	
}

BoardSch

package board.vo;
// board.vo.BoardSch
public class BoardSch {
	//페이징 처리 및 검색을 위한 vo
	//1. 검색
	private String subject;
	private String writer;
	//2.페이징 처리를 위한 속성
	private int count; //총데이터 건수
	private int pageSize; //한번에 보여줄 페이지 크기
	private int pageCount; //총 페이지 수
	private int curPage; //클릭한 현재 페이지 번호
	private int start; // 시작번호(현재페이지)
	private int end; // 마지막 번호(현재페이지)
	//3. 하단에 block속성
	
	private int blockSize; //한번에 보여줄 block의 크기
	private int startBlock; //block의 시작번호
	private int endBlock; // block의 마지막번호
	
	public String getSubject() {
		return subject;
	}
	public void setSubject(String subject) {
		this.subject = subject;
	}
	public String getWriter() {
		return writer;
	}
	public void setWriter(String writer) {
		this.writer = writer;
	}
	public int getCount() {
		return count;
	}
	public void setCount(int count) {
		this.count = count;
	}
	public int getPageSize() {
		return pageSize;
	}
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	public int getPageCount() {
		return pageCount;
	}
	public void setPageCount(int pageCount) {
		this.pageCount = pageCount;
	}
	public int getCurPage() {
		return curPage;
	}
	public void setCurPage(int curPage) {
		this.curPage = curPage;
	}
	public int getStart() {
		return start;
	}
	public void setStart(int start) {
		this.start = start;
	}
	public int getEnd() {
		return end;
	}
	public void setEnd(int end) {
		this.end = end;
	}
	public int getBlockSize() {
		return blockSize;
	}
	public void setBlockSize(int blockSize) {
		this.blockSize = blockSize;
	}
	public int getStartBlock() {
		return startBlock;
	}
	public void setStartBlock(int startBlock) {
		this.startBlock = startBlock;
	}
	public int getEndBlock() {
		return endBlock;
	}
	public void setEndBlock(int endBlock) {
		this.endBlock = endBlock;
	}
	
	
}

db

local db에 맞춰서 입력하시면 됩니다.

--테이블 생성
create table board(
		no number primary key,
		refno number,
		subject varchar2(200),
		content varchar2(2000),
		writer varchar2(100),
		readcnt number,
		regdte date,
		uptdte date
	);
    
    --번호 +1 테이블
    	create sequence board_seq
		start with 1
		minvalue 1
		maxvalue 999999
		increment by 1;
        
        
    --sample 데이터 등록
	insert into board2 values(
	board_seq2.nextval,0,'첫번째글','내용','홍길동',0, sysdate, sysdate);
    
    
    -- 글조회건수 수정
    update board2
    set readcnt = readcnt+1
    where no = 1;	


    -- 수정 처리
    UPDATE board2
     SET subject = '제목수정',
         content = '내용수정',
          uptdte = sysdate
    WHERE NO = 1;	
    
    
        -- 게시판 글 삭제
    delete
    from board
    where no = 1;

 

 

실행화면