๊ด€๋ฆฌ ๋ฉ”๋‰ด

JiYoung Dev ๐Ÿ–ฅ

[Spring Boot] Spring Boot ์‚ฌ์šฉํ•˜๊ธฐ (2023.06.07) ๋ณธ๋ฌธ

full stack/Back-End

[Spring Boot] Spring Boot ์‚ฌ์šฉํ•˜๊ธฐ (2023.06.07)

Shinjio 2023. 6. 7. 18:20

 

Spring Boot ๊ฐœ๋ฐœ ํ™˜๊ฒฝ ์ค€๋น„

 

๐ŸŽˆ ํ”„๋กœ๊ทธ๋žจ ์„ค์น˜ (Spring & MySQL)

 

spring tool suite 4

 

 

Spring | Tools

 

spring.io

 

 

 

C๋“œ๋ผ์ด๋ธŒ ๋ฐ”๋กœ ์•„๋ž˜์— ์„ค์น˜ (ํ•„์ˆ˜๋Š” ์•„๋‹˜!)

 

 

jarํŒŒ์ผ๋„ ์••์ถ•ํŒŒ์ผ์ด๋ผ ์••์ถ•ํ’€๊ธฐํ•˜๋ฉด ๋จ

๊ทธ๋Ÿฐ๋ฐ ์•ˆ๋˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ์–ด์„œ cmdํ™œ์šฉ

 

cmd ์ฐฝ ์—ฐ ํ›„       cd /             >> c๋“œ๋ผ์ด๋ธŒ๋กœ ์ด๋™

java -jar spring-tool-suite-4-4.18.1.RELEASE-e4.27.0-win32.win32.x86_64.self-extracting.jar ์ž…๋ ฅ

 

sts-4.16.1.RELEASE ๋œจ๋ฉด ์ •์ƒ ์••์ถ• ํ’€๊ธฐ ์™„๋ฃŒ

 

์›Œํฌ์ŠคํŽ˜์ด์Šค ๋งŒ๋“ค๊ธฐ 

 

 

 

 

๊ฒฝ๋กœ์— ํ•œ๊ธ€์ด ์žˆ์œผ๋ฉด ์‹คํ–‰์‹œ ๋ฌธ์ œ ๋ฐœ์ƒ๋˜๋ฏ€๋กœ ๊ผญ ํ™•์ธ!

workspace ์„ ํƒ ํ›„ Launch

 

 

My SQL 

 

 

MySQL :: Begin Your Download

The world's most popular open source database Contact MySQL  |  Login  |  Register

dev.mysql.com

 

 

 

Developer Default > Execute > Visual C++ ๋™์˜ ํ›„ ์„ค์น˜ > execute > 

ํŒจ์Šค์›Œ๋“œ, ๊ณ„์ • ์ƒ์„ฑ ํ›„ next,  excute ๋ฐ˜๋ณต

 

 

 

 

๐ŸŽˆ ์Šคํ”„๋ง ๋ถ€ํŠธ ํ”„๋กœ์ ํŠธ ์ƒ์„ฑ

 

1. ์ธ์ฝ”๋”ฉ

 

 

 

 

java class file, text ์ธ์ฝ”๋”ฉ ์ง„ํ–‰

 

 

2. ํ”„๋กœ์ ํŠธ ์ƒ์„ฑ

 

 

 

spring ๊ฒ€์ƒ‰ํ›„ spring starter project ์„ ํƒ

 

 

 

 

name : ํ”„๋กœ์ ํŠธ ์ด๋ฆ„

type : ๋นŒ๋“œ๋ฐฉ์‹

packaging : ํŒจํ‚ค์ง• ๋ฐฉ์‹

Group : ๊ธฐ๋ณธ ํŒจํ‚ค์ง€๋ช…

package : ๊ต‰์žฅํžˆ ์ค‘์š”! ํŒจํ‚ค์ง€๊ฐ€ ๊ธฐ๋ณธ์ ์œผ๋กœ ๋งŒ๋“ค์–ด์ง€๊ณ  ๊ทธ ์•ˆ์— ํŠน์ˆ˜ํ•œ ๋ฌด์–ธ๊ฐ€๊ฐ€ ์ƒ๊น€

 

 

 


๋””ํŽœ๋˜์‹œ ์„ ํƒ

