Framework/Spring Framework

Spring ํ”„๋ ˆ์ž„์›Œํฌ : lombok ํ™œ์šฉํ•˜๊ธฐ, Annotation( GetMapping, PostMapping )ํ™œ์šฉํ•˜๊ธฐ

ํ”„๋กœ๊ทธ๋ž˜๋จธ ์˜ค์›” 2022. 12. 15.

โ—โ—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}&emsp;
   <a href="/emp/detailEmp/empno=${line.empno}">${line.ename}</a>&emsp;
   ${line.deptno}&emsp;
   ${line.sal}&emsp;
   ${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}&emsp;
์ด๋ฆ„:   ${emp.ename}&emsp;
๋ถ€์„œ๋ฒˆํ˜ธ:   ${emp.deptno}&emsp;
๊ธ‰์—ฌ:   ${emp.sal}&emsp;
์ž…์‚ฌ์ผ:   ${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

 

 

์‹คํ–‰๊ฒฐ๊ณผ : 

์ฒซ ์‹คํ–‰ ํ™”๋ฉด

 

 

์‚ฌ์› ์ •๋ณด ์ถ”๊ฐ€ ์„ฑ๊ณต

 

์‚ฌ์› ์ •๋ณด ๋ณด๊ธฐ

 

์‚ฌ์› ์ƒ์„ธ๋ณด๊ธฐ

 

์‚ฌ์› ์ •๋ณด ์ˆ˜์ •

 

์‚ฌ์› ์ •๋ณด ์ˆ˜์ • ์„ฑ๊ณต

 

 

์ˆ˜์ • ๊ฒฐ๊ณผ

 

 

 

 

 

 

 

๋Œ“๊ธ€