페이징
PageVo
@Data
public class PageVo {
private int currentPage;
private int cntPerPage;
private int pageBtnCnt;
private int totalRow;
private int startRow;
private int endRow;
private int startPage;
private int endPage;
private int lastPage;
public PageVo(String currentPageStr, int cntPerPage, int pageBtnCnt, int totalRow) {
int currentPage = Integer.parseInt(currentPageStr);
this.currentPage = currentPage;
this.cntPerPage = cntPerPage;
this.pageBtnCnt = pageBtnCnt;
this.totalRow = totalRow;
calc(currentPage, cntPerPage, pageBtnCnt, totalRow);
}
private void calc(int currentPage, int cntPerPage, int pageBtnCnt, int totalRow) {
this.setEndRow(this.getCurrentPage() * this.getCntPerPage());
this.setStartRow(this.getEndRow() - this.getCntPerPage() + 1);
int lastPage = this.getTotalRow() / this.getCntPerPage();
if(this.getTotalRow() % this.getCntPerPage() > 0) {
lastPage++;
}
this.setLastPage(lastPage);
int endPage = this.getCurrentPage() / this.getPageBtnCnt();
if(this.getCurrentPage() % this.getPageBtnCnt() > 0) {
endPage++;
}
if(endPage > lastPage) {
endPage = lastPage;
}
this.setEndPage(endPage * this.getPageBtnCnt());
this.setStartPage(this.getEndPage() - this.getPageBtnCnt() + 1);
}
}
list.jsp
<body>
<%@ include file="/WEB-INF/views/common/header.jsp" %>
<div id="div-main">
<div style="text-align:center;">
<h1>공지사항</h1>
</div>
<table border="1" style="margin:auto;">
<thead>
<tr>
<th><input type="checkbox" id="allCheck"></th>
<th>글번호</th>
<th>제목</th>
<th>작성자</th>
<th>작성시간</th>
</tr>
</thead>
<tbody>
<c:forEach items="${list}" var="n">
<tr>
<td><input type="checkbox" class="checkbox-del" value="${n.no}"></td>
<td>${n.no}</td>
<td>${n.title}</td>
<td>${n.userNick}</td>
<td>${n.writeDate}</td>
</tr>
</c:forEach>
</tbody>
</table>
<br><br>
<c:if test="${page.startPage != 1}">
<a href="${page.startPage - 1}">이전</a>
</c:if>
<c:forEach var="i" begin="${page.startPage}" end="${page.endPage}">
<c:if test="${page.currentPage != i and i <= page.lastPage}">
<a href="${root}/notice/list/${i}">${i}</a>  
</c:if>
<c:if test="${page.currentPage == i and i <= page.lastPage}">${i}  </c:if>
</c:forEach>
<c:if test="${page.endPage < page.lastPage}">
<a href="${page.endPage + 1}">다음</a>
</c:if>
<br><br>
<a href="${root}/notice/write">공지 작성</a>
<button onclick="del();">삭제하기</button>
</div>
<script type="text/javascript">
let allCheck = document.querySelector('thead input[type=checkbox]');
let delArr = document.getElementsByClassName('checkbox-del');
allCheck.onchange = function(e){
console.log(this.checked);
if(this.checked){
for(let i=0; i<delArr.length; i++){
delArr[i].checked = true;
}
}else{
for(let i=0; i<delArr.length; i++){
delArr[i].checked = false;
}
}
}
function del() {
let delArr = document.getElementsByClassName('checkbox-del');
let result = "";
for(let i = 0; i<delArr.length; i++){
let t = delArr[i];
if(t.checked){
console.log(t.value);
result += t.value + ',';
}
}
$.ajax({
url : "${root}/notice/delete",
data : {"str" : result},
type : "post",
success : function(data){
console.log(data);
},
error : function(error){
console.log(error)
},
complete : function(){
window.location.reload();
}
});
}
</script>
</body>
notice-mapper.xml
<mapper namespace="notice">
<insert id="insertNotice" parameterType="noticeVo">
INSERT INTO NOTICE
(
NO
,TITLE
,CONTENT
,WRITER
)
VALUES
(
NOTICE_SEQ.NEXTVAL
,#{title}
,#{content}
,#{writer}
)
</insert>
<select id="getNoticeList" resultType="noticeVo">
SELECT * FROM
(
SELECT ROWNUM RN, T.*
FROM
(
SELECT *
FROM NOTICE N
JOIN MEMBER M ON (N.WRITER = M.USER_NO)
WHERE N.DEL = 'N'
ORDER BY N.NO
) T
)
WHERE RN BETWEEN #{startRow} AND #{endRow}
</select>
<update id="deleteNotice" >
UPDATE NOTICE
SET
DEL = 'Y'
WHERE NO IN
<foreach collection="array" item="n" open="(" close=")" separator=",">
#{n}
</foreach>
</update>
<select id="getNoticeCnt" resultType="int">
SELECT COUNT(NO)
FROM NOTICE
WHERE DEL = 'N'
</select>
</mapper>
NoticeController
@Controller
@RequestMapping("notice")
@Slf4j
public class NoticeController {
@Autowired
private NoticeService service;
@GetMapping(value = {"/list/{page}", "/list"})
public String list(Model model, @PathVariable(required=false) String page) throws Exception {
if(page == null) page = "1";
int cntPerPage = 10;
int pageBtnCnt = 5;
int totalRow = service.getNoticeCnt();
PageVo pageVo = new PageVo(page, cntPerPage, pageBtnCnt, totalRow);
List<NoticeVo> list = service.getNoticeList(pageVo);
model.addAttribute("list", list);
model.addAttribute("page", pageVo);
return "notice/list";
}
@GetMapping("write")
public String write() {
return "notice/write";
}
@PostMapping("write")
public String write(NoticeVo vo, HttpServletRequest req) throws Exception {
System.out.println(vo);
int result = service.write(vo);
if(result>0) {
return "redirect:/notice/list";
}else {
req.setAttribute("msg", "공지사항 작성 실패");
return "error/errorPage";
}
}
@PostMapping("delete")
@ResponseBody
public String delete(String str) throws Exception {
System.out.println(str);
System.out.println(str.length()/2);
int result = service.deleteNotice(str);
log.warn("선택한 row 개수 : {}", result);
if(result == str.length()/2) {
return "ok";
}else {
return "fail_" + result;
}
}
}
NoticeService 인페
public interface NoticeService {
int write(NoticeVo vo) throws Exception;
List<NoticeVo> getNoticeList(PageVo pageVo) throws Exception;
int deleteNotice(String str) throws Exception;
int getNoticeCnt() throws Exception;
}
NoticeServiceImpl
@Service
public class NoticeServiceImpl implements NoticeService{
@Autowired
private NoticeDao dao;
@Override
public int write(NoticeVo vo) throws Exception {
return dao.write(vo);
}
@Override
public List<NoticeVo> getNoticeList(PageVo pageVo) throws Exception {
return dao.getNoticeList(pageVo);
}
@Override
public int deleteNotice(String str) throws Exception {
String[] delArr = str.split(",");
return dao.deleteNotice(delArr);
}
@Override
public int getNoticeCnt() throws Exception {
return dao.getNoticeCnt();
}
}
NoticeDao 인페
public interface NoticeDao {
int write(NoticeVo vo) throws Exception;
List<NoticeVo> getNoticeList(PageVo pageVo) throws Exception;
int deleteNotice(String[] delArr) throws Exception;
int getNoticeCnt() throws Exception;
}
NoticeDaoImpl
@Repository
public class NoticeDaoImpl implements NoticeDao{
@Autowired
private SqlSession sqlSession;
@Override
public int write(NoticeVo vo) throws Exception {
return sqlSession.insert("notice.insertNotice", vo);
}
@Override
public List<NoticeVo> getNoticeList(PageVo pageVo) throws Exception {
return sqlSession.selectList("notice.getNoticeList", pageVo);
}
@Override
public int deleteNotice(String[] delArr) throws Exception {
return sqlSession.update("notice.deleteNotice", delArr);
}
@Override
public int getNoticeCnt() throws Exception {
return sqlSession.selectOne("notice.getNoticeCnt");
}
}