๋ถ€ํŠธ์—์„œ๋Š” pom์— ๋”ฐ๋กœ ์ž…๋ ฅํ•˜์ง€ ์•Š์•„๋„ ํ”„๋กœ์ ํŠธ ์ƒ์„ฑ์‹œ ์„ ํƒ ๊ฐ€๋Šฅ

 

 

Spring Boot version :3๋ฒ„์ ผ์ด๋ฉด ๋†’์€๊ฑฐ >>>> ์ž๋ฐ” ๋ฒ„์ ผ๊ณผ ๋งž์ถฐ์•ผ ํ•จ. ์šฐ๋ฆฌ๋Š” 2.7.12 ์„ ํƒ!

 

 

 

 

 

 

 

 

๊ธฐ๋ณธ์ ์œผ๋กœ ์‚ฌ์šฉํ•  ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋Š” ๊ฒ€์ƒ‰ํ•˜์—ฌ ์ถ”๊ฐ€ ๊ฐ€๋Šฅ

- Spring Web Services

- Spring Web

- MySQL Driver

- MyBatis Framework

- Lombok

 

 

๐ŸŽˆ ์Šคํ”„๋ง ๋ถ€ํŠธ ํ”„๋กœ์ ํŠธ ๊ธฐ๋ณธ ๊ตฌ์กฐ ์‚ดํŽด๋ณด๊ธฐ

 

 

๐Ÿ“– BootBasicApplication.java 

 

 

 

//@SpringBootApplication : ์–ด๋…ธํ…Œ์ด์…˜์ด ๋ถ™์€ ํด๋ž˜์Šค์˜ ์œ„์น˜๊ฐ€ ๊ต‰์žฅํžˆ ์ค‘์š”!

//ํด๋ž˜์Šค์˜ ์œ„์น˜๋ฅผ ๊ธฐ์ค€์œผ๋กœ ํ•˜์œ„์— ์žˆ๋Š” ์„ค์ • ๋‚ด์šฉ๋“ค์„ ์ฝ์–ด์ฃผ๊ธฐ ๋•Œ๋ฌธ

//ํด๋ž˜์Šค์˜ ์œ„์น˜๊ฐ€ ํ”„๋กœ์ ํŠธ์˜ ์ตœ์ƒ๋‹จ์— ์œ„์น˜ํ•ด์•ผ ํ•จ!

//์Šคํ”„๋ง ๋ถ€ํŠธ์˜ ์„ค์ • ์ž๋™ํ™”, bean(๊ฐ์ฒด) ์ฝ์–ด์˜ค๊ฑฐ๋‚˜ ์ƒ์„ฑํ•˜๋Š” ์ž‘์—…์„ ์ž๋™์œผ๋กœ ์„ค์ •ํ•ด์คŒ

 

์ปจํŠธ๋กค๋Ÿฌ, mapper ๋“ฑ ๋ชจ๋‘ @application ํด๋ž˜์Šค ์•„๋ž˜์— ์œ„์น˜ํ•ด์•ผ ํ•จ!

application์žˆ๋Š” ์œ„์น˜์— ํด๋” ์ƒ์„ฑ(mapper ํด๋”, controllerํด๋” ๋“ฑ) ํ›„ ๊ทธ ์•„๋ž˜์— controller ๋“ฑ ์ƒ์„ฑ 

 

 

ํ•˜์œ„ ๊ฒฝ๋กœ์— ๋งŒ๋“ค์–ด์ค˜์•ผ์ง€๋งŒ ๊ฐ์ฒดํ™” ์‹œ์ผœ์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค! ๊ต‰์žฅํžˆ ์ค‘์š”ํ•จ!!!!!

 

 

๐Ÿ“– src/main/resources

 

 

static

์ด๋ฏธ์ง€, css, js ๋“ฑ ์ •์  ๋ฆฌ์†Œ์Šค ํŒŒ์ผ >>> static ํด๋” ์•„๋ž˜์— ๋„ฃ๊ธฐ!

static ํด๋” ๊ธฐ์ค€์œผ๋กœ ๊ฒฝ๋กœ๋งŒ ์„ค์ •ํ•ด์ฃผ๋ฉด ํ•ด๋‹น ์ž์› ์‚ฌ์šฉ ๊ฐ€๋Šฅ

 

templete โ˜…โ˜…โ˜…

teplete ๊ต‰์žฅํžˆ ์ค‘์š”!

