관리 메뉴

거니의 velog

231216_SPRING CRUD 보강 4 본문

대덕인재개발원_웹기반 애플리케이션

231216_SPRING CRUD 보강 4

Unlimited00 2023. 12. 16. 10:50

package kr.or.ddit.controller.board;

import java.util.List;

import javax.inject.Inject;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import kr.or.ddit.service.IBoardService;
import kr.or.ddit.vo.BoardVO;
import kr.or.ddit.vo.PaginationInfoVO;

@Controller
@RequestMapping("/board")
public class BoardController {
	
	@Inject
	private IBoardService boardService;
	
	@RequestMapping(value="/list.do", method = RequestMethod.GET)
	public String boardList(
			@RequestParam(name = "page", required = false, defaultValue = "1") int currentPage,
			@RequestParam(required = false, defaultValue = "title") String searchType,
			@RequestParam(required = false) String searchWord,
			Model model
			) {
		PaginationInfoVO<BoardVO> pagingVO = new PaginationInfoVO<BoardVO>();
		
		// 검색을 진행
		if(StringUtils.isNotBlank(searchWord)) {
			pagingVO.setSearchType(searchType);
			pagingVO.setSearchWord(searchWord);
			model.addAttribute("searchType", searchType);
			model.addAttribute("searchWord", searchWord);
		}
		
		pagingVO.setCurrentPage(currentPage); // startRow, endRow, startPage, endPage 가 결정
		int totalRecord = boardService.selectBoardCount(pagingVO); // totalRecord(총 게시글 수)
		
		pagingVO.setTotalRecord(totalRecord); // totalPage 결정
		List<BoardVO> dataList = boardService.selectBoardList(pagingVO);
		pagingVO.setDataList(dataList);
		
		model.addAttribute("pagingVO", pagingVO);
		
		return "board/list";
	}
	
	@RequestMapping(value="/detail.do", method = RequestMethod.GET)
	public String boardDetail() {
		return "board/detail";
	}
	
	@RequestMapping(value="/form.do", method = RequestMethod.GET)
	public String boardForm() {
		return "board/form";
	}
	
}
package kr.or.ddit.service;

import java.util.List;

import kr.or.ddit.vo.BoardVO;
import kr.or.ddit.vo.PaginationInfoVO;

public interface IBoardService {

	public int selectBoardCount(PaginationInfoVO<BoardVO> pagingVO);
	public List<BoardVO> selectBoardList(PaginationInfoVO<BoardVO> pagingVO);

}
package kr.or.ddit.service.impl;

import java.util.List;

import javax.inject.Inject;

import org.springframework.stereotype.Service;

import kr.or.ddit.mapper.IBoardMapper;
import kr.or.ddit.service.IBoardService;
import kr.or.ddit.vo.BoardVO;
import kr.or.ddit.vo.PaginationInfoVO;

@Service
public class BoardServiceImpl implements IBoardService {

	@Inject
	private IBoardMapper boardMapper;
	
	@Override
	public int selectBoardCount(PaginationInfoVO<BoardVO> pagingVO) {
		return boardMapper.selectBoardCount(pagingVO);
	}

	@Override
	public List<BoardVO> selectBoardList(PaginationInfoVO<BoardVO> pagingVO) {
		return boardMapper.selectBoardList(pagingVO);
	}

}
package kr.or.ddit.mapper;

import java.util.List;

import kr.or.ddit.vo.BoardVO;
import kr.or.ddit.vo.PaginationInfoVO;

public interface IBoardMapper {

