์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
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 |
- Absolute
- AGI
- ai
- AI agents
- AI engineer
- AI researcher
- ajax
- algorithm
- Algorithms
- aliases
- Array ๊ฐ์ฒด
- ASI
- bayes' theorem
- Bit
- Blur
- BOM
- bootstrap
- canva
- challenges
- ChatGPT
- Today
- In Total
A Joyful AI Research Journey๐ณ๐
[63] 230329 Spring: ์คํ๋ง์ ์ด์ฉํ MVC ๊ตฌํ [K-๋์งํธ ํธ๋ ์ด๋ 63์ผ] ๋ณธ๋ฌธ
[63] 230329 Spring: ์คํ๋ง์ ์ด์ฉํ MVC ๊ตฌํ [K-๋์งํธ ํธ๋ ์ด๋ 63์ผ]
yjyuwisely 2023. 3. 29. 10:50230329 Wed 63rd class
Ch. 18 MVC ํจํด ๊ตฌํ
์ง๋: Ch. 18 MVC ํจํด ๊ตฌํ์ ๊ธฐ๋ฐํ ์์ฒด ์์
(๊ต์ฌ: ์ต๋ฒ๊ท ์ JSP 2.3 ์น ํ๋ก๊ทธ๋๋ฐ: ๊ธฐ์ด๋ถํฐ ์ค๊ธ๊น์ง, ์ ์: ์ต๋ฒ๊ท )
์ฑ
์์ ์ฝ๋: https://github.com/madvirus/jsp23
์ ์ ๋ธ๋ก๊ทธ: https://javacan.tistory.com/
์ฐธ๊ณ
https://wikidocs.net/book/5792
https://www.javatpoint.com/spring-tutorial
์ค๋ ๋ฐฐ์ด ๊ฒ ์ค ๊ธฐ์ตํ ๊ฒ์ ์ ๋ฆฌํ๋ค.
๋ค์ ๊ธฐ์ตํ ๊ฒ
ํด๋์ค๋ก๋ถํฐ ๊ฐ์ฒด๋ฅผ ์์ฑํ๋ ค๋ฉด ๋ค์๊ณผ ๊ฐ์ด new ์ฐ์ฐ์๋ฅผ ์ฌ์ฉํ๋ค.
public class Car{ //Carํด๋์ค ์ ์ธ
}
//Carํด๋์ค์ ๊ฐ์ฒด ์์ฑ
new Car();
new๋ ํด๋์ค๋ก๋ถํฐ ๊ฐ์ฒด๋ฅผ ์์ฑ์ํค๋ ์ฐ์ฐ์์ด๋ค.
new ์ฐ์ฐ์๋ ํ ์์ญ์ ๊ฐ์ฒด๋ฅผ ์์ฑ์ํจ ํ ๊ฐ์ฒด์ ๋ฒ์ง๋ฅผ ๋ฆฌํดํ๋๋ก ๋์ด ์๋ค.
//ํด๋์ค๋ก ์ ์ธ๋ ๋ณ์์ new ์ฐ์ฐ์๊ฐ ๋ฆฌํดํ ๊ฐ์ฒด์ ๋ฒ์ง๋ฅผ ์ ์ฅํ๋ค.
ํด๋์ค ๋ณ์;
๋ณ์ = new ํด๋์ค();
//1๊ฐ์ ์คํ๋ฌธ์ผ๋ก ์์ฑํ ์ ์๋ค.
ํด๋์ค ๋ณ์ = new ํด๋์ค();
ํ๋ก์ ํธ์ ๊ตฌ์ฑ
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 ํ๊ทธ๋ฅผ ์จ์ผํ๋ค.
์ฝ๋) 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";
}