Notice
Recent Posts
Recent Comments
ยซ   2024/11   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
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
Archives
Today
In Total
๊ด€๋ฆฌ ๋ฉ”๋‰ด

A Joyful AI Research Journey๐ŸŒณ๐Ÿ˜Š

[63] 230329 Spring: ์Šคํ”„๋ง์„ ์ด์šฉํ•œ MVC ๊ตฌํ˜„ [K-๋””์ง€ํ„ธ ํŠธ๋ ˆ์ด๋‹ 63์ผ] ๋ณธ๋ฌธ

๐ŸŒณBootcamp Revision 2023โœจ/Spring Framework, Java

[63] 230329 Spring: ์Šคํ”„๋ง์„ ์ด์šฉํ•œ MVC ๊ตฌํ˜„ [K-๋””์ง€ํ„ธ ํŠธ๋ ˆ์ด๋‹ 63์ผ]

yjyuwisely 2023. 3. 29. 10:50

230329 Wed 63rd class
Ch. 18 MVC ํŒจํ„ด ๊ตฌํ˜„
์ง„๋„: Ch. 18 MVC ํŒจํ„ด ๊ตฌํ˜„์— ๊ธฐ๋ฐ˜ํ•œ ์ž์ฒด ์ˆ˜์—… (๊ต์žฌ: ์ตœ๋ฒ”๊ท ์˜ JSP 2.3 ์›น ํ”„๋กœ๊ทธ๋ž˜๋ฐ: ๊ธฐ์ดˆ๋ถ€ํ„ฐ ์ค‘๊ธ‰๊นŒ์ง€, ์ €์ž: ์ตœ๋ฒ”๊ท )
์ฑ… ์˜ˆ์ œ ์ฝ”๋“œ: https://github.com/madvirus/jsp23
์ €์ž ๋ธ”๋กœ๊ทธ: https://javacan.tistory.com/

 

์ตœ๋ฒ”๊ท ์˜ JSP 2.3 ์›น ํ”„๋กœ๊ทธ๋ž˜๋ฐ: ๊ธฐ์ดˆ๋ถ€ํ„ฐ ์ค‘๊ธ‰๊นŒ์ง€ | ์ตœ๋ฒ”๊ท  - ๊ต๋ณด๋ฌธ๊ณ 

์ตœ๋ฒ”๊ท ์˜ JSP 2.3 ์›น ํ”„๋กœ๊ทธ๋ž˜๋ฐ: ๊ธฐ์ดˆ๋ถ€ํ„ฐ ์ค‘๊ธ‰๊นŒ์ง€ | [์ตœ๋ฒ”๊ท ์˜ JSP 2.3 ์›น ํ”„๋กœ๊ทธ๋ž˜๋ฐ ๊ธฐ์ดˆ๋ถ€ํ„ฐ ์ค‘๊ธ‰๊นŒ์ง€]๋Š” JSP 2.3์˜ ์ƒˆ๋กœ์šด ํŠน์ง• ๋ฐ˜์˜๊ณผ JSP๋ฅผ ์ง€์›ํ•˜๋Š” ์š”์†Œ์ธ ์„œ๋ธ”๋ฆฟ๊ณผ ํ‘œํ˜„ ์–ธ์–ด ๋“ฑ์˜ ์ƒˆ๋กœ์šด

product.kyobobook.co.kr

์ฐธ๊ณ  
https://wikidocs.net/book/5792

 

์Šคํ”„๋ง MVC ํ•˜๋ฃจ๋งŒ์— ๋ฐฐ์šฐ๊ธฐ