๊ธฐ์กด์—๋Š” view ์ž‘์„ฑ์‹œ jsp๋ฅผ ๋งŽ์ด ์‚ฌ์šฉ 

๊ทธ๋Ÿฌ๋‚˜ jsp๋ง๊ณ  spring boot๋‚˜ ์Šคํ”„๋ง ํ”„๋ ˆ์ž„์›Œํฌ์—์„œ ์ž์ฃผ ์‚ฌ์šฉํ•˜๋Š” ํ˜•์‹์€ thym leaf (์Šคํ”„๋ง์—์„œ ์‚ฌ์šฉํ•˜๋Š” ํ…œํ”Œ๋ฆฟ)

ํƒ€์ž„๋ฆฌํ”„๋Š” ํ˜•์‹์ด ๋”ฐ๋กœ์žˆ๋Š”๊ฑด ์•„๋‹ˆ๊ณ  html์— script๋ฅผ ๋„ฃ์„ ์ˆ˜ ์žˆ๋Š” ํ˜•์‹

 

ํƒ€์ž„๋ฆฌํ”„๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์ด์œ 

JSP๋Š” HTML๋กœ ๋ณ€ํ™˜๋˜๋Š” ๊ณผ์ • + ํŒŒ์ผ ์ž์ฒด๋„ HTML๋ณด๋‹ค ๋ฌด๊ฑฐ์›€(JAVA์ฝ”๋“œ ๋“ค์–ด๊ฐ€์„œ)

ํƒ€์ž„๋ฆฌํ”„๋Š” HTML ํ˜•์‹์„ ๊ทธ๋Œ€๋กœ ์“ฐ๋˜ ๊ฐ€๊ณตํ•  ์ˆ˜ ์žˆ๋Š” ํ˜•์‹ 

๋”ฐ๋ผ์„œ ์Šคํ”„๋ง์—์„œ๋Š” ํƒ€์ž„๋ฆฌํ”„๋ฅผ ๋งŽ์ด ์‚ฌ์šฉํ•จ!

 

์–˜(ํƒ€์ž„๋ฆฌํ”„)์˜ ์œ„์น˜๊ฐ€ TEMPLETES ํ•˜์œ„์— ์ƒ์„ฑ

JSP๋Š” ๋ฌด์กฐ๊ฑด WEB-APP > WEB-INF ์•„๋ž˜์— ๋“ค์–ด๊ฐ

 

application.properties โ˜…โ˜…โ˜…

์„ค์ • ํŒŒ์ผ์ด๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ์Œ

์Šคํ”„๋ง๋ถ€ํŠธ๊ฐ€ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ๊ตฌ๋™ํ•  ๋•Œ ์ž๋™์œผ๋กœ ๋กœ๋”ฉ๋จ!

DB์—ฐ๊ฒฐ์ •๋ณด, context-root, view ๋ฆฌ์กธ๋ฒ„ ๋“ฑ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Œ!

 

 

 

์„ค์ •ํŒŒ์ผ ๊ธฐ๋ณธ ์ฝ”๋“œ

 

# ์„ค์ •ํŒŒ์ผ์ด๋ผ๊ณ  ๋ณผ ์ˆ˜ ์žˆ์Œ
# ์Šคํ”„๋ง ๋ถ€ํŠธ๊ฐ€ ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ๊ตฌ๋™ํ•  ๋•Œ ์ž๋™์œผ๋กœ ๋กœ๋”ฉ๋˜๋Š” ํŒŒ์ผ 
# ์—ฌ๊ธฐ ์•„๋ž˜์— KEY-VALUE ์Œ ํ˜•์‹์œผ๋กœ ๊ฐ’์„ ์ž…๋ ฅํ•˜๋ฉด
# ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์—์„œ ์ฐธ์กฐํ•˜์—ฌ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Œ 

#context-root : ์„œ๋ฒ„์ƒ์—์„œ ๋™์ž‘ํ•˜๋Š” ํ”„๋กœ์ ํŠธ๋ฅผ ๊ตฌ๋ณ„ํ•˜๊ธฐ ์œ„ํ•œ ๊ฒฝ๋กœ
#๋ฌด์กฐ๊ฑด slash๋กœ ์‹œ์ž‘ 
server.servlet.context-path=/myapp
#server port number
server.port=8089

#view(jsp) : prefix, suffix
spring.mvc.view.prefix=/WEB-INF/Views/
spring.mvc.view.suffix=.jsp

