configuration.xml
pom.xml
환경설정
web.xml
servlet맵핑, 필터를 이용한 한글인코딩
servlet-context.xml
<context:component-scan base-package="myBatis2" />
myBatis2폴더 하위파일들을 다 불러온다.
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
ViewResolver 불러오기
root-context.xml
DB접속을 위한 bean 3개 추가 되어 있음(스프링으로 DB연결)
controller, dao, service
테이블당 각각 만들어야 한다. ex) emp, dept
dao
SQL문 실행을 위한 @Autowired
@Autowired
private SqlSessionTemplate sst;
@Autowired
private SqlSessionTemplate st;
@RequestMapping("deptInsertForm.do")
public String deptInsertForm() {
return "deptInsertForm";
}
<%@ 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>
<div class="container" align="center">
<h2 class="text-primary">부서정보 입력</h2>
<form action="deptInsert.do" method="post">
<table class="table table-hover">
<tr>
<td>부서코드</td>
<td><input type="number" maxlength="2" name="deptno"
required="required" autofocus="autofocus"></td>
</tr>
<tr>
<td>부서명</td>
<td><input type="text" name="dname" required="required"></td>
</tr>
<tr>
<td>근무지</td>
<td><input type="text" name="loc" required="required"></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="확인"></td>
</tr>
</table>
</form>
</div>
</body>
</html>
@RequestMapping("deptInsert.do")
public String deptInsert(@ModelAttribute Dept dept,
Model model) {
System.out.println("deptno:"+dept.getDeptno());
Dept dt = ds.select(dept.getDeptno());
if (dt == null) { // 중복된 부서 없음
int result = ds.insert(dept);
model.addAttribute("result", result);
} else { // 중복된 부서 있음
model.addAttribute("msg", "이미 있는 데이터입니다");
model.addAttribute("result", -1);
}
return "deptInsert";
}
<%@ 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>
<c:if test="${result > 0 }">
<script type="text/javascript">
alert("입력 성공");
location.href = "deptList.do";
</script>
</c:if>
<c:if test="${result == 0 }">
<script type="text/javascript">
alert("입력 실패");
history.go(-1);
</script>
</c:if>
<c:if test="${result == -1 }">
<script type="text/javascript">
alert("${msg}");
history.go(-1);
</script>
</c:if>
</body>
</html>
@RequestMapping("empAllList.do")
public String empAllList(Model model) {
List<Emp> list = es.empAllList();
model.addAttribute("list", list);
return "emp/empAllList";
}
<%@ 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>
<div class="container">
<table class="table table-striped">
<tr>
<td>사번</td>
<td>이름</td>
<td>업무</td>
<td>급여</td>
<td>부서코드</td>
<td>부서명</td>
<td>근무지</td>
</tr>
<c:forEach var="emp" items="${list }">
<tr>
<td>${emp.empno }</td>
<td>${emp.ename }</td>
<td>${emp.job }</td>
<td>${emp.sal }</td>
<td>${emp.deptno }</td>
<td>${emp.dname }</td>
<td>${emp.loc }</td>
</tr>
</c:forEach>
</table>
<a href="deptList.do" class="btn btn-default">부서목록</a>
</div>
</body>
</html>
@RequestMapping("empList.do")
public String empList(int deptno, Model model) {
Dept dept = ds.select(deptno);
List<Emp> list = es.list(deptno);
model.addAttribute("dept", dept);
model.addAttribute("list", list);
return "emp/empList";
}
<%@ 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>
<script type="text/javascript">
$(function() {
$('#list').load('deptList.do');
});
</script>
</head>
<body>
<div class="container" align="center">
<h2 class="text-primary">${dept.dname}직원목록</h2>
<table class="table table-striped">
<tr>
<td>사번</td>
<td>이름</td>
<td>업무</td>
<td>급여</td>
<td>부서코드</td>
</tr>
<tr>
<c:if test="${empty list }">
<tr>
<td colspan="5">직원이 없는 부서입니다</td>
</tr>
</c:if>
<c:if test="${not empty list }">
<c:forEach var="emp" items="${list }">
<tr>
<td>${emp.empno }</td>
<td><a href="empView.do?empno=${emp.empno}"
class="btn btn-info">${emp.ename}</a></td>
<td>${emp.job}</td>
<td>${emp.sal}</td>
<td>${emp.deptno}</td>
</tr>
</c:forEach>
</c:if>
</table>
<a href="deptList.do" class="btn btn-success">부서목록</a>
<a href="empInsertForm.do" class="btn btn-success">직원 입력</a>
<div id="list"></div>
</div>
</body>
</html>
@RequestMapping("empInsertForm.do")
public String empInsertForm(Model model) {
List<Dept> deptList = ds.list();
List<Emp> empList = es.empList();
model.addAttribute("deptList", deptList);
model.addAttribute("empList", empList);
return "emp/empInsertForm";
}
<%@ 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>
<script type="text/javascript">
$(function() {
$('#dupCheck').click(function() {
var empno = $('#empno').val();
if (!empno) {
alert('사번 입력후에 체크');
$('#empno').focus();
return false;
}// $.post("요청이름","전달될 값","콜백함수");
$.post('dupCheck.do', 'empno=' + empno, function(msg) {
alert(msg);
});
});
});
</script>
</head>
<body>
<div class="container">
<h2 class="text-primary">직원 등록</h2>
<form action="empInsert.do" method="post">
<table class="table table-bordered">
<tr>
<td>사번</td>
<td><input type="number" name="empno" required="required"
id="empno" autofocus="autofocus">
<input type="button" value="중복" id="dupCheck"></td>
</tr>
<tr>
<td>이름</td>
<td><input type="text" name="ename" required="required"></td>
</tr>
<tr>
<td>업무</td>
<td><input type="text" name="job" required="required"></td>
</tr>
<tr>
<td>관리자</td>
<td><select name="mgr">
<c:forEach var="emp" items="${empList }">
<option value="${emp.empno}">${emp.ename}</option>
</c:forEach>
</select>
</td>
</tr>
<tr>
<td>입사일</td>
<td><input type="date" name="hiredate" required="required"></td>
</tr>
<tr>
<td>급여</td>
<td><input type="number" name="sal" required="required"></td>
</tr>
<tr>
<td>보너스</td>
<td><input type="number" name="comm" required="required"></td>
</tr>
<tr>
<td>부서코드</td>
<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>
</div>
</body>
</html>
// 사번 중복검사
@RequestMapping("dupCheck.do")
public String dupCheck(int empno, Model model) {
System.out.println("empno:"+empno);
Emp emp = es.select(empno);
String msg = "";
if (emp != null) // 중복 사번
msg = "이미 있는 데이터입니다";
else // 사용가능한 사번
msg = "사용 가능한 사번 입니다";
model.addAttribute("msg", msg);
return "emp/dupCheck";
}
// 직원 등록
@RequestMapping("empInsert.do")
// public String empInsert(Emp emp, String hiredate1, Model model) {
public String empInsert(Emp emp, Model model) {
// emp.setHiredate(Date.valueOf(hiredate1)); // String -> Date 형변환
int result = es.insert(emp);
model.addAttribute("result", result);
model.addAttribute("deptno", emp.getDeptno());
return "emp/empInsert";
}
<%@ 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>
<c:if test="${result > 0 }">
<script type="text/javascript">
alert("입력 성공");
location.href = "empList.do?deptno=${deptno}";
</script>
</c:if>
<c:if test="${result <= 0 }">
<script type="text/javascript">
alert("입력 실패");
history.go(-1);
</script>
</c:if>
</body>
</html>
// 직원 상세
@RequestMapping("empView.do")
public String empView(int empno, Model model) {
Emp emp = es.select(empno);
model.addAttribute("emp", emp);
return "emp/empView";
}
<%@ 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>
<script type="text/javascript">
$(function() {
$('#list').load('empList.do?deptno=${emp.deptno}');
});
</script>
</head>
<body>
<div class="container">
<h2 class="text-primary">직원 상세정보</h2>
<table class="table table-bordered">
<tr>
<td>사번</td>
<td>${emp.empno }</td>
</tr>
<tr>
<td>이름</td>
<td>${emp.ename}</td>
</tr>
<tr>
<td>업무</td>
<td>${emp.job }</td>
</tr>
<tr>
<td>관리자</td>
<td>${emp.mgr }</td>
</tr>
<tr>
<td>입사일</td>
<td>${emp.hiredate }</td>
</tr>
<tr>
<td>급여</td>
<td>${emp.sal }</td>
</tr>
<tr>
<td>보너스</td>
<td>${emp.comm }</td>
</tr>
<tr>
<td>부서코드</td>
<td>${emp.deptno }</td>
</tr>
</table>
<a href="empUpdateForm.do?empno=${emp.empno}" class="btn btn-info">수정</a>
<a class="btn btn-danger" href="empDelete.do?empno=${emp.empno}">삭제</a>
<a href="empList.do?deptno=${emp.deptno}" class="btn btn-default">목록</a>
<div id="list"></div>
</div>
</body>
</html>
@RequestMapping("empUpdateForm.do")
public String empUpdateForm(int empno, Model model) {
Emp emp = es.select(empno);
List<Dept> deptList = ds.list();
model.addAttribute("emp", emp);
model.addAttribute("deptList", deptList);
return "emp/empUpdateForm";
}
<%@ 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>
<div class="container" align="center">
<h2 class="text-primary">직원정보 수정</h2>
<form action="empUpdate.do" method="post">
<table class="table table-bordered">
<tr>
<td>사번</td>
<td><input type="text" name="empno" readonly="readonly"
value="${emp.empno}"></td>
</tr>
<tr>
<td>이름</td>
<td><input type="text" name="ename" required="required"
value="${emp.ename }"></td>
</tr>
<tr>
<td>업무</td>
<td><input type="text" name="job" required="required"
value="${emp.job }"></td>
</tr>
<tr>
<td>급여</td>
<td><input type="text" name="sal" required="required"
value="${emp.sal}"></td>
</tr>
<tr>
<td>보너스</td>
<td><input type="text" name="comm" required="required"
value="${emp.comm }"></td>
</tr>
<tr>
<td>부서코드</td>
<td><select name="deptno">
<c:forEach var="dept" items="${deptList}">
<c:if test="${emp.deptno==dept.deptno}">
<option value="${dept.deptno}" selected="selected">
${dept.dname}(${dept.deptno})</option>
</c:if>
<c:if test="${emp.deptno!=dept.deptno}">
<option value="${dept.deptno}">${dept.dname}(${dept.deptno})</option>
</c:if>
</c:forEach>
</select></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="수정"></td>
</tr>
</table>
</form>
</div>
</body>
</html>
// 사원 정보 수정
@RequestMapping("empUpdate.do")
public String empUpdate(Emp emp, Model model) {
int result = es.update(emp);
model.addAttribute("deptno", emp.getDeptno());
model.addAttribute("result", result);
return "emp/empUpdate";
}
<%@ 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>
<c:if test="${result > 0 }">
<script type="text/javascript">
alert("수정 성공");
location.href = "empList.do?deptno=${deptno}";
</script>
</c:if>
<c:if test="${result <= 0 }">
<script type="text/javascript">
alert("수정 실패");
history.go(-1);
</script>
</c:if>
</body>
</html>