์ด ์‹œ๋ฆฌ์ฆˆ๋Š” ์ฑ…์œผ๋กœ ์ถœ๊ฐ„๋œ [์Šคํ”„๋ง MVC ํ•˜๋ฃจ๋งŒ์— ๋ฐฐ์šฐ๊ธฐ](http://www.yes24.com/Product/Goods/96581707) ๋ฅผ ๋‹ค๋ฃน๋‹ˆ๋‹ค. ![์Šคํ”„๋ง MVC…

wikidocs.net

https://www.javatpoint.com/spring-tutorial

 

Learn Spring Tutorial - javatpoint

Learn Spring Tutorial. This spring tutorial for beginners and professionals provides in depth learning of DI, AOP, Data Access, MVC, Remoting, ORM and Integration.

www.javatpoint.com


์˜ค๋Š˜ ๋ฐฐ์šด ๊ฒƒ ์ค‘ ๊ธฐ์–ตํ•  ๊ฒƒ์„ ์ •๋ฆฌํ–ˆ๋‹ค.


๋‹ค์‹œ ๊ธฐ์–ตํ•  ๊ฒƒ 

ํด๋ž˜์Šค๋กœ๋ถ€ํ„ฐ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๋ ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด new ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

public class Car{ //Carํด๋ž˜์Šค ์„ ์–ธ

}

//Carํด๋ž˜์Šค์˜ ๊ฐ์ฒด ์ƒ์„ฑ
new Car();

new๋Š” ํด๋ž˜์Šค๋กœ๋ถ€ํ„ฐ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑ์‹œํ‚ค๋Š” ์—ฐ์‚ฐ์ž์ด๋‹ค.
new ์—ฐ์‚ฐ์ž๋Š” ํž™ ์˜์—ญ์— ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑ์‹œํ‚จ ํ›„ ๊ฐ์ฒด์˜ ๋ฒˆ์ง€๋ฅผ ๋ฆฌํ„ดํ•˜๋„๋ก ๋˜์–ด ์žˆ๋‹ค. 

//ํด๋ž˜์Šค๋กœ ์„ ์–ธ๋œ ๋ณ€์ˆ˜์— new ์—ฐ์‚ฐ์ž๊ฐ€ ๋ฆฌํ„ดํ•œ ๊ฐ์ฒด์˜ ๋ฒˆ์ง€๋ฅผ ์ €์žฅํ•œ๋‹ค.
ํด๋ž˜์Šค ๋ณ€์ˆ˜;
๋ณ€์ˆ˜ = new ํด๋ž˜์Šค();

//1๊ฐœ์˜ ์‹คํ–‰๋ฌธ์œผ๋กœ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค.
ํด๋ž˜์Šค ๋ณ€์ˆ˜ = new ํด๋ž˜์Šค();

2023.02.18 - [๐Ÿ’ป Self-Study Revisionโœจ/๋‚˜์˜ ๊ฐœ๋ฐœ ์ผ์ง€โœ๐Ÿป] - ๐ŸŒท[K-๋””์ง€ํ„ธ ํŠธ๋ ˆ์ด๋‹ 24์ผ์ฐจ~] Java: ๋‹ค์‹œ ๊ธฐ์–ตํ•  ๊ฒƒ ๋ชฉ๋ก


ํ”„๋กœ์ ํŠธ์˜ ๊ตฌ์„ฑ


Business: ์„œ๋น„์Šค (ex. ํšŒ์›๊ฐ€์ž… insert, ๋กœ๊ทธ์ธ ์„œ๋น„์Šค -> MyBatis -> DB) 

์œ ์ง€๋ณด์ˆ˜๊นŒ์ง€ ๊ณ ๋ คํ•œ ๊ตฌ์กฐ์ด๋‹ค.
๋‹จ์ˆœํ•˜๊ฒŒ ํ™”๋ฉด์„ ๋„์šฐ๋Š” ๊ฒƒ์€ DB์ž‘์—…์„ ์•ˆ ํ•ด๋„ ๋œ๋‹ค.


http://localhost:8080/(์›น๋ธŒ๋ผ์šฐ์ €) -> controller -> WEB-INF/views/home.jsp๋ฅผ ์‹คํ–‰ํ•œ๋‹ค.
/: @RequestMapping์˜ value๊ฐ€ /์ธ ๊ฒƒ์„ ์‹คํ–‰ํ•œ๋‹ค. 
๋ฉ”์„œ๋“œ๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ ๋‹ค GET๋ฐฉ์‹์ด๋‹ค.
๋‹จ์ˆœํ•˜๊ฒŒ ํ™”๋ฉด์„ ์—ฐ๊ฒฐํ•œ๋‹ค. (-> Presentation ์˜์—ญ๋งŒ ์žˆ์œผ๋ฉด ๋œ๋‹ค.)
DB ์ž‘์—…์€ ํ•˜์ง€ ์•Š์œผ๋ฏ€๋กœ, ์„œ๋น„์Šค๋Š” ํ•„์š”์—†๋‹ค.
(-> DB, MyBatis๋Š” ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค.) 

์ฝ”๋“œ) HomeController.java ์ผ๋ถ€

package org.hj.controller;

import java.util.Locale;

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

@Controller
public class HomeController {
	