# EX. DB์—ฐ๊ฒฐ์ •๋ณด  
spring.datasource.url=jdbc:mysql://localhost:3306/Boot?useSSL=false&characterEncoding=UTF-8&serverTimezone=UTC
spring.datasource.username=fullstack
spring.datasource.password=12345
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

 

 

Boot >>>> ์Šคํ‚ค๋งˆ ์ด๋ฆ„

 

๐Ÿ“– pom.xml

ํŠน์ด์  : ๋ฒ„์ ผ์ด ์—†์Œ >> ๋ถ€ํŠธ์—์„œ๋Š” ๋ฒ„์ ผ์„ ์•Œ์•„์„œ ๊ด€๋ฆฌํ•ด์คŒ 

ํ”„๋กœ์ ํŠธ ์ƒ์„ฑ์‹œ ์„ ํƒํ•œ ๋””ํŽœ๋˜์Šค๋“ค์— ๋Œ€ํ•˜์—ฌ ์ž๋™์œผ๋กœ ์ž…๋ ฅ

 

 

 

๐ŸŽˆ JSP ํŒŒ์ผ ์ƒ์„ฑ ํ›„ view ๋ฐ˜ํ™˜ >> ๋ฉ”์ธํŽ˜์ด์ง€ ๋ณด์—ฌ์ฃผ๊ธฐ

1. jsp ํŒŒ์ผ ์ƒ์„ฑ

 

 

์Šคํ”„๋ง ํ”„๋ ˆ์ž„์›Œํฌ์™€ ๋˜‘๊ฐ™์ด ํด๋” ์ƒ์„ฑ ํ›„ ์ง‘์–ด๋„ฃ๊ธฐ

 

 

 

ํด๋” ๋‚ด jsp ํŒŒ์ผ ์ƒ์„ฑํ•˜๋ ค ํ–ˆ๋”๋‹ˆ jsp ํŒŒ์ผ ํ˜•์‹์ด ์—†์Œ 

 

 

 

 

์ด์œ 

: ์Šคํ”„๋ง4์—์„œ๋Š” jsp๋ฅผ ๊ธฐ๋ณธ์ ์œผ๋กœ ์“ด๋‹ค๊ณ  ์ƒ๊ฐํ•˜์ง€ ์•Š์Œ > ๊ด€๋ จ ํ”Œ๋Ÿฌ๊ทธ์ธ ์„ค์น˜๊ฐ€ ๋˜์ง€ ์•Š์Œ

  >>>> ๊ด€๋ จ ํ”Œ๋Ÿฌ๊ทธ์ธ ์„ค์น˜

 

help > market place์— ๋“ค์–ด๊ฐ€์„œ ์„ค์น˜

 

 

๋‹ค๋ฅธ ๋ณ€๊ฒฝ์‚ฌํ•ญ ์—†์ด ์„ค์น˜ํ•˜๋ฉด ๋จ!

 

 

์œ„์˜ ์ฐฝ ๋œจ๋ฉด select all >> trust selected ์„ ํƒ ํ›„ ์ง„ํ–‰ >> ์„ค์น˜ ํ›„ restart

 

์„ค์น˜ ์ •์ƒ์ ์œผ๋กœ ๋˜๋ฉด jsp file ์„ ํƒ ๊ฐ€๋Šฅ

 

 

2. jsp ์ž‘์„ฑ

 

์ œ์ด์ฟผ๋ฆฌ ์‚ฌ์šฉ

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>

 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>
<body>
	<button onClick="loadList()">๋ฉค๋ฒ„ ๋ฆฌ์ŠคํŠธ ๊ฐ€์ ธ์˜ค๊ธฐ</button>
	<div id="list">
	</div>
	<script>
	
		function loadList(){
			$.ajax({
				url : "member", //localhost:8089/myapp/member
				type : "get", //์š”์ฒญ๋ฐฉ์‹
				dataType : "json", //์‘๋‹ต ๋ฐ›๋Š” ๋ฐ์ดํ„ฐ์˜ ํ˜•์‹ (view(html, jsp)๋ฅผ ๋ฐ›๋Š”๊ฒŒ ์•„๋‹ˆ๋ผ ๋ฐ์ดํ„ฐ(๋ชจ๋ธ)๋งŒ ๋ฐ˜ํ™˜)
				success : function(res){ //์š”์ฒญ-์‘๋‹ต ์„ฑ๊ณตํ–ˆ์„ ๋•Œ
					console.log(res)
					let t = "<table border=1>"
					for(let i=0; i<res.length ; i++){
						t += "<tr>"
						t += "<td>" + res[i].id + "</td>"
						t += "<td>" + res[i].pw + "</td>"
						t += "<td>" + res[i].nick + "</td>"
						t += "</tr>"
					}
					t += "</table>"
					$("#list").append(t)
				},
				error : function(){
					console.log("ํ†ต์‹ ์‹คํŒจ!")
				}
			})
		}
	
	</script>
