모듈화 시켜놓으면 원하는 페이지에 가져다 쓰면
전부 페이징 처리 가능함
package com.oracle.oBootMybatis01.service;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
public class Paging {
private int currentPage =1;
private int rowPage = 10;
private int pageBlock =10;
private int start ;
private int end;
private int startPage ;
private int endPage;
private int total;
private int totalPage;
// 25 기본이 1이라고 가정
public Paging(int total, String currentPage1) {
this.total = total; //140
if(currentPage1 != null) {
this.currentPage = Integer.parseInt(currentPage1); //2
}
//1 10
start = (currentPage -1 ) * rowPage +1; //시작시 1 11
end = start +rowPage -1; //시작시 10 20
//25 / 10
totalPage = (int) Math.ceil((double)total/ rowPage); //시작시 3 5 14
//2 2
startPage = currentPage - (currentPage-1) % pageBlock; //시작시 1
endPage =startPage + pageBlock -1; //10
//10 14
//공갈페이지 방지
if(endPage >totalPage ) {
endPage=totalPage;
}
}
}
package com.oracle.oBootMybatis01.controller;
import java.util.List;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import com.oracle.oBootMybatis01.model.Emp;
import com.oracle.oBootMybatis01.service.EmpService;
import com.oracle.oBootMybatis01.service.Paging;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
@Controller
@RequiredArgsConstructor
@Slf4j
public class EmpController {
private final EmpService es;
@RequestMapping(value="listEmp") //listEmp 걸면 내가 떠오른다
//empList조회 --> Emp ,페이지 , 모델을 데리고 조회
public String empList(Emp emp , String currentPage, Model model) {
//페이징 작업
Paging page = new Paging(totalEmp, currentPage);
//parameter emp --> 페이지만 추가 (setting)
emp.setStart(page.getStart()); //시작시 1
emp.setEnd(page.getEnd()); //시작시 10
listEmp.size()->" +listEmp.size());
model.addAttribute("page", page );
return "list";
}
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ include file="header.jsp" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>회원관리 </title>
</head>
<body>
<h1>회원관리 </h1>
<a href="wirteForm">입력</a>
<h5> 사원수 : ${totalEmp}</h5>
<c:set var="num" value="${page.total-page.start+1 }"></c:set>
<table>
<tr><th>번호 </th><th>사번 </th><th>이름 </th><th>업무 </th><th>급여 </th></tr>
<c:forEach var="emp" items="${listEmp }" >
<tr><td>${num }</td> <td>${emp.empno }</td>
<td><a href="detailEmp?empno=${emp.empno }">${emp.ename }</a></td>
<td>${emp.job }</td> <td>${emp.sal }</td></tr>
<c:set var="num" value="${num - 1 }"></c:set>
</c:forEach>
</table>
<!-- 페이지버튼 -->
<c:if test="${page.startPage>page.pageBlock }">
<a href="listEmp?currentPage=${page.startPage-page.pageBlock }">[이전]</a>
</c:if>
<c:forEach var="i" begin="${page.startPage }" end="${page.endPage }">
<a href="listEmp?currentPage=${i }"> [${i }]</a>
</c:forEach>
<c:if test="${page.endPage>page.totalPage }">
<a href="listEmp?currentPage=${page.startPage+page.pageBlock }">[다음]</a>
</c:if>
</body>
</html>
<body>
<h1>회원관리</h1>
<a href="/writeForm">회원가입</a><p>
<a href="/listEmp">회원목록</a><p>
</body>
🔽 페이징 처리 완
// 1. EmpService안에 detailEmp method 선언
// 1) parameter : empno
// 2) Return Emp
//
// 2. EmpDao detailEmp method 선언
//// mapper ID , Parameter
// emp = session.selectOne("tkEmpSelOne", empno);
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ include file="header.jsp" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h2>직원정보</h2>
<table>
<tr><th>사번</th><td>${emp.empno }</td></tr>
<tr><th>이름</th><td>${emp.ename }</td></tr>
<tr><th>업무</th><td>${emp.job }</td></tr>
<tr><th>급여</th><td>${emp.sal }</td></tr>
<tr><th>입사일</th><td>${emp.hiredate }</td></tr>
<tr><th>보너스</th><td>${emp.com }</td></tr>
<tr><th>관리자사번</th><td>${emp.mgr }</td></tr>
<tr><th>부서코드</th><td>${emp.deptno }</td></tr>
<tr><td colspan="2">
<input type="button" value="목록"
onclick="location.href='list'" >
<input type="button" value="수정"
onclick="location.href='updateFormEmp?empno=${emp.empno}'" >
<input type="button" value="삭제"
onclick="location.href='delete?empno=${emp.empno}'"> </td>
</tr>
</table>
</body>
</html>
//회원목록 상세조회
@GetMapping(value = "detailEmp")
public String detailEmp(int empno, Model model) {
System.out.println("EmpController start detail . ." );
Emp emp = es.detailEmp(empno);
model.addAttribute("emp",emp);
return "detailEmp" ;
}
Emp detailEmp(int empno);
@Override
public Emp detailEmp(int empno) {
Emp emp = null; //다오를 연결해주고
emp = ed.detailEmp(empno); //다오에서 받아온 empno를 가져옴
return emp;
}
Emp detailEmp(int empno);
@Override
public Emp detailEmp(int empno) {
System.out.println("EmpDaoImpl detailEmp start. . . ");
Emp emp = new Emp();
try {
emp = session.selectOne("tkEmpSelOne" , empno);
System.out.println("EmpDaoImpl detailEmp getEname-> "+emp.getEname());
} catch (Exception e) {
System.out.println("EmpDaoImpl detailEmp Exception-> "+e.getMessage());
}
return emp;
}
한줄가져올떄는 selectOne
여러줄 가져올때는 selectList
하나일때는 int ,String
여러개일때는 Dto타입
<select id="tkEmpSelOne" parameterType="int" resultType="Emp">
SELECT * FROM emp WHERE empno = #{empno}
</select>
🔽회원목록 상세보기 완
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ include file="header.jsp" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h2>직원정보</h2>
<form action="updateEmp" method="post">
<input type="hidden" name="empno" value="${emp.empno }">
<table>
<tr><th>사번</th><td>${emp.enpno }</td></tr>
<tr><th>이름</th><td>
<input type="text" name="ename" required="required" value="${emp.ename }"></td></tr>
<tr><th>업무</th><td>
<input type="text" name="job" required="required" value="${emp.job }"></td></tr>
<tr><th>급여</th><td>
<input type="number" name="sal" required="required" value="${emp.sal }"></td></tr>
<tr><th>입사일</th><td>
<input type="date" name="hiredate" id="hiredate" value="${emp.hiredate }">
<input type="button" value="기본일자확인" onclick="inDate()"></td></tr>
<tr><th>보너스</th><td>
<input type="number" name="comm" required="required" value="${emp.comm }"></td></tr>
<tr><th>관리자사번</th><td>
<input type="number" name="mgr" value="${emp.mgr }"></td></tr>
<tr><th>부서코드</th><td>
<input type="number" name="deptno" required="required" value="${emp.deptno }"></td></tr>
</table>
<tr><td colspan="2">
<input type="submit" value="확인">
</td></tr>
</form>
</body>
</html>
//회원정보 수정 화면
@GetMapping(value = "updateFromEmp")
public String updateFromEmp(int empno, Model model) {
System.out.println(" EmpController updateFromEmp start . . . ");
Emp emp = es.detailEmp(empno);
//문제
//1.Dto String hitedate
//2.View : 단순 조회 ok, jsp에서 input type = "date" 문제발생
//3.해결책 :년 월일만 짤라 넣어 주어야 함 .
String hiredate = "";
if(emp.getHiredate() != null) {
hiredate = emp.getHiredate().substring(0,10);
emp.setHiredate(hiredate);
}
model.addAttribute("emp",emp);
return "updateFromEmp";
}
// 1. EmpService안에 updateEmp method 선언
// 1) parameter : Emp
// 2) Return updateCount (int)
//
// 2. EmpDao updateEmp method 선언
//// mapper ID , Parameter
// updateCount = session.update("TKempUpdate",emp);
@PostMapping(value = "updateEmp")
public String updateEmp(Emp emp, Model model) {
log.info("EmpController updateEmp start. . . ");
int updateCount = es.updateEmp(emp);
model.addAttribute("uptcnt", updateCount);
return "redirect:listEmp";
}
int updateEmp(Emp emp);
@Override
public int updateEmp(Emp emp) {
System.out.println(" EmpServiceImpl updateEmp Start . . . ");
int updateEmp =0;
updateEmp =ed.updateEmp(emp);
return updateEmp;
}
int updateEmp(Emp emp);
@Override
public int updateEmp(Emp emp) {
System.out.println("EmpDaoImpl updateEmp start. . . ");
int updateCount = 0;
try {
updateCount=session.update("tkempUpdate", emp);
System.out.println("EmpDaoImpl updateEmp getEname-> "+emp.getEname());
} catch (Exception e) {
System.out.println("EmpDaoImpl updateEmp Exception-> "+e.getMessage());
}
return updateCount;
}
<update id="tkempUpdate" parameterType="Emp" >
UPDATE emp SET ename = #{ename},
job = #{job},
sal = #{sal},
hiredate = #{hiredate},
<!-- 필요하면 if문걸어도됨 -->
<if test ="comm!=null"> comm= #{comm}, </if>
mgr = #{mgr},
deptno = #{deptno}
WHERE empno = #{empno}
</update>
🔽회원수정 완
사용자에게(웹 브라우저에서) URL이 변경되어 보이는지 여부일 것이다.
1 )return "redirect:listEmp";
🔽수정해도 값 안나타남
2 )return "forward:listEmp";
🔽수정하면 파라메타 가지고 뿌려줌
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ include file="header.jsp" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h2>직원정보 입력</h2>
<c:if test="${msg!=null}">${msg}</c:if>
<form action="write" method="post" name="frm">
<table>
<tr><th>사번</th><td>
<input type="number" name="empno" required="required" maxlength="4" value="${empno }">
<input type="button" value="중복확인" onclick="chk()"></td></tr>
<tr><th>이름</th><td> <input type="text" name="ename" required="required"></td></tr>
<tr><th>업무</th><td><input type="text" name="job" required="required"></td></tr>
<tr><th>급여</th><td> <input type="number" name="sal" required="required"></td></tr>
<tr><th>입사일</th><td><input type="date" name="hiredater" required="required"></td></tr>
<tr><th>보너스</th><td><input type="number" name="comm" required="required"></td></tr>
<tr><th>관리자사번</th><td>
<select name="mgr">
<c:forEach var="emp" items="${empMngList}">
<option value="${emp.empno}">${emp.ename}</option>
</c:forEach>
</select></td>
</tr>
<tr><th>부서코드</th><td>
<select name="deptno">
<c:forEach var="dept" items="${deptList}">
<option value="${dept.deptno}">${dept.dname}</option>
</c:forEach>
</select></td>
</tr>
<tr><td colspan="2"><input type="submit" value="확인"></td></tr>
</table>
</form>
</body>
</html>
//직원정보입력 화면
@RequestMapping( value = "writeFormEmp")
public String writeFormEmp(Model model) {
//관리자 사번만 GET
List<Emp> empList = es.listManager();
log.info("EmpController empList start. . . ");
//부서 (코드, 부서명)
List<Dept> deptList = es.deptSelect();
log.info("EmpController deptList start. . . ");
model.addAttribute("empMngList" , empList );
model.addAttribute("deptList" , deptList);
return "writeFormEmp";
}
//관리자 사번만 GET
List<Emp> listManager();
//부서 (코드, 부서명)
List<Dept> deptSelect();
//emp -> 관리자
@Override
public List<Emp> listManager() {
System.out.println(" EmpServiceImpl listManager Start . . . ");
List<Emp> empList = null;
listManager = ed.empList();
return empList;
}
//dept 정보
@Override
public List<Dept> deptSelect() {
System.out.println(" EmpServiceImpl deptSelect Start . . . ");
List<Dept> deptSelect = null;
deptSelect=dd.deptSelect();
System.out.println("deptSelect.size()->"+deptSelect.size());
return deptSelect;
}
//관리자 사번만 GET
List<Emp> listManager();
//관리자 사번만 GET
@Override
public List<Emp> listManager () {
// emp 관리자만 Select Naming Rule
// empList = session.selectList("tKSelectManager");
System.out.println("EmpDaoImpl listManager start. . . ");
List<Emp> empList = null;
try {
empList = session.selectList("tKSelectManager" );
} catch (Exception e) {
System.out.println("EmpDaoImpl listManagersException-> "+e.getMessage());
}
return empList;
}
//관리자 사번만 GET
<select id="tKSelectManager" parameterType="Emp" resultType="Emp">
SELECT *
FROM emp
WHERE empno IN ( select mgr from emp)
</select>
//부서 (코드, 부서명)
List<Dept> deptSelect();
인터페이스
//부서 (코드, 부서명)
package com.oracle.oBootMybatis01.dao;
import java.util.List;
import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Repository;
import com.oracle.oBootMybatis01.model.Dept;
import lombok.RequiredArgsConstructor;
@Repository
@RequiredArgsConstructor
public class DeptDaoImlp implements DeptDao {
private final SqlSession session;
@Override
public List<Dept> deptSelect() {
// deptList = session.selectList("tKselectDept");
System.out.println("EmpDaoImpl deptSelect start. . . ");
List<Dept> deptList = null;
try {
deptList=session.selectList("tkSelectDept");
} catch (Exception e) {
System.out.println("EmpDaoImpl deptSelect Exception-> "+e.getMessage());
}
return deptList;
}
}
<?xml version="1.0" encoding="UTF-8" ?>
<!-- ======= mapper 기본설정 ======= -->
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.oracle.oBootMybatis01.DeptMapper">
<select id="tkSelectDept" resultType="Dept">
select * from dept
</select>
</mapper>
🔽직원정보 입력화면 완