	@RequestMapping(value = "/", method = RequestMethod.GET)
	public String home(Locale locale, Model model) { 
		return "home"; //home์ด๋ผ๋Š” ๋ฉ”์„œ๋“œ๋ฅผ ์‹คํ–‰ํ•œ๋‹ค. ๋ฉ”๋ชจ์žฅ์˜ URL์ฃผ์†Œ๊ฐ€ ํ˜ธ์ถœํ•œ ๊ฒƒ์ด๋‹ค. 
						// /WEB-INF/views/home.jsp
	}

๋งŒ์•ฝ main.jsp๋ฅผ ์›ํ•˜๋ฉด home -> main์œผ๋กœ ํ•˜๋ฉด ๋œ๋‹ค.


์ปจํŠธ๋กค๋Ÿฌ๋ฅผ ๋‚˜๋ˆ ์•ผ ๋‹ค๋ฅธ ํŒ€์›๋“ค๋ผ๋ฆฌ ์ž‘์—…์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

ํšŒ์›๊ฐ€์ž…

http://localhost8080/member -> MemberController -> WEB-INF/views/member/memberin.jsp
ํšŒ์›๊ฐ€์ž… ํ™”๋ฉด์ด ๋‚˜์˜จ๋‹ค.

์ฝ”๋“œ) MemberController.java ์ผ๋ถ€

package org.hj.controller;

import javax.servlet.http.HttpSession;

import org.hj.model.LoginVO;
import org.hj.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class MemberController {

	@Autowired
	LoginService ls;
	
	// ํšŒ์›๊ฐ€์ž… //ํ™”๋ฉด ์‹คํ–‰์„ ์œ„ํ•œ ๋ฉค๋ฒ„ (GET๋ฐฉ์‹)
	@RequestMapping(value = "/member", method = RequestMethod.GET) //member ๋ฉ”์„œ๋“œ GET ๋ฐฉ์‹
	public String member () {
		return "member/memberin"; //๋ฉค๋ฒ„ ํด๋”์˜ memberin.jsp๋ฅผ ์‹คํ–‰ํ•œ๋‹ค.
	}

๋งŒ์•ฝ ์˜ค๋ฅ˜๊ฐ€ ๋‚œ๋‹ค๋ฉด ์ดํด๋ฆฝ์Šค์—์„œ ์„œ๋ฒ„ ๋”๋ธ”ํด๋ฆญ ํ›„ Modules์—์„œ Path๊ฐ€ /๋กœ ๋˜์–ด์žˆ๋Š”์ง€ ์ฒดํฌํ•ด๋ณธ๋‹ค.

๊ฒฐ๊ณผ)


์•„์ด๋”” ์ค‘๋ณต ์ฒดํฌ ๋“ฑ๋„ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋กœ ํ•œ๋‹ค.

์ฝ”๋“œ) memberin.jsp ์ผ๋ถ€
(๊ฒฝ๋กœ: SpringEx\src\main\webapp\WEB-INF\views\member\memberin.jsp)
๊ธฐ๋ณธ ๋ฐฉ์‹์€ GET์ธ๋ฐ, ๋ณด์•ˆ์ƒ ์ˆจ๊ฒจ์•ผํ•˜๋ฏ€๋กœ POST๋ฐฉ์‹์„ ์„ ํƒํ•œ๋‹ค.

<h1>ํšŒ์›๊ฐ€์ž…</h1>

<form action="/member" method="post"><!-- ๋ฉ”์„œ๋“œ๋ฐฉ์‹:POST -->
์•„์ด๋”” <input type="text" name="id">
๋น„๋ฐ€๋ฒˆํ˜ธ <input type="text" name="password">
๋ณ„๋ช… <input type="text" name="name">
<input type="submit" value="๊ฐ€์ž…ํ•˜๊ธฐ">

</form>

MemberController.java์™€ ๋น„๊ตํ–ˆ์„ ๋•Œ ๋ฉ”์„œ๋“œ ๋ฐฉ์‹์ด POST์ด๋ฏ€๋กœ ์•„๋ž˜ ๋‘๋ฒˆ์งธ์˜ ํšŒ์›๊ฐ€์ž… ์„œ๋ฒ„๋ฅผ ์‹คํ–‰ํ•œ๋‹ค.

์ฝ”๋“œ) MemberController.java ์ผ๋ถ€
์‹ค์ œ๋กœ insert๋ฅผ ํ•˜๊ธฐ ์œ„ํ•ด ์‹คํ–‰๋œ๋‹ค. 

@Controller
public class MemberController {

	@Autowired
	LoginService ls; //LoginService ls = new LoginService(); ํด๋ž˜์Šค๋ฅผ new๋ฅผ ์จ์„œ ๊ฐ์ฒดํ™”ํ•œ ๊ฒƒ
	