</body>
</html>

 

3. view ๋ณด์—ฌ์ฃผ๊ธฐ ์œ„ํ•ด ์—ฐ๊ฒฐ

: application.properties

root-context ์ž‘์„ฑ

์Šคํ”„๋ง๋ถ€ํŠธ๋„ ํ†ฐ์บฃ์„ ์“ฐ์ง€๋งŒ boot๊ฐ€ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๋‚ด์žฅ ์„œ๋ฒ„๋ฅผ ์‚ฌ์šฉ

๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๋‚ด์žฅ์„œ๋ฒ„์—์„œ ํ”„๋กœ๊ทธ๋žจ์„ ๋™์ž‘์‹œํ‚ด 

 

#context-root : ์„œ๋ฒ„์ƒ์—์„œ ๋™์ž‘ํ•˜๋Š” ํ”„๋กœ์ ํŠธ๋ฅผ ๊ตฌ๋ณ„ํ•˜๊ธฐ ์œ„ํ•œ ๊ฒฝ๋กœ
#๋ฌด์กฐ๊ฑด slash๋กœ ์‹œ์ž‘
server.servlet.context-path=/myapp
#server port number
server.port=8089

 

 

view ๋ฆฌ์กธ๋ฒ„ -- ์Šคํ”„๋งํ”„๋ ˆ์ž„์›Œํฌ์—์„œ ์ž‘์„ฑํ–ˆ๋˜ ๊ฒƒ๊ณผ ๋˜‘๊ฐ™์ด ์„ค์ • ์ •๋ณด ์ž‘์„ฑํ•ด์•ผ ํ•จ

๋ถ€ํŠธ์—์„œ๋Š” properties์—์„œ ํ•จ

 

์Šคํ”„๋ง

 

 

4. ์ปจํŠธ๋กค๋Ÿฌ ๋งŒ๋“ค๊ธฐ

4-1. ํŒจํ‚ค์ง€ ์ƒ์„ฑ

 

 

์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜ ํด๋ž˜์Šค ์•„๋ž˜์— ํŒŒ์ผ ์œ„์น˜

 

 

4-2. ์ž๋ฐ” ํŒŒ์ผ ์ƒ์„ฑ

 

package com.smhrd.myapp.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

//View ๋ฐ˜ํ™˜ ์ปจํŠธ๋กค๋Ÿฌ ์ง€์ •
@Controller
public class HomeController {

	//์Šคํ”„๋ง 3๋ฒ„์ „์—์„œ ์‚ฌ์šฉํ–ˆ๋˜ ๋ฐฉ์‹
//	@RequestMapping(value="/", method=RequestMethod.GET)
	@GetMapping(value="/")
	//Get๋ฐฉ์‹ ๋งคํ•‘, ํฌ์ŠคํŠธ๋Š” PostMapping
	public String main() {
		return "main";
	}
	
}

 

์ค‘๊ฐ„์ ๊ฒ€. ๋ถ€ํŠธ ์‹คํ–‰ํ•˜์—ฌ main์ฐฝ ๋œจ๋Š”์ง€ ํ™•์ธ!

 

 

์˜ค๋ฅ˜  ๋ฐœ์ƒ 1. database ์—ฐ๋™ X

 

์„œ๋ฒ„ ์‹คํ–‰ ์‹คํŒจ 

my SQL ์—ฐ๊ฒฐ ์•ˆ๋๊ธฐ ๋•Œ๋ฌธ

์„œ๋ฒ„ ์—ฐ๊ฒฐํ•˜๊ฑฐ๋‚˜ pom.xml์—์„œ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๊ด€๋ จ ๋‚ด์šฉ ์‚ญ์ œํ›„ restart

 

