โโlombok annotation (lombok ์ฃผ์)โโ
@ToString : lombok ๋๋ถ์ Override ํด์ฃผ์ง ์์๋ VOํด๋์ค์์ ๊ทธ๋ฅ ์ธ ์์๊ฒ ๋๋ค.
@EqualsAndHashCode(exclude= {"ename", "deptno", "sal","hiredate"}) : lombok ๋๋ถ์ Override ํด์ฃผ์ง ์์๋ VOํด๋์ค์์ ๊ทธ๋ฅ ์ธ ์์๊ฒ ๋๋ค. exclude ํด์ฃผ๋ฉด ํด๋นํ์ง ์๊ฒ ๋๊ณ 4๊ฐ์ง๋ฅผ ๋นผ์ฃผ์์ผ๋ empno๋ง์ผ๋ก eqauls ๋ฅผ ๋น๊ตํ๊ฒ ๋๋ค.
@AllArgsConstructor : lombok ๋๋ถ์ ๋ชจ๋ ์๊ท๋จผํธ๋ฅผ ๊ฐ์ง ์์ฑ์๋ฅผ ๋ง๋ค์ด ์ฃผ์ง ์์๋ ๋ด๋ถ์์ ์์์ ๋ง๋ค์ด ์ค๋ค.
@NoArgsConstructor : ์๊ท๋จผํธ๊ฐ ์๋ ๊ธฐ๋ณธ ์์ฑ์๋ฅผ ๋ง๋ค์ด ์ฃผ์ง ์์๋ ๋ด๋ถ์์ ์์์ ๋ง๋ค์ด ์ค๋ค.
โโ์ฌ์ ์ ๋ณด ์ถ๊ฐ ์ญ์ ์์ ํ๊ธฐโโ
M : Emp.java
V : empAdd.jsp , empList.jsp , empUpdate.jsp , empDetail.jsp
C : EmpController.java
Service : EmpService.java
Emp.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
import java.io.Serializable;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.ToString;
@Data
@ToString
@EqualsAndHashCode(exclude= {"ename", "deptno", "sal","hiredate"})
@AllArgsConstructor
@NoArgsConstructor
public class Emp implements Serializable
{
private int empno;
private String ename;
private int deptno;
private float sal;
private java.sql.Date hiredate;
}
|
cs |
@Data : setter, getter ์ค์ ํด์ฃผ์ง ์์๋ ๋ด๋ถ์์ ๋ง๋ค์ด ์ค๋ค.
@ToString : lombok ๋๋ถ์ Override ํด์ฃผ์ง ์์๋ VOํด๋์ค์์ ๊ทธ๋ฅ ์ธ ์์๊ฒ ๋๋ค.
@EqualsAndHashCode(exclude= {"ename", "deptno", "sal","hiredate"}) : lombok ๋๋ถ์ Override ํด์ฃผ์ง ์์๋ VOํด๋์ค์์ ๊ทธ๋ฅ ์ธ ์์๊ฒ ๋๋ค. exclude ํด์ฃผ๋ฉด ํด๋นํ์ง ์๊ฒ ๋๊ณ 4๊ฐ์ง๋ฅผ ๋นผ์ฃผ์์ผ๋ empno๋ง์ผ๋ก eqauls ๋ฅผ ๋น๊ตํ๊ฒ ๋๋ค.
@AllArgsConstructor : lombok ๋๋ถ์ ๋ชจ๋ ์๊ท๋จผํธ๋ฅผ ๊ฐ์ง ์์ฑ์๋ฅผ ๋ง๋ค์ด ์ฃผ์ง ์์๋ ๋ด๋ถ์์ ์์์ ๋ง๋ค์ด ์ค๋ค.
@NoArgsConstructor : ์๊ท๋จผํธ๊ฐ ์๋ ๊ธฐ๋ณธ ์์ฑ์๋ฅผ ๋ง๋ค์ด ์ฃผ์ง ์์๋ ๋ด๋ถ์์ ์์์ ๋ง๋ค์ด ์ค๋ค.
EmpController.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
|
import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ezen.spring.web.service.EmpService;
import com.ezen.spring.web.vo.Emp;
import lombok.extern.slf4j.Slf4j;
@Controller
@RequestMapping("/emp") // get์์ฒญ์ด๋ post ์์ฒญ์ด๋ emp๋ก ์์ํ๋ฉด ์ด์ชฝ์ผ๋ก ๊ฐ์ ธ์จ๋ค.
@Slf4j
public class EmpController
{
@Autowired
private EmpService svc;
@GetMapping("/add")
public String showAddForm()
{
log.info("ํผ ์์ฒญํจ");
return "/emp/empAdd";
}
@PostMapping("/add")
@ResponseBody
public Map<String,Object> addEmp(Emp emp)
{
boolean added = svc.add(emp);
Map<String,Object> map = new HashMap<>();
map.put("added", added);
return map;
}
@GetMapping("/list")
public String list(Model m)
{
m.addAttribute("list", svc.getList());
return "/emp/empList";
}
@GetMapping("/detailEmp/empno={num}")
public String detail(@PathVariable(name="num") int num, Model m)
{
m.addAttribute("emp", svc.detailEmp(num));
return "/emp/empDetail";
}
@GetMapping("/update/empno={num}")
public String showUpdateForm(@PathVariable(name="num") int num, Model m)
{
m.addAttribute("emp", svc.detailEmp(num));
return "/emp/empUpdate";
}
@PostMapping("/updateResult")
@ResponseBody
public Map<String,Boolean> updateResult(Emp emp)
{
boolean updated = svc.update(emp);
log.info("๊ฐฑ์ ๊ฒฐ๊ณผ" +updated);
Map<String,Boolean> map = new HashMap<>();
map.put("updated", updated);
return map;
}
@PostMapping("/delete")
@ResponseBody
public Map<String,Boolean> delete(int empno)
{
boolean deleted = svc.delete(empno);
Map<String,Boolean> map = new HashMap<>();
map.put("deleted", deleted);
return map;
}
}
|
cs |
EmpService.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
|
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Service;
import com.ezen.spring.web.vo.Emp;
@Service
public class EmpService
{
private String fpath = "C:/JavaTest/springEmp.ser";
public boolean add(Emp emp)
{
List<Emp> list = deserialize();
list.add(emp);
return serialize(list);
}
public List<Emp> getList()
{
return deserialize();
}
public Emp detailEmp(int empno)
{
Emp emp = new Emp();
emp.setEmpno(empno);
List<Emp> list = deserialize();
if(list.contains(emp)) {
return list.get(list.indexOf(emp));
}
return null;
}
public boolean update(Emp emp)
{
List<Emp> list = deserialize();
if(list.contains(emp)) {
Emp findemp = list.get(list.indexOf(emp));
findemp.setDeptno(emp.getDeptno());
findemp.setSal(emp.getSal());
return serialize(list);
}
return false;
}
public boolean delete(int empno)
{
List<Emp> list = deserialize();
Emp emp = new Emp();
emp.setEmpno(empno);
if(list.contains(emp)) {
list.remove(list.indexOf(emp));
return serialize(list);
}
return false;
}
private boolean serialize(List<Emp> list)
{
try {
ObjectOutputStream objOut = new ObjectOutputStream(new FileOutputStream(fpath));
objOut.writeObject(list);
objOut.close();
return true;
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
private List<Emp> deserialize()
{
File f = new File(fpath);
List<Emp> list = null;
if(!f.exists()) {
list = new ArrayList<Emp>();
}else {
try {
ObjectInputStream objInput = new ObjectInputStream(new FileInputStream(fpath));
list = (List<Emp>)objInput.readObject();
objInput.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return list;
}
}
|
cs |
empAdd.jsp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>์ฌ์์ถ๊ฐ</title>
<script src="https://code.jquery.com/jquery-3.6.1.min.js"
integrity="sha256-o88AwQnZB+VDvE9tvIXrMQaPlFFSUTR+nldQm1LuPXQ=" crossorigin="anonymous">
</script>
<script type="text/javascript">
function addEmp()
{
var obj = $('#empadd').serialize();
$.ajax({
url : '/emp/add',
method: 'post',
data: obj,
cache : false,
dataType:'json',
success:function(res){
alert(res.added ? '์ถ๊ฐ ์ฑ๊ณต' : '์ถ๊ฐ ์คํจ');
location.href = '/emp/list'
},
error : function(xhr,status,err){
alert('์๋ฌ:' + err);
}
});
return false;
}
</script>
</head>
<body>
<h2>์ฌ์ ์ถ๊ฐ</h2>
<form id="empadd"onsubmit="return addEmp();">
์ฌ๋ฒ<input id="empno" type="number" name="empno" value="์
๋ ฅํด์ฃผ์ธ์"><br>
์ด๋ฆ<input id="ename" type="text" name="ename" value="์
๋ ฅํด์ฃผ์ธ์"><br>
๋ถ์<input id="deptno" type="number" name="deptno" value="์
๋ ฅํด์ฃผ์ธ์"><br>
์ฐ๋ด<input id="sal" type="number" name="sal" value="์
๋ ฅํด์ฃผ์ธ์"><br>
์
์ฌ์ผ<input id="hiredate" type="date" name="hiredate" value="์
๋ ฅํด์ฃผ์ธ์"><br>
<button type="submit">์ ์ฅ</button>
</form>
</body>
</html>
|
cs |
empList.jsp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>์ฌ์ ๋ชฉ๋ก ๋ณด๊ธฐ </title>
</head>
<body>
<h3> ์ฌ์ ๋ชฉ๋ก</h3>
<c:forEach var="line" items="${list}">
${line.empno} 
<a href="/emp/detailEmp/empno=${line.empno}">${line.ename}</a> 
${line.deptno} 
${line.sal} 
${line.hiredate}<br>
</c:forEach>
</body>
</html>
|
cs |
empDetail.jsp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>์ฌ์ ์์ธ ๋ณด๊ธฐ ๋ณด๊ธฐ </title>
</head>
<script src="https://code.jquery.com/jquery-3.6.1.min.js"
integrity="sha256-o88AwQnZB+VDvE9tvIXrMQaPlFFSUTR+nldQm1LuPXQ=" crossorigin="anonymous">
</script>
<script type="text/javascript">
function deleteEmp(empno)
{
if(!confirm("์ ๋ง๋ก ํ์ฌ ์ฌ์์ ์ญ์ ํ๊ฒ ์ต๋๊น?")) return;
$.ajax({
url:'/emp/delete',
method:'post',
data: {'empno':${emp.empno}},
cache : false,
dataType: 'json',
success: function(res){
if(res.deleted){
alert(res.deleted ? '์ญ์ ์ฑ๊ณต':'์๋ฌ');
location.href ='/emp/list';
}
},
error : function(xhr,status,err){
alert(err);
}
});
return false;
}
</script>
<body>
<h3> ์ฌ์ ์์ธ์ ๋ณด</h3>
์ฌ๋ฒ: ${emp.empno} 
์ด๋ฆ: ${emp.ename} 
๋ถ์๋ฒํธ: ${emp.deptno} 
๊ธ์ฌ: ${emp.sal} 
์
์ฌ์ผ: ${emp.hiredate}<br>
[<a href="/emp/list"> ๋ฑ๋ก๋ ์ด์ฉ์ ์ ๋ณด ๋ณด๊ธฐ </a>]
[<a href="/emp/update/empno=${emp.empno}"> ์์ </a>]
[<a href="javascript:deleteEmp(${emp.empno});"> ์ญ์ </a>]
</body>
</html>
|
cs |
empUpdate.jsp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>์ฌ์ ์์ </title>
<script src="https://code.jquery.com/jquery-3.6.1.min.js"
integrity="sha256-o88AwQnZB+VDvE9tvIXrMQaPlFFSUTR+nldQm1LuPXQ=" crossorigin="anonymous">
</script>
<script type="text/javascript">
function updateEmp()
{
var obj = $('#empupdate').serialize();
$.ajax({
url : '/emp/updateResult',
method: 'post',
data: obj,
cache : false,
dataType:'json',
success:function(res){
alert(res.updated ? '์์ ์ฑ๊ณต' : '์์ ์คํจ');
location.href = '/emp/list'
},
error : function(xhr,status,err){
alert('์๋ฌ:' + err);
}
});
return false;
}
</script>
</head>
<body>
<h2>์ฌ์์ ๋ณด ์์ </h2>
<form id="empupdate"onsubmit="return updateEmp();">
<input type="hidden" name="empno" value="${emp.empno }">
์ฌ๋ฒ: ${emp.empno }<br>
์ด๋ฆ: ${emp.ename }<br>
๋ถ์<input id="deptno" type="number" name="deptno" value="${emp.deptno }"><br>
์ฐ๋ด<input id="sal" type="number" name="sal" value="${emp.sal}"><br>
์
์ฌ์ผ: ${emp.hiredate }<br>
<button type="submit">์ ์ฅ</button>
</form>
</body>
</html>
|
cs |
์คํ๊ฒฐ๊ณผ :
๋๊ธ