	// ํšŒ์›๊ฐ€์ž…์„œ๋ฒ„ //์‹ค์ œ๋กœ ๊ฐ€์ž…ํ•˜๊ธฐ ์œ„ํ•œ ๋ฉค๋ฒ„ (POST๋ฐฉ์‹)
	@RequestMapping(value = "/member", method = RequestMethod.POST) //๋‘˜ ๋‹ค GET์ด๋ฉด ์ถฉ๋Œ์ด ์ผ์–ด๋‚œ๋‹ค. ๋ฉ”์„œ๋“œ ๋ฐฉ์‹์ด POST์ด๋‹ค.
	//							int		a //member๋Š” ์ฐธ์กฐ ๋ณ€์ˆ˜
	//public String memberPost(String id, String password, String name) //๋ฉ”๋ชจ๋ฆฌ ์ฐจ์ง€๊ฐ€ ํฌ๋ฏ€๋กœ ์“ฐ์ง€ ์•Š๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค. private ๊ฐ™์€ ์ ‘๊ทผ ์ œํ•œ์ž๋„ ์—†๋‹ค.
	public String memberPost (LoginVO member) { // ๋ชจ๋ธ: LoginVO
		System.out.println(member); //member.toString()์ด ์ƒ๋žต๋œ ๊ฒƒ์ด๋‹ค.
		//#####์ƒˆ๋กœ ์ถ”๊ฐ€ํ•จ#####
		ls.memreg(member);
		return "board/list"; //board ํด๋”์˜ list.jsp๋ฅผ ์‹คํ–‰ํ•œ๋‹ค. ๊ฒŒ์‹œํŒ ๋ชฉ๋ก์„ ๋ณด์—ฌ์ค€๋‹ค.
	}
 }

LoginVO ๋ชจ๋ธ์„ ์“ฐ๋Š” ์ด์œ 

์˜ˆ์‹œ) ์ž๋ฐ”์˜ ๋ฉ”์„œ๋“œ ์„ ์–ธ๊ณผ ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ
์ธ์ˆ˜๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜์— ๋“ค์–ด๊ฐ„๋‹ค.

//๋ฉ”์„œ๋“œ ์„ ์–ธ
public String memberPost(int a){ //๋งค๊ฐœ๋ณ€์ˆ˜
	return "board/list";
}

//๋ฉ”์„œ๋“œ ํ˜ธ์ถœ
memberPost(10); //์ธ์ˆ˜

์Šคํ”„๋ง์ด ๋‚ด๋ถ€์ ์œผ๋กœ memberPost("id1234", "5678910", "์œ ์ž๋ฐ”")๋ฅผ ์ฒ˜๋ฆฌํ•œ๋‹ค.
์ž๋™์œผ๋กœ ํ•ด์ค€๋‹ค. ๊ทธ๋ ‡๊ฒŒ ํ•˜๋„๋ก ์„ค๊ณ„๋˜์–ด์žˆ๋‹ค.

//๋ฉ”์„œ๋“œ ์„ ์–ธ
public String memberPost(String id, String password, String name){ //๋งค๊ฐœ๋ณ€์ˆ˜
	return "board/list";
}

//๋ฉ”์„œ๋“œ ํ˜ธ์ถœ (์Šคํ”„๋ง์ด ๋‚ด๋ถ€์ ์œผ๋กœ ์ฒ˜๋ฆฌํ•˜๋Š” ๊ฒƒ)
memberPost("aaa", "1234", "์ •์ž๋ฐ”");

๋ฉ”๋ชจ๋ฆฌ ์ฐจ์ง€๊ฐ€ ํฌ๋ฏ€๋กœ ์“ฐ์ง€ ์•Š๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค. private ๊ฐ™์€ ์ ‘๊ทผ ์ œํ•œ์ž๋„ ์—†๋‹ค. 
-> ํšจ์œจ์ ์ด์ง€ ์•Š์œผ๋ฏ€๋กœ ๋ชจ๋ธ์ธ LoginVO์„ ์“ด๋‹ค. (๋ชจ๋ธ์€ ํ•„์ˆ˜๊ฐ€ ์•„๋‹ˆ๊ณ  ์„ ํƒ์ด๋‹ค.) 

๋ชจ๋ธ์„ ์“ฐ๋Š” ์ด์œ : private์ด ์žˆ๊ณ , String ์ฐธ์กฐ ํƒ€์ž…(์ฃผ์†Œ๋ฅผ ์ €์žฅ)์„ ์“ด๋‹ค.
LoginVO (8๋ฐ”์ดํŠธ๋งŒ ์ฐจ์ง€ํ•œ๋‹ค.)
-> ๋” ํšจ์œจ์ ์ด๋‹ค.

์ฝ”๋“œ) MemberController.java์™€ LoginVO.java

์ฝ”๋“œ) LoginVO.java ์ผ๋ถ€

package org.hj.model;

public class LoginVO {

	
	private String id;
	private String password;
	private String addr;
	private String phone;
	private String email;
	private String name;
	private int age;
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}