์˜ค๋ฅ˜๋ฐœ์ƒ 2. port8080 ์‚ฌ์šฉ์ค‘

>> maven update ์ง„ํ–‰!

 

 

์„œ๋ฒ„ ์—ฐ๊ฒฐ ์„ฑ๊ณต

 

๐ŸŽˆ ๋ฉค๋ฒ„๋ฆฌ์ŠคํŠธ ๋ณด์—ฌ์ฃผ๊ธฐ ํ™”๋ฉด ๊ตฌํ˜„

1. ๋ฐ์ดํ„ฐ (json) ๋ฐ˜ํ™˜์„ ์œ„ํ•œ RestController ์ƒ์„ฑ

 

์Šคํ”„๋ง ํ”„๋ ˆ์ž„์›Œํฌ >>

 

controller ์ƒ์„ฑ - RestController ์‚ฌ์šฉ

๋ทฐ๊ฐ€ ์•„๋‹Œ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ˜ํ™˜ํ•  ๊ฒƒ์ด๋ฏ€๋กœ RestController ์‚ฌ์šฉ

 

package com.smhrd.myapp.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import com.smhrd.myapp.domain.Member;
import com.smhrd.myapp.service.MemberService;

//๋ฐ์ดํ„ฐ(๋ชจ๋ธ)์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ์ปจํŠธ๋กค๋Ÿฌ @RestController : @Controller + @ResponseBody ํ•ฉ์ณ์ง„ ํ˜•ํƒœ
//์ผ๋ฐ˜ ์ปจํŠธ๋กค๋Ÿฌ๋Š” view๋ฅผ ๋ฐ˜ํ™˜
@RestController
public class MemberController {
	
	@Autowired
	private MemberService service;
	//Spring ๊ธฐ๋ณธ ์ˆœ์„œ
	//(์š”์ฒญ ์ฒ˜๋ฆฌ) Controller > Service > mapper Interface > mapper XML
	//(๊ฒฐ๊ณผ ์ฒ˜๋ฆฌ) mapperXML > mapper Interface > Service > Controller 
	// Controller : ์š”์ฒญ ๋ฐ›๋Š”๊ฑฐ, ์‘๋‹ต ํ•˜๋Š”๊ฑฐ์— ์ง‘์ค‘
	// Service : ๋ฐ์ดํ„ฐ ๊ฐ€๊ณต, ์š”์ฒญ ๊ฐ€๊ณต์— ์ง‘์ค‘
	@GetMapping(value="/member")
	public List<Member> memberList() {
		//List<Member> ๋ฐ˜ํ™˜
		// >> ์ž๋ฐ”์—์„œ ์‚ฌ์šฉํ•˜๋Š” ํ˜•ํƒœ๋กœ ์ด ํ˜•ํƒœ ๊ทธ๋Œ€๋กœ๋Š” javaScript์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†์Œ. 
		// >> ๊ทธ๋Ÿฌ๋‚˜ ๋ฐ”๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์—ˆ์Œ ----> ์™œ๋ƒ? jackson library ๋•Œ๋ฌธ
		//Jackson library
		// : java object๋ฅผ json์œผ๋กœ ๋ณ€ํ™˜
		// : json์„ javaObject๋กœ ๋ณ€ํ™˜ํ•ด์ฃผ๋Š” ๊ธฐ๋Šฅ์„ ๊ฐ€์ง€๊ณ  ์žˆ์Œ
		// ์ผ๋ฐ˜ ์Šคํ”„๋ง ํ”„๋ ˆ์ž„์›Œํฌ์—์„œ ์‚ฌ์šฉํ•˜๋ ค๋ฉด ๋””ํŽœ๋˜์‹œ์— ์ถ”๊ฐ€ํ•ด์•ผ ํ•จ 
		// 
		return service.memberList();
	}

}

 

2. member VO ์ƒ์„ฑ

package ์ƒ์„ฑ

 

ํด๋ž˜์Šค ์ƒ์„ฑ

 

 

ํ•„๋“œ ์ž‘์„ฑ ๋ฐ ๋กฌ๋ณต ์‚ฌ์šฉํ•˜์—ฌ ์ƒ์„ฑ์ž ์ƒ์„ฑ

 

๋กฌ๋ณต ์‚ฌ์šฉ 

๋กฌ๋ณต ํŒŒ์ผ ์ด๋™์‹œํ‚ค๊ธฐ

