โโ์ด๋ฆ ๋๋ ๋ถ์๋ฒํธ ํ๋๋ก ๊ฒ์ํ๋ ๊ธฐ๋ฅ ๋ง๋ค๊ธฐ(๋ค์ด๋๋ฏน sql ) โโ
MybatisEmpController.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
|
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.MultiValueMap;
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.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ezen.spring.web.mapper.CRUDMapper;
import com.ezen.spring.web.mapper.EmpMapper;
import com.ezen.spring.web.vo.Emp;
import lombok.extern.slf4j.Slf4j;
@Controller
@RequestMapping("/mybatis/emp")
@Slf4j
public class MybatisEmpController {
@Autowired
private EmpMapper dao;
@Autowired
private CRUDMapper dao2;
@GetMapping("/list")
public String list() {
return dao.getlist().toString();
}
@GetMapping("/listByDeptno/{deptno}")
public String listByDeptno(@PathVariable(name="deptno") int deptno) {
return dao.listByDeptno(deptno).toString();
}
@GetMapping("/deleteByEmpno/{empno}")
public Boolean deleteByEmpno(@PathVariable(name="empno") int empno) {
Emp emp = new Emp();
emp.setEmpno(empno);
return dao.deleteByEmpno(emp)>0?true:false;
}
/*
* @GetMapping("/deleteByEmpno") ์ฃผ์์ฐฝ์ /mybatis/emp/deleteByEmpno?empno=7369
public Boolean deleteByEmpno(Emp emp) {
return dao.deleteByEmpno(emp)>0?true:false;
}
*/
@GetMapping("/updateByDeptno/{deptno}/{sal}")
public Boolean updateByDeptno(@PathVariable Map<String,Integer> map) {
return dao.updateByDeptno(map)>0?true:false;
}
@GetMapping("/getListWithDeptno")
public String getListWithDeptno() {
return dao.getListWithDeptno().toString();
}
@GetMapping("/searchform")
public String search() {
return "/emp/SearchEMP";
}
@PostMapping("/search")//๋ฆฌํ์คํธ๋ฐ๋๋ฅผ ํผ๋ฐ์ดํฐ ์์ฒด๋ก ๋ฐ๊ฒ ๋ค. ๋ฉ๋งต์ ์๋ฐ ํ์ค์ด ์๋๊ณ ์คํ๋ง์๋ง ์๋ค.
@ResponseBody
public String search(@RequestBody MultiValueMap<String,Object> mulmap) {
//ํ๋์ ์ด๋ฆ์ผ๋ก ๋ค์ค ๊ฐ์ ๊ฐ์ง ํ๋ผ๋ฏธํฐ๋ ์ฒ๋ฆฌํ๊ธฐ ์ํจ
Map<String,Object> map = mulmap.toSingleValueMap();
log.info(map.toString());
return dao2.search(map).toString();
}
}
|
cs |
CRUDMapper.java
1
2
3
4
5
6
7
8
9
10
11
|
import java.util.List;
import java.util.Map;
import org.apache.ibatis.annotations.Mapper;
import com.ezen.spring.web.vo.Emp;
@Mapper
public interface CRUDMapper {
List<Emp> search(Map<String,Object> map);
void addBoardAndAttach(Map<String,Object> map);
}
|
cs |
CRUDMapper.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.ezen.spring.web.mapper.CRUDMapper">
<select id="search"
resultType="com.ezen.spring.web.vo.Emp"
parameterType="map">
SELECT * FROM emp2
<!--WHERE deptno =#{deptno} AND ename=#{ename}-->
<where>
<if test="deptno!=''">AND deptno =#{deptno}</if>
<if test="ename!=''">AND ename =#{ename}</if>
</where>
</select>
</mapper>
|
cs |
SearchEMP.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
|
<%@ page contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>์ฌ์ฉ์ ๊ฒ์</title>
<style type="text/css">
main {width:fit-content; margin:1em auto; }
main h3{ text-align: center;}
form {border:1px solid black; padding:1em;}
label {display:inline-block; width:3em; text-align: right; padding-right:1em; }
div.btn { width:fit-content; margin: 0.5em auto; }
</style>
<script src="https://code.jquery.com/jquery-3.6.1.min.js"
integrity="sha256-o88AwQnZB+VDvE9tvIXrMQaPlFFSUTR+nldQm1LuPXQ=" crossorigin="anonymous">
</script>
</head>
<body>
<main>
<h3>์ฌ์ฉ์ ๊ฒ์</h3>
<form id="loginForm" method="post" action="/mybatis/emp/search">
<div><label for="ename">์ด๋ฆ์ผ๋ก ๊ฒ์</label>
<input type="text" id="ename" name="ename" value="">
</div>
<div><label for="deptno">๋ถ์๋ฒํธ๋ก ๊ฒ์</label>
<input type="text" id="deptno" name="deptno" value="">
</div>
<div class="btn">
<button type="reset">์ทจ์</button>
<button type="submit">๊ฒ์</button>
</div>
</form>
</main>
</body>
</html>
|
cs |
์คํ๊ฒฐ๊ณผ :
โโ์ฒจ๋ถํ์ผ ๋ค์ด๋ก๋/์ ๋ก๋ ๊ธฐ๋ฅ ๋ง๋ค๊ธฐโโ
UploadController.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.io.File;
import java.io.IOException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import jakarta.servlet.ServletContext;
import jakarta.servlet.http.HttpServletRequest;
@Controller
@RequestMapping("/files")
public class UploadController
{
@Autowired
ResourceLoader resourceLoader;
@GetMapping("/upload")
public String getForm()
{
return "files/upload_form";
}
@PostMapping("/upload")
@ResponseBody
public String upload(@RequestParam("files")MultipartFile[] mfiles,
HttpServletRequest request,
@RequestParam("author") String author)
{
ServletContext context = request.getServletContext();
String savePath = context.getRealPath("/WEB-INF/files");
/* static/upload ๋๋ ํ ๋ฆฌ์ ์
๋ก๋ํ๋ ค๋ฉด, ์๋์ฒ๋ผ ์ ๋๊ฒฝ๋ก๋ฅผ ๊ตฌํ์ฌ ์ฌ์ฉํ๋ฉด ๋๋ค
* Resource resource = resourceLoader.getResource("classpath:/static");
* String absolutePath = resource.getFile().getAbsolutePath();
*/
try {
for(int i=0;i<mfiles.length;i++) {
mfiles[i].transferTo(
new File(savePath+"/"+mfiles[i].getOriginalFilename()));
/* MultipartFile ์ฃผ์ ๋ฉ์๋
String cType = mfiles[i].getContentType();
String pName = mfiles[i].getName();
Resource res = mfiles[i].getResource();
long fSize = mfiles[i].getSize();
boolean empty = mfiles[i].isEmpty();
*/
}
String msg = String.format("ํ์ผ(%d)๊ฐ ์ ์ฅ์ฑ๊ณต(์์ฑ์:%s)", mfiles.length,author);
return msg;
} catch (Exception e) {
e.printStackTrace();
return "ํ์ผ ์ ์ฅ ์คํจ:";
}
}
@GetMapping("/download/{filename}")
public ResponseEntity<Resource> download(
HttpServletRequest request,
@PathVariable String filename)
{
Resource resource = resourceLoader.getResource("WEB-INF/files/"+filename);
System.out.println("ํ์ผ๋ช
:"+resource.getFilename());
String contentType = null;
try {
contentType = request.getServletContext().getMimeType(resource.getFile().getAbsolutePath());
} catch (IOException e) {
e.printStackTrace();
}
if(contentType == null) {
contentType = "application/octet-stream"; //http ํ๋กํ ์ฝ ์ฝ์
}
return ResponseEntity.ok()
.contentType(MediaType.parseMediaType(contentType))
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + resource.getFilename() + "\"")
.body(resource);
}
}
|
cs |
upload_form.jsp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>ํ์ผ ์
๋ก๋ ํ
์คํธ</title>
</head>
<body>
<h3>Spring boot ํ์ผ ์
๋ก๋ ํ
์คํธ</h3>
<form action="/files/upload" method="post" enctype="multipart/form-data">
์์ฑ์ <input type="text" name="author" value="smith"><br>
File <input type="file" name="files" multiple="multiple"><br>
<button type="submit">์
๋ก๋</button>
</form>
</body>
</html>
|
cs |
application.properties๋ฅผ ์ค์ ํด์ค์ผํ๋ค.
์ปจํธ๋กค์์ ์ง์ ํด๋ ํ์ผ ๊ฒฝ๋ก์ ๋ง๊ฒ ํด๋๋ฅผ ์ค์น ํด์ค์ผํ๋ค.
์คํ ๊ฒฐ๊ณผ :
๋ค์ด๋ก๋
DB์ ์ ์ฅํด์ผ ํ ๋ฐ์ดํฐ
์์ฑ์, ์์ฑ์ผ, ์ ๋ชฉ, ๋ด์ฉ, ํ์ผ๋ช , ํ์ผ์ฌ์ด์ฆ
๊ธ ์์ฑ์ ์ฒจ๋ถํ์ผ์ด ์๋ค๋ฉด? -> ์ปฌ๋ผ๊ฐ์ด ๋น์ด์๊ฒ ๋๋ค.
๊ธ์ ํ๋์ธ๋ฐ ์ฒจ๋ถํ์ผ์ด 2๊ฐ๋ผ๋ฉด -> ํ๊ธ์ ์ฒจ๋ถํ์ผ ํ์ด ๋๊ฐ๊ฐ ํ์ํด์ง๋ค.
์์์ฑ : ํ๊ฐ์ ์ปฌ๋ผ์๋ ํ๊ฐ์ ๊ฐ๋ง ์ ์ฅํด์ผ ํ๋ค.
board (์์ฑ์, ์์ฑ์ผ, ์ ๋ชฉ, ๋ด์ฉ)
attach(ํ์ผ๋ช , ํ์ผ์ฌ์ด์ฆ)
๊ธ ํ๊ฐ๋ฅผ ์ ์ฅํ ๋ ์ฒจ๋ถํ์ผ์ด 2๊ฐ ํฌํจ๋ ๊ฒฝ์ฐ
-board ํ ์ด๋ธ์ ํ ํ ์ถ๊ฐ(๊ธ๋ฒํธ 1)
-attach ํ ์ด๋ธ์ 2๊ฐ ํ ์ถ๊ฐ(๊ธ๋ฒํธ 1 ํ์ผ๋ช 1 ํ์ผ์ฌ์ด์ฆ1 / ๊ธ๋ฒํธ1 ํ์ผ๋ช 2 ํ์ผ์ฌ์ด์ฆ2) JOIN๋ฌธ ์ด์ฉ
DB Normalization(์ ๊ทํ)
-๋ฌด๊ฒฐ์ฑ ํ๋ณด
์ 1 ์ ๊ทํ : ์์์ฑ์ ๊ฐ๊ฒํ๋ค.
์ 2 ์ ๊ทํ : pk๊ฐ ๋ค์๊ฐ์ ์ปฌ๋ผ์ผ๋ก ๊ตฌ์ฑ๋ ๊ฒฝ์ฐ์ ์ ์ฉ
-pk์ ๋ชจ๋ ๋ค๋ฅธ ์์ฑ์ด ์ข ์๋์ด์ผ ํ๋ค.
-pk์ ์ผ๋ถ์ ์ข ์๋๋ ๊ฒฝ์ฐ(๋ถ๋ถ ํจ์ ์ข ์์ฑ)๊ฐ ์์ด์ผ ํ๋ค.
์ 3 ์ ๊ทํ : pk ์ด์ธ์ ์์ฑ์ ๋ค๋ฅธ ์์ฑ์ด ์ข ์๋๋ ๊ฒฝ์ฐ(์ดํํจ์ ์ข ์์ฑ)๊ฐ ์์ด์ผ ํ๋ค.
์ข ์์ฑ : ์ด๋ค ์์ฑ 'a' ๋ฅผ ์๋ฉด ๋ค๋ฅธ ์์ฑ 'b'์ ๊ฐ์ด ์ ์ผํ๊ฒ ์ ํด์ง๋ ๊ด๊ณ
์์) ์ฃผ๋ฏผ๋ฑ๋ก๋ฒํธ๋ฅผ ์๋ฉด ๊ทธ์ฌ๋์ ์ด๋ฆ์ ์ ์ผํ๊ฒ ์ ํ ์ ์๋ค.
SQL ํ์ค ๊ฒ์
SELECT * FROM emp2 WHERE ename LIKE '%SM%' // ๋ถ๋ถ ์ผ์น ๊ฒ์(ํจํด ๊ฒ์)
SELECT * FROM emp2 WHERE ename='SM' //์์ ์ผ์น ๊ฒ์
์ค๋ผํด, MyBatis ์์ ๊ฒ์์ ํ์ค๊ณผ ์กฐ๊ธ ๋ค๋ฅด๋ค
ename LIKE '%'||#{ ์ฐพ์ ๋ฌธ์์ด (ex : SM)}||'%'
๋๊ธ