์ฝ”๋“œ) MemberController.java ์ผ๋ถ€
์‹ค์ œ๋กœ insert๋ฅผ ํ•˜๊ธฐ ์œ„ํ•ด ์‹คํ–‰๋œ๋‹ค. 

	// ํšŒ์›๊ฐ€์ž…์„œ๋ฒ„ //์‹ค์ œ๋กœ ๊ฐ€์ž…ํ•˜๊ธฐ ์œ„ํ•œ ๋ฉค๋ฒ„ (POST๋ฐฉ์‹)
	@RequestMapping(value = "/member", method = RequestMethod.POST) //๋‘˜ ๋‹ค GET์ด๋ฉด ์ถฉ๋Œ์ด ์ผ์–ด๋‚œ๋‹ค. ๋ฉ”์„œ๋“œ ๋ฐฉ์‹์ด POST์ด๋‹ค.
	//							int		a //member๋Š” ์ฐธ์กฐ ๋ณ€์ˆ˜
	//public String memberPost(String id, String password, String name) //๋ฉ”๋ชจ๋ฆฌ ์ฐจ์ง€๊ฐ€ ํฌ๋ฏ€๋กœ ์“ฐ์ง€ ์•Š๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค. private ๊ฐ™์€ ์ ‘๊ทผ ์ œํ•œ์ž๋„ ์—†๋‹ค.
	public String memberPost (LoginVO member) { // ๋ชจ๋ธ: LoginVO
		System.out.println(member); //member.toString()์ด ์ƒ๋žต๋œ ๊ฒƒ์ด๋‹ค.
		//#####์ƒˆ๋กœ ์ถ”๊ฐ€ํ•จ#####
		ls.memreg(member);
		return "board/list"; //board ํด๋”์˜ list.jsp๋ฅผ ์‹คํ–‰ํ•œ๋‹ค. ๊ฒŒ์‹œํŒ ๋ชฉ๋ก์„ ๋ณด์—ฌ์ค€๋‹ค.
	}

LoginVO member (ex. int a)

int a = 10;

System.out.println(a); //์ถœ๋ ฅํ•  ๋•Œ๋Š” ๋ณ€์ˆ˜ ์ด๋ฆ„ a๋ฅผ ์“ด๋‹ค.

๊ฒฐ๊ณผ)

์ฝ˜์†”์ฐฝ์— ์ธ์ถœ๋œ ๊ฐ’์ด ๋‚˜์˜จ๋‹ค.

LoginVO [id=id0329, password=5678910, addr=null, phone=null, email=null, name=์œ ์ž๋ฐ”, age=0]

์ปจํŠธ๋กค๋Ÿฌ์™€ ์„œ๋น„์Šค์˜ ์—ฐ๊ฒฐ

์ฝ”๋“œ) MemberController.java ์ผ๋ถ€

์ปจํŠธ๋กค๋Ÿฌ์™€ ์„œ๋น„์Šค๋ฅผ ์—ฐ๊ฒฐํ•˜๋ ค๋ฉด ์„œ๋น„์Šค(LoginService)๊ฐ€ ์ปจํŠธ๋กค๋Ÿฌ(MemberController.java)์— ํฌํ•จ๋˜์–ด ์žˆ์–ด์•ผ ํ•œ๋‹ค. (ํฌํ•จ ๊ด€๊ณ„๊ฐ€ ๋˜์–ด์•ผํ•œ๋‹ค.)

import org.hj.service.LoginService;

@Controller
public class MemberController {

	@Autowired
	LoginService ls; //LoginService ls = new LoginService(); ํด๋ž˜์Šค๋ฅผ new๋ฅผ ์จ์„œ ๊ฐ์ฒดํ™”ํ•œ ๊ฒƒ. 
	//LoginService๋Š” ์ธํ„ฐํŽ˜์ด์Šค์ด๋ฏ€๋กœ ์›๋ž˜ new๋Š” ์•ˆ ๋˜๋Š” ๋ฐ ์œ„์— @AutoWired ์ด์šฉํ•ด์„œ ๊ฐ€๋Šฅํ•˜๊ฒŒ ํ–ˆ๋‹ค.
}

=> MemberController.java์•ˆ์— LoginService๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ๋‹ค.


Presentation    Business    Persistencetier   DB
SpringMVC <---------------> Spring Core  <---------------> MyBatis <------------>  
  <-------------------------------------------------------------------------------------------------------------  
MemberController.
java
  LoginService.
java 
  LoginMapper
from
LoginServiceImpl.
java
   
๋ฉ”์„œ๋“œ
member();
memberPost();
--------------->
member
๋ฐ์ดํ„ฐ๋ฅผ ๋„˜๊ธด๋‹ค.
ls