	public int selectBoardCount(PaginationInfoVO<BoardVO> pagingVO);
	public List<BoardVO> selectBoardList(PaginationInfoVO<BoardVO> pagingVO);

}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
  PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  "https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="kr.or.ddit.mapper.IBoardMapper">

	<sql id="boardSearch">
		<if test="searchType != null and searchType == 'title'">
			and (bo_title like '%' || #{searchWord} || '%')
		</if>
		<if test="searchType != null and searchType == 'writer'">
			and (bo_writer like '%' || #{searchWord} || '%')
		</if>
		<if test="searchType != null and searchType == 'both'">
			and (bo_title like '%' || #{searchWord} || '%')
			and (bo_writer like '%' || #{searchWord} || '%')
		</if>
	</sql>

	<select id="selectBoardCount" parameterType="pagingVO" resultType="int">
		select count(bo_no) 
		from board 
		where 1=1 
		<include refid="boardSearch" />
	</select>
	
	<select id="selectBoardList" parameterType="pagingVO" resultType="boardVO">
		select 
			b.* 
		from (
			select 
				a.*, row_number() over (order by a.bo_no desc) rnum
			from (
				select 
					bo_no, bo_title, bo_content, bo_writer, bo_date, bo_hit 
				from board 
				where 1=1 
				<include refid="boardSearch" /> 
				order by bo_no desc
			) a
		) b
		<![CDATA[
			where b.rnum >= #{startRow} and b.rnum <= #{endRow}
		]]>
	</select>

</mapper>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
  PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  "https://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>

	<settings>
		<setting name="mapUnderscoreToCamelCase" value="true"/>
	</settings>
	
	<typeAliases>
		<typeAlias type="kr.or.ddit.vo.MemberVO" alias="memberVO"/>
		<typeAlias type="kr.or.ddit.vo.BoardVO" alias="boardVO"/>
		<typeAlias type="kr.or.ddit.vo.PaginationInfoVO" alias="pagingVO"/>
	</typeAliases>

</configuration>

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<div class="container-fluid py-4">
	<div class="row">
		<div class="col-12">
			<div class="card my-4">
				<div class="card-header p-0 position-relative mt-n4 mx-3 z-index-2">
					<div class="bg-gradient-primary shadow-primary border-radius-lg pt-4 pb-3">
						<h6 class="text-white text-capitalize ps-3">공지사항 게시판</h6>
					</div>
				</div>
				<div class="card-body px-0 pb-2">
					<div class="row">
						<div class="col-md-8"></div>
						<div class="col-md-4">
							<form class="input-group input-group-outline" id="searchForm">
								<input type="hidden" name="page" id="page"/>
								<div class="col-md-2">
									<div class="input-group input-group-static mb-4">
										<select class="form-control" id="searchType" name="searchType">
											<option value="title" <c:if test="${searchType eq 'title' }">selected</c:if>>제목</option>
											<option value="writer" <c:if test="${searchType eq 'writer' }">selected</c:if>>작성자</option>
											<option value="both" <c:if test="${searchType eq 'both' }">selected</c:if>>제목+작성자</option>
										</select>
									</div>
								</div>
								<div class="col-md-8">
									<div class="ms-md-auto">
										<label class="form-label"></label> 
										<input type="text" class="form-control" name="searchWord" value="${searchWord }">
									</div>
								</div>
								<div class="col-md-2">
									<button type="submit" class="btn btn-outline-secondary">검색</button>
								</div>
							</form>
						</div>
					</div>
				</div>
				<div class="card-body px-0 pb-2">
					<div class="table-responsive p-0">
						<table class="table align-items-center mb-0">
							<thead>
								<tr class="text-center">
									<th width="4%" class="text-dark font-weight-bolder">번호</th>
									<th width="px" class="text-dark font-weight-bolder">제목</th>
									<th width="14%" class="text-dark font-weight-bolder">작성자</th>
									<th width="14%" class="text-dark font-weight-bolder">작성일</th>
									<th width="6%" class="text-dark font-weight-bolder">조회수</th>
								</tr>
							</thead>
							<tbody>
								<c:set value="${pagingVO.dataList }" var="boardList" />
								<c:choose>
									<c:when test="${empty boardList }">
										<tr class="text-center">
											<td colspan="5" class="text-dark font-weight-bolder">조회하신 게시글이 존재하지 않습니다.</td>
										</tr>
									</c:when>
									<c:otherwise>
										<c:forEach items="${boardList }" var="board">
											<tr class="text-center">
												<td>${board.boNo }</td>
												<td class="text-dark">
													<a href="/board/detail.do?boNo=${board.boNo }">
														${board.boTitle }
													</a>
												</td>
												<td>${board.boWriter }</td>
												<td>
													<span class="text-dark text-xs font-weight-bold">${board.boDate }</span>
												</td>
												<td>
													<span class="text-dark text-xs font-weight-bold">${board.boHit }</span>
												</td>
											</tr>
										</c:forEach>
									</c:otherwise>
								</c:choose>
							</tbody>
						</table>
					</div>
				</div>
				<div class="card-footer">
					<button type="button" class="btn btn-outline-primary" id="addBtn" onclick="javascript:location.href='/board/form.do'">등록</button>
				</div>
				<nav aria-label="Page navigation example" id="pagingArea">
					${pagingVO.pagingHTML }
				</nav>
			</div>
		</div>
	</div>
</div>
<script>
	$(function(){
		var searchForm = $("#searchForm");
		var pagingArea = $("#pagingArea");
		
		pagingArea.on("click", "a", function(e){
			e.preventDefault();
			var pageNo = $(this).data("page");
			searchForm.find("#page").val(pageNo);
			searchForm.submit();
		});
	});
</script>
insert into board values(seq_board.nextval, '제목1', '내용', 'a001', sysdate, 0);
insert into board values(seq_board.nextval, '제목2', '내용', 'a001', sysdate, 0);
insert into board values(seq_board.nextval, '제목3', '내용', 'a001', sysdate, 0);
insert into board values(seq_board.nextval, '제목4', '내용', 'a001', sysdate, 0);
insert into board values(seq_board.nextval, '제목5', '내용', 'a001', sysdate, 0);
insert into board values(seq_board.nextval, '제목6', '내용', 'a001', sysdate, 0);
insert into board values(seq_board.nextval, '제목7', '내용', 'a001', sysdate, 0);
insert into board values(seq_board.nextval, '제목8', '내용', 'a001', sysdate, 0);
insert into board values(seq_board.nextval, '제목9', '내용', 'a001', sysdate, 0);
insert into board values(seq_board.nextval, '제목10', '내용', 'a001', sysdate, 0);
insert into board values(seq_board.nextval, '제목11', '내용', 'a001', sysdate, 0);
insert into board values(seq_board.nextval, '제목12', '내용', 'a001', sysdate, 0);
insert into board values(seq_board.nextval, '제목13', '내용', 'a001', sysdate, 0);
insert into board values(seq_board.nextval, '제목14', '내용', 'a001', sysdate, 0);
insert into board values(seq_board.nextval, '제목15', '내용', 'a001', sysdate, 0);

commit;


 

'대덕인재개발원_웹기반 애플리케이션' 카테고리의 다른 글

231216_SPRING CRUD 보강 6  (0) 2023.12.16
231216_SPRING CRUD 보강 5  (0) 2023.12.16
231216_SPRING CRUD 보강 3  (0) 2023.12.16
231216_SPRING CRUD 보강 2  (0) 2023.12.15
231216_SPRING CRUD 보강 1  (0) 2023.12.15