4๋ฒ„์ ผ ์žˆ๋Š”์ง€ ํ™•์ธ ํ›„ intall

 

 

package com.smhrd.myapp.domain;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@AllArgsConstructor //์ „๋ถ€ ์ดˆ๊ธฐํ™” ์‹œ์ผœ์ฃผ๋Š” ์ƒ์„ฑ์ž
@NoArgsConstructor //๊ธฐ๋ณธ ์ƒ์„ฑ์ž
@Getter 
@Setter
public class Member {
	
	private String id;
	private String pw;
	private String nick;
	
	

}

 

3. service ๋งŒ๋“ค๊ธฐ

ํŒจํ‚ค์ง€ ์ƒ์„ฑ

 

 

MemberService ํด๋ž˜์Šค ์ƒ์„ฑ

 

package com.smhrd.myapp.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.smhrd.myapp.domain.Member;
import com.smhrd.myapp.mapper.MemberMapper;

@Service
public class MemberService {
	
	//์™ธ๋ถ€์—์„œ ์ƒ์„ฑ ํ›„ ์ฃผ์ž… > ์˜์กด์„ฑ ์ฃผ์ž…
	@Autowired
	private MemberMapper mapper;
	
	//service์˜ ๋ฉ”์„œ๋“œ๋ฅผ controller์—์„œ ํ˜ธ์ถœํ•˜์—ฌ ์‚ฌ์šฉ
	//mapper ๊ธฐ๋Šฅ ํ˜ธ์ถœ
	public List<Member> memberList(){
		return mapper.memberList();
	}
}

 

mapper ์ธํ„ฐํŽ˜์ด์Šค ๋ฐ XML ๋งŒ๋“ค๊ธฐ

 

 

๊ธฐ์กด ์Šคํ”„๋ง์—์„œ๋Š” mapper ํŒจํ‚ค์ง€์— ์ถ”๊ฐ€ํ–ˆ์—ˆ์Œ

๋ถ€ํŠธ์—์„œ๋Š” resource ์•„๋ž˜์— mapper.java ํŒจํ‚ค์ง€ ์œ„์น˜์™€ ๋งž์ถฐ์ค˜์•ผ ํ•จ !

 

 

 

 

 

 

Mapper ์ธํ„ฐํŽ˜์ด์Šค์™€ Mapper.XML ๋งž์ถ”๊ธฐ

 

 

 

 

package com.smhrd.myapp.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Mapper;

import com.smhrd.myapp.domain.Member;

@Mapper
public interface MemberMapper {
	
	//์‹ค์ œ SQL ์ถ”๊ฐ€ : @annotation ์ถ”๊ฐ€ ํ˜น์€ XML ์ถ”๊ฐ€
	public List<Member> memberList();
}

 

 

Member.XML 

 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
  PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  "https://mybatis.org/dtd/mybatis-3-mapper.dtd">
  
  <!-- mapper๋Š” ํŒŒ์ผ์ด ์—ฌ๋Ÿฌ๊ฐœ์ผ ์ˆ˜ ์žˆ์Œ ์ด๋ฅผ ๊ตฌ๋ถ„ํ•˜๊ธฐ ์œ„ํ•ด namespace ์ž‘์„ฑ
  namespace๋Š” mapper ์žˆ๋Š” ๊ฒฝ๋กœ ๊ทธ๋Œ€๋กœ ์ž‘์„ฑ -->
  <mapper namespace="com.smhrd.myapp.mapper.MemberMapper">
  	<select id="memberList" resultType="com.smhrd.myapp.domain.Member">
  		select * from member;
  	</select>
  </mapper>

 

 

์˜ค๋ฅ˜

 

 

jsp  ํŒŒ์ผ ์˜ค๋ฅ˜

 

 

๋ฐ˜์  ์—†์Œ!

 

 

๊ตฌํ˜„ํ™”๋ฉด

 

 

์ถ”๊ฐ€. MySQL WorkBench ์‚ฌ์šฉ

 

 

์Šคํ‚ค๋งˆ ์ƒ์„ฑ

 

 

 

๊ธฐ๋ณธ ์Šคํ‚ค๋งˆ ์„ค์ •

 

 

 

ํ…Œ์ด๋ธ” ์ƒ์„ฑ

 

 

๋ฐ์ดํ„ฐ ์‚ฝ์ž