๋ฉ”์„œ๋“œ

memreg();
login();
--------------->
member
๋ฐ์ดํ„ฐ๋ฅผ ๋„˜๊ธด๋‹ค.
lm

๋ฉ”์„œ๋“œ

memreg();
login();
   

๋ฐ์ดํ„ฐ๋ฅผ ๋น„์ฆˆ๋‹ˆ์Šค ์˜์—ญ์— ์ค€๋‹ค. -> ๊ทธ ํ›„ DB๊นŒ์ง€ ์ „๋‹ฌ๋œ๋‹ค. 

๋ฉ”์„œ๋“œ์˜ return์— ์˜ํ•ด DB์—์„œ ํ™”๋ฉด์œผ๋กœ (ํ˜ธ์ถœํ•œ ๊ณณ์œผ๋กœ) ๋˜๋Œ์•„๊ฐ„๋‹ค.

Mapper๋Š” ๊ตฌํ˜„ ๋ฉ”์„œ๋“œ๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š๋Š”๋‹ค. return์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค.
์ฝ”๋“œ) LoginMapper.xml ์ผ๋ถ€

	<select id="login" resultType="org.hj.model.LoginVO"> <!-- ๊ตฌํ˜„๋ฉ”์„œ๋“œ๊ฐ€ ์—†์œผ๋‹ˆ๊นŒ return์ด ์—†๋‹ค. -->
	<!-- ๋Œ€์‹  select๋Š” resultType์ด๋ผ๋Š” ํƒ€์ž…์ด return ์—ญํ• ์ด๋‹ค. -->
  		select id, password
  		from member
  		where id=#{id} and password=#{password}
	</select>

member ๋ฐ์ดํ„ฐ๋Š” ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

LoginVO [id=id0329, password=5678910, addr=null, phone=null, email=null, name=์œ ์ž๋ฐ”, age=0]

์ฝ”๋“œ) LoginService.java
์„ค๊ณ„ ์—ญํ• ์ด๋‹ค.
์ถ”์ƒ๋ฉ”์„œ๋“œ๋งŒ ์„ค๊ณ„ํ–ˆ๋‹ค.

package org.hj.service;

import org.hj.model.LoginVO;

public interface LoginService { //์ธํ„ฐํŽ˜์ด์Šค๊ฐ€ new()๋ฅผ ํ•  ์ˆ˜ ์—†์œผ๋ฏ€๋กœ MemberController.java์— @Autowired๋ฅผ ์„ค์ •
	//#####์ƒˆ๋กœ ์ถ”๊ฐ€ํ•จ#####
	public void memreg(LoginVO member); //์ถ”์ƒ๋ฉ”์„œ๋“œ์ด๋ฏ€๋กœ LoginServiceImpl.java์—์„œ ๊ตฌํ˜„ํ•œ๋‹ค.
	public LoginVO login(LoginVO member);
}

์ฝ”๋“œ) LoginServiceImpl.java
implements๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๊ตฌํ˜„ํ•œ๋‹ค.

package org.hj.service;

import org.hj.mapper.LoginMapper;
import org.hj.model.LoginVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class LoginServiceImpl implements LoginService {
	
	@Autowired
	LoginMapper lm;
	
	//#####์ƒˆ๋กœ ์ถ”๊ฐ€ํ•จ#####
	public void memreg(LoginVO member) {
		lm.memreg(member);
	};
	
	public LoginVO login (LoginVO member) {
		System.out.println("service="+member);
		System.out.println("service return="+lm.login(member));
		return lm.login(member);
	}
}

Mapper ์ธํ„ฐํŽ˜์ด์Šค

์ฝ”๋“œ) LoginMapper.java
Mapper ์ธํ„ฐํŽ˜์ด์Šค๋Š” Mapping ํŒŒ์ผ์— ๊ธฐ์žฌ๋œ SQL์„ ํ˜ธ์ถœํ•˜๊ธฐ ์œ„ํ•œ ์ธํ„ฐํŽ˜์ด์Šค์ด๋‹ค.
์ฐธ๊ณ : https://bigstupid.tistory.com/23

package org.hj.mapper;

import org.hj.model.LoginVO;

public interface LoginMapper {
	//#####์ƒˆ๋กœ ์ถ”๊ฐ€ํ•จ#####
	public void memreg(LoginVO member);
	public LoginVO login(LoginVO member);
}

์•„๋ž˜์ฒ˜๋Ÿผ ์‚ฌ์šฉํ–ˆ์„ ๋•Œ ๋ถˆํŽธํ•œ ์ ์€ SQL๋ฌธ์žฅ์ด ๊ธธ์–ด์ง€๋ฉด ์‚ฌ์šฉํ•˜๊ธฐ ๋ถˆํŽธํ•˜๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. ๊ทธ๋ž˜์„œ Mapper๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

select *
from(select @rownum:=@rownum+1 rownum, b.*
from board b, (select @rownum:=0) as tmp
order by no desc
) as boardlist
where rownum > 1 <= 10

์›๋ž˜์˜ SQL ์ฝ”๋“œ

public class LoginMapper implements LoginMapper {
	//String sql = "select * from member"; // ํ•œ ์ค„์€ ๊ฐ„ํŽธํ•œ๋ฐ ์—ฌ๋Ÿฌ ์ค„์ด๋ฉด ๋ถˆํŽธํ•˜๋‹ค.
    String sql = "select *
    	sql += "from(select @rownum:=@rownum+1 rownum, b.*
        sql += " from board b, (select @rownum:=0) as tmp"
}

memreg์˜ ๊ตฌํ˜„ ๋ฐ ์—ฐ๊ฒฐ

LoginService.java -> LoginServiceImpl.java : ์ถ”์ƒ๋ฉ”์„œ๋“œ memreg๋ฅผ ๊ตฌํ˜„ํ•œ๋‹ค. 

LoginMapper.java -> LoginMapper.xml : memreg ์—ฐ๊ฒฐํ•œ๋‹ค. insert ํƒœ๊ทธ๋ฅผ ์จ์•ผํ•œ๋‹ค. 

๊ฐ memreg ๊ตฌํ˜„ (LoginService.java -> LoginServiceImpl.java) ๋ฐ ์—ฐ๊ฒฐ (LoginMapper.java -> LoginMapper.xml)์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค.

์ฝ”๋“œ) LoginMapper.xml

<?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="org.hj.mapper.LoginMapper">
  
  <!-- #####์ƒˆ๋กœ ์ถ”๊ฐ€ํ•จ##### -->
	<insert id="memreg"> <!-- LoginMapper.java์˜ memreg๋ฅผ ์—ฐ๊ฒฐํ•œ๋‹ค. insertํƒœ๊ทธ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.-->
  		insert into member (id, password, name, birthday, gender)
  		values(#{id},#{password},#{name},sysdate(),"f");
	</insert>
  
  	
	<select id="login" resultType="org.hj.model.LoginVO">
  		select id, password
  		from member
  		where id=#{id} and password=#{password}
	</select>
  
  </mapper>

DB์˜ Insert์˜ ์ง„ํ–‰ ๊ณผ์ •

LoginMapper.java์˜ LoginVO member๊ฐ€ 

	public void memreg(LoginVO member);

๊ฐ€ ์•„๋ž˜ LoginMapper.xml์˜ "memreg"์— ์—ฐ๊ฒฐ๋˜๋ฉฐ,

	<insert id="memreg"> <!-- LoginMapper.java์˜ memreg๋ฅผ ์—ฐ๊ฒฐํ•œ๋‹ค. insertํƒœ๊ทธ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.-->
  		insert into member (id, password, name, birthday, gender)
  		values(#{id},#{password},#{name},sysdate(),"f");
	</insert>

์ฝ˜์†”์ฐฝ์— ์ธ์ถœ๋œ ๊ฐ’์ด 

์ฝ˜์†”์ฐฝ ๊ฒฐ๊ณผ

LoginVO [id=id0329, password=5678910, addr=null, phone=null, email=null, name=์œ ์ž๋ฐ”, age=0]

LoginMapper.xml์˜ ์•„๋ž˜ ๊ฐ’์— ์—ฐ๊ฒฐ๋œ๋‹ค.

values(#{id},#{password},#{name},sysdate(),"f");

๋กœ๊ทธ์ธ ํ™”๋ฉด

์ฝ”๋“œ) home.jsp
<input>์ด๋‚˜ <a>ํƒœ๊ทธ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page session="false" %>
<html>
<head>
	<meta charset="UTF-8">
	<title>Home</title>
	<!-- css -->
	<!-- javascript -->
</head>
<body>
<h1>
	Hello world!  
</h1>
Main page์ž…๋‹ˆ๋‹ค.<br><br> <!-- ๋ฐฉ์‹ ์ง€์ • ์—†์œผ๋ฉด GET ๋ฐฉ์‹์ด๋‹ค. -->
<input type=button value="๋กœ๊ทธ์ธ ํ•˜๋Ÿฌ๊ฐ€๊ธฐ" onclick="location.href='http://localhost:8081/login'">
<a href="/login">๋กœ๊ทธ์ธ ํ•˜๋Ÿฌ ๊ฐ€๊ธฐ</a>

</body>
</html>

๊ฒฐ๊ณผ) 


๋ฐ์ดํ„ฐ์˜ ํ๋ฆ„

login.jsp: ๋ฐ์ดํ„ฐ 

-> MemberController.java: member -> member

-> LoginService.java์˜ member

-> LoginServiceImpl.java์˜ member from public LoginVO login (LoginVO member) -> return lm.login(member)


-> LoginMapper.java์˜ member from public LoginVO login(LoginVO member);


-> LoginMapper.xml์˜ <select>์˜ #{id}, #{password}๊ฐ€ ์ผ์น˜ํ•ด์•ผํ•œ๋‹ค.

๊ทธ ํ›„ MySql์˜ DB์— ์ €์žฅ๋œ๋‹ค.


Mapper XML Files

์ฝ”๋“œ) LoginMapper.xml ์ผ๋ถ€
(๊ฒฝ๋กœ: SpringEx\src\main\resources\org\hj\mapper\LoginMapper.xml)

	<select id="login" resultType="org.hj.model.LoginVO"> <!-- ๊ตฌํ˜„๋ฉ”์„œ๋“œ๊ฐ€ ์—†์œผ๋‹ˆ๊นŒ return์ด ์—†๋‹ค. -->
	<!-- ๋Œ€์‹  select๋Š” resultType์ด๋ผ๋Š” ํƒ€์ž…์ด return ์—ญํ• ์ด๋‹ค. -->
  		select id, password
  		from member
  		where id=#{id} and password=#{password}
	</select>

๊ตฌํ˜„๋ฉ”์„œ๋“œ๊ฐ€ ์—†์œผ๋‹ˆ๊นŒ return์ด ์—†๋‹ค. 
๋Œ€์‹  select๋Š” resultType์ด๋ผ๋Š” ํƒ€์ž…์ด return ์—ญํ• ์ด๋‹ค. 

resultType The fully qualified class name or alias for the expected type that will be returned from this statement.

Note that in the case of collections, this should be the type that the collection contains,
not the type of the collection itself.

Use resultType OR resultMap, not both.

์ฐธ๊ณ : https://mybatis.org/mybatis-3/ko/sqlmap-xml.html
์ฐธ๊ณ : https://mybatis.org/mybatis-3/sqlmap-xml.html#mapper-xml-files

์ฝ”๋“œ)
LoginServiceImpl.java
์•„๋ž˜ ์ฝ”๋“œ๋Š” resultType์˜ ํ˜ธ์ถœ์„ ํ•œ๋‹ค.

return lm.longin(member)
package org.hj.service;

import org.hj.mapper.LoginMapper;
import org.hj.model.LoginVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class LoginServiceImpl implements LoginService {
	
	@Autowired
	LoginMapper lm;
	
	//#####์ƒˆ๋กœ ์ถ”๊ฐ€ํ•จ#####
	public void memreg(LoginVO member) {
		lm.memreg(member);
	};
	
	public LoginVO login (LoginVO member) {
		System.out.println("service="+member);
		System.out.println("service return="+lm.login(member));
		return lm.login(member);
	}
}

์•„๋ž˜ LoginVO.java๋ฅผ ๊ทธ๋Œ€๋กœ return ํ•œ๋‹ค. 

์ฝ”๋“œ) LoginVO.java ์ผ๋ถ€ 

public class LoginVO {

	private String id;
	private String password;
	private String addr;
	private String phone;
	private String email;
	private String name;
	private int age;
}

์œ„ ๋ฐ์ดํ„ฐ๋Š” ๊ฒฐ๊ตญ MemberController.java์˜
session.setAttribute("login", 
์˜ session์— ์ €์žฅ์„ ํ•œ๋‹ค. (JSP์™€ ๋™์ผํ•˜๋‹ค.)

์ฝ”๋“œ) MemberController.java ์ผ๋ถ€ 
๋‹ค์‹œ ์ˆ˜์ •๋๋‹ค.

		if (ls.login(member)==null) {
			return "member/login";
			} else {
				session.setAttribute("login", ls.login(member)); //null์ด ์•„๋‹ˆ๋ฉด ์„ธ์…˜์— ์ €์žฅํ•œ๋‹ค.
				//return "redirect:/list"; //๊ฒŒ์‹œํŒ ๋ฆฌ์ŠคํŠธ ๋ณด๊ธฐ ์œ„ํ•ด ๋‚ด๊ฐ€ ์•„๋ž˜ ์ฝ”๋“œ๋กœ ๋ฐ”๊ฟˆ.
				return "board/list";
			}

 

728x90
๋ฐ˜์‘ํ˜•
Comments