โโSpring ํ๋ ์์ํฌ < - > ์ค๋ผํด ์ฐ๊ฒฐ ๋ฐฉ๋ฒโโ
1๋ฒ ๋ฐฉ๋ฒ
jdbc ์ ์์ค ์ฝ๋๋ฅผ ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ(ํ๋ก์ ํธ์ jdbc ๋๋ผ์ด๋ฒ๋ง ๋ฑ๋กํด์ฃผ๊ณ ์ฝ๋ฉ๋ง ํด์ฃผ๋ฉด ๋๋ค.)
2๋ฒ ๋ฐฉ๋ฒ
์คํ๋ง ํ๋ ์์ํฌ์์ ์ ๊ณตํด์ฃผ๋ jdbc ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ฌ์ฉ
3๋ฒ ๋ฐฉ๋ฒ
2๋ฒ ๋ฐฉ๋ฒ + ORM ํ๋ ์์ํฌ(MyBatis) ์ฌ์ฉํ๊ธฐ
4๋ฒ ๋ฐฉ๋ฒ
JPA(Java Persistence Api) ์ฌ์ฉํ๊ธฐ
โโ1๋ฒ ๋ฐฉ๋ฒ: ํ๋ก์ ํธ์ jdbc ๋๋ผ์ด๋ฒ ์ค์น ํ ์ ์์ค ์ฝ๋๋ฅผ ์ฌ์ฉํ๋ ๋ฐฉ๋ฒโโ
ํ๋ก์ ํธ์ jdbc ๋๋ผ์ด๋ฒ๋ง ๋ฑ๋กํด์ฃผ๊ณ ์ฝ๋ฉ๋ง ํด์ฃผ๋ฉด ๋๋ค.
๋จผ์ jdbc ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ค์นํ๊ณ ์ถ์ ํ๋ก์ ํธ์ ์ฐํด๋ฆญํ์ฌ [Build Path] - [Configure Build Path]์ ๋ค์ด๊ฐ๋ค.
๊ทธ๋ค์ Classpath๋ฅผ ์ ํํ๊ณ [Add External JARs] ๋๋ฌ ojdbc ํ์ผ์ด ๋ค์ด ์๋ ๊ฒฝ๋ก๋ฅผ ์ฐพ์์ ์ถ๊ฐํ ๋ค [Apply] [Apply and Close] ํด์ฃผ๋ฉด ๋๋ค.
์์ฑ๋ ๊ฒฐ๊ณผ :
โโOracle SCOTT๊ณ์ ์ EMP table๊ณผ Spring ์ฐ๋ํด๋ณด๊ธฐโโ
TestController.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
|
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.EmpDAOService;
import com.ezen.spring.web.vo.Emp;
import lombok.extern.slf4j.Slf4j;
//@RestController ๋ชจ๋ ๋ฉ์๋๊ฐ Responsebody๋ฅผ ๊ฐ๊ฒ๋๋ค.
@Controller
@RequestMapping("/test")
@Slf4j
public class TestController
{
@Autowired
private EmpDAOService svc;
@GetMapping("/add")
public String showAddForm()
{
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);
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 |
EmpDAOService.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
|
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ezen.spring.web.dao.EmpDAO;
import com.ezen.spring.web.vo.Emp;
@Service
public class EmpDAOService
{
@Autowired
private EmpDAO dao;
public boolean add(Emp emp) {
return dao.addEmp(emp);
}
public List<Emp> getList() {
return dao.getList();
}
public Emp detailEmp(int empno)
{
return dao.getEmpByEmpno(empno);
}
public boolean update(Emp emp)
{
return dao.updateEmp(emp);
}
public boolean delete(int empno)
{
return dao.deleteEmp(empno);
}
}
|
cs |
EmpDAO.java
EMP ํ ์ด๋ธ์ ๋ณต์ฌํ์ฌ EMP2 ํ ์ด๋ธ์ ๋ง๋ค์ด ํ์ฉํ์๋ค.
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
|
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Component;
import com.ezen.spring.web.vo.Emp;
@Component
public class EmpDAO
{
private Connection conn;
private PreparedStatement pstmt;
private ResultSet rs;
private Connection getConn()
{
try {
Class.forName("oracle.jdbc.OracleDriver");
conn = DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:xe", "SCOTT", "TIGER");
this.conn = conn;
return conn;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public List<Emp> getList()
{
getConn();
try {
String sql="SELECT * FROM emp2";
this.pstmt=conn.prepareStatement(sql);
this.rs=pstmt.executeQuery();
List<Emp> list = new ArrayList<>();
while(rs.next())
{
int empno = rs.getInt("EMPNO");
String ename = rs.getString("ENAME");
int deptno = rs.getInt("DEPTNO");
java.sql.Date hiredate = rs.getDate("HIREDATE");
float salary = rs.getFloat("SAL");
Emp emp = new Emp();
emp.setEmpno(empno);
emp.setEname(ename);
emp.setDeptno(deptno);
emp.setHiredate(hiredate);
emp.setSal(salary);
list.add(emp);
}
return list;
}catch(Exception ex) {
ex.printStackTrace();
}finally {
closeAll();
}
return null;
}
public boolean addEmp(Emp emp)
{
getConn();
try {
String sql = "INSERT INTO emp2 "
+ "(empno, ename, deptno, sal, hiredate) VALUES(?,?,?,?,?)";
this.pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, emp.getEmpno());
pstmt.setString(2, emp.getEname());
pstmt.setInt(3, emp.getDeptno());
pstmt.setFloat(4, emp.getSal());
pstmt.setDate(5, emp.getHiredate());
int rows = pstmt.executeUpdate();
return rows>0 ? true : false;
}catch(Exception ex) {
ex.printStackTrace();
}finally {
closeAll();
}
return false;
}
public Emp getEmpByEmpno(int empno)
{
getConn();
try {
String sql = "SELECT * FROM emp2 WHERE empno="+empno;
this.pstmt = conn.prepareStatement(sql);
this.rs = pstmt.executeQuery(sql);
if(rs.next())
{
int eno = rs.getInt("EMPNO");
String ename = rs.getString("ENAME");
int deptno = rs.getInt("DEPTNO");
java.sql.Date hiredate = rs.getDate("HIREDATE");
float salary = rs.getFloat("SAL");
Emp emp = new Emp();
emp.setEmpno(eno);
emp.setEname(ename);
emp.setDeptno(deptno);
emp.setHiredate(hiredate);
emp.setSal(salary);
return emp;
}
}catch(Exception ex) {
ex.printStackTrace();
}finally {
closeAll();
}
return null;
}
public boolean updateEmp(Emp emp)
{
getConn();
try {
String sql = "UPDATE emp2 SET sal=?, deptno=? WHERE empno=?";
this.pstmt = conn.prepareStatement(sql);
pstmt.setFloat(1, emp.getSal());
pstmt.setInt(2, emp.getDeptno());
pstmt.setInt(3, emp.getEmpno());
int rows = pstmt.executeUpdate();
return rows>0 ? true : false;
}catch(Exception ex) {
ex.printStackTrace();
}finally {
closeAll();
}
return false;
}
public boolean deleteEmp(int empno)
{
getConn();
try {
String sql = "DELETE FROM emp2 WHERE empno=?";
this.pstmt = conn.prepareStatement(sql);
pstmt.setInt(1,empno);
int rows = pstmt.executeUpdate();
return rows>0 ? true : false;
}catch(Exception ex) {
ex.printStackTrace();
}finally {
closeAll();
}
return false;
}
private void closeAll()
{
try {
if(rs!=null) rs.close();
if(pstmt!=null) pstmt.close();
if(conn!=null) conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
|
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 : '/test/add',
method: 'post',
data: obj,
cache : false,
dataType:'json',
success:function(res){
alert(res.added ? '์ถ๊ฐ ์ฑ๊ณต' : '์ถ๊ฐ ์คํจ');
location.href = '/test/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="/test/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:'/test/delete',
method:'post',
data: {'empno':${emp.empno}},
cache : false,
dataType: 'json',
success: function(res){
if(res.deleted){
alert(res.deleted ? '์ญ์ ์ฑ๊ณต':'์๋ฌ');
location.href ='/test/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="/test/list"> ๋ฑ๋ก๋ ์ด์ฉ์ ์ ๋ณด ๋ณด๊ธฐ </a>]
[<a href="/test/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 : '/test/updateResult',
method: 'post',
data: obj,
cache : false,
dataType:'json',
success:function(res){
alert(res.updated ? '์์ ์ฑ๊ณต' : '์์ ์คํจ');
location.href = '/test/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 |
์คํ ๊ฒฐ๊ณผ :
์์ ๊ฒฐ๊ณผ
๋๊ธ