์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
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 |
- 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: ํ์ ๊ด๋ฆฌ ํ์ด์ง ๋ง๋ค๊ธฐ [K-๋์งํธ ํธ๋ ์ด๋ 63์ผ] ๋ณธ๋ฌธ
[63] 230329 Spring: ํ์ ๊ด๋ฆฌ ํ์ด์ง ๋ง๋ค๊ธฐ [K-๋์งํธ ํธ๋ ์ด๋ 63์ผ]
yjyuwisely 2023. 3. 29. 12:46230329 Wed 63rd class
Ch. 18 MVC ํจํด ๊ตฌํ
์ง๋: Ch. 18 MVC ํจํด ๊ตฌํ์ ๊ธฐ๋ฐํ ์์ฒด ์์
(๊ต์ฌ: ์ต๋ฒ๊ท ์ JSP 2.3 ์น ํ๋ก๊ทธ๋๋ฐ: ๊ธฐ์ด๋ถํฐ ์ค๊ธ๊น์ง, ์ ์: ์ต๋ฒ๊ท )
์ฑ
์์ ์ฝ๋: https://github.com/madvirus/jsp23
์ ์ ๋ธ๋ก๊ทธ: https://javacan.tistory.com/
์ค๋ ๋ฐฐ์ด ๊ฒ ์ค ๊ธฐ์ตํ ๊ฒ์ ์ ๋ฆฌํ๋ค.
์์์ ๋ฐฐ์ด ๊ฒ
Presentation | Business | Persistence tier | DB | |||
SpringMVC | <---------------> | Spring Core | <---------------> | MyBatis | <------------> | |
<------------------------------------------------------------------------------------------------------------- | ||||||
MemberController. java |
LoginService. java |
LoginMapper from LoginServiceImpl. java |
||||
๋ฉ์๋ member(); memberPost(); |
---------------> member ๋ฐ์ดํฐ๋ฅผ ๋๊ธด๋ค. |
ls ๋ฉ์๋ memreg(); login(); |
---------------> member ๋ฐ์ดํฐ๋ฅผ ๋๊ธด๋ค. |
lm ๋ฉ์๋ memreg(); login(); |
๋ฐ์ดํฐ์ ํ๋ฆ
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์ ์ ์ฅ๋๋ค.
ํ์ ๊ด๋ฆฌ ํ์ด์ง ๋ง๋ค๊ธฐ
ํ์์ ๊ด๋ฆฌํ๋ ํ๋ฉด (๊ด๋ฆฌ์์ฉ)
http://localhost:8081/list
id | name ์ด๋ฆ |
birthday ์์ผ |
gender ์ฑ๋ณ |
email ์ด๋ฉ์ผ |
phone ํฐ๋ฒํธ |
์ด๋ค ํ๋ฉด์ ๋ง๋ค๊ฑด์ง ์๊ฐํด๋ณด๊ณ , ์ฃผ์๋ฅผ ์๊ฐํด๋ณด๊ณ , ์ปจํธ๋กค๋ฌ๋ฅผ ๋ง๋ค๊ณ , ํด๋๋ช
๋ง๋ค๊ณ ๋ฆฌํดํ๋ค.
ํด๋๋ช
์์ผ๋ฉด JSP ํ์ผ ์ด๋ฆ์ผ๋ก ๋ฆฌํดํ๋ค.
ํ๋ฉด์์ ๋์ง ๊ฒ ์์ผ๋ฉด ๋งค๊ฐ ๋ณ์ ๋น์๋๋ค.
์ฝ๋) MemberController.java ์ผ๋ถ
@Controller
public class MemberController {
@Autowired
LoginService ls; //LoginService ls = new LoginService(); ํด๋์ค๋ฅผ new๋ฅผ ์จ์ ๊ฐ์ฒดํํ ๊ฒ.
// ํ์๊ด๋ฆฌํ์ด์ง
@RequestMapping(value = "/list", method = RequestMethod.GET)
public String mergerlist() { //์ฒ์ ์๋ฌ ๋ฉ์์ง: ๋ฆฌํด์ด ์์ด์ผ ํ๋ค.
return "member/list"; //memberํด๋์ list.jsp ๋ง๋ค์ด์ค๋ค.
}
}
์คํ๋์๋ง์ select ํด์ผ ํ๋ค. ๋ ํ๋ฉด์ผ๋ก ๋๋ ํ์๊ฐ ์๊ณ (ex. ๋ก๊ทธ์ธ, ๋ก๊ทธ์ธ ์๋ฒ, ํ์ ๊ฐ์
, ํ์ ๊ฐ์
์๋ฒ ์ฝ๋)
๋ฐ๋ก DB ์ฐ๊ฒฐํ๋ค.
(์์์ ํ ๋ฒํผ ํด๋ฆญ ํ DB์ฐ๊ฒฐ๊ณผ๋ ๋ค๋ฅด๋ค.)
ํฐ๋ฒํธ๋ int๋ก ํ๋ฉด ์์ 0์ด ์ฌ๋ผ์ง๋ฏ๋ก varchar๋ก ํ๋ค.
ArrayList
๋์ ์ธ ๋ฐฐ์ด์ด๋ค. (๊ธธ์ด๊ฐ ์๋ค๊ฐ๋คํ๋ค.)
์ ์ ๊ฐ๋ง ๋ค์ด๊ฐ ์ ์๋ค.
ArrayList<Integer> score = new ArrayList<>();
score | 10 | 20 | 30 | 40 | 50 | 60 | 70 |
์ ์ ๋ช
์นญ: ์ ๋ค๋ฆญ ํ์
ArrayList<LoginVO> score = new ArrayList<>();
์ ๋ค๋ฆญ(Generic)์ ํด๋์ค ๋ด๋ถ์์ ์ง์ ํ๋ ๊ฒ์ด ์๋ ์ธ๋ถ์์ ์ฌ์ฉ์์ ์ํด ์ง์ ๋๋ ๊ฒ์ ์๋ฏธํ๋ค.
์ฐธ๊ณ : https://st-lab.tistory.com/153
MySQL์ ์๋ ๊ฐ๊ฐ์ ํ์ด ์ ์ฅ๋๋ค.
LoginVO ํ์
์ด ์ ์ฅ๋๋ค.
public class LoginVO { private String id; //aaaa private String password; //1234 private String addr; private String phone; //2023-03-29 private String email; private String name; //์ ์๋ฐ private int age; } //์๋ ๊ฒ์ null๊ฐ์ด ๋ค์ด๊ฐ๋ค. |
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 class LoginVO { private String id; private String password; private String addr; private String phone; private String email; private String name; private int age; } |
score[0] | score[1] (๊ฐ๊ฐ ๋ค๋ฅธ ๊ฐ) | score[2] (๊ฐ๊ฐ ๋ค๋ฅธ ๊ฐ) ... ์ญ ๋๊ฐ score[n] |
๋ฆฌํดํด์ ๊ฐ์ ธ๊ฐ๋ค.
๋ฐ์ดํฐ์ ํ๋ฆ
ํ๋ฆ ์์๋ ์ข์ธก ์๋จ, ๊ทธ ์๋, ์ค๊ฐ, ๊ทธ ์๋, ์ฐ์ธก ์๋จ, ๊ทธ ์๋ ์์์ด๋ค.
์ฒ์์ ์ค๋ฅ๊ฐ ๋ฌ์๋๋ฐ ์๋ ํ์ผ์ LoginVO ํ์
๋ก ์ค์ ์ ์ ํด์ ๊ทธ๋ ๋ค.
LoginService.java
LoginServiceImpl.java
LoginMapper.java
[1] list.jsp: ๋ฐ์ดํฐ
(board/list.jsp : ๊ฒ์ํ ๋ชฉ๋ก์ด๋ฉฐ, member/list.jsp : ํ์ ๋ชฉ๋ก์ผ๋ก ๋ค๋ฅด๋ค.)
์ปจํธ๋กค๋ฌ์ ๋ฐ์ดํฐ๋ฅผ .jsp๋ก ๋ณด๋ผ ๋ ์๋์ ๊ฐ์ ๋ฐฉ์์ผ๋ก ๋ชจ๋ธ์ ์ด๋ค.
์ฝ๋) BoardController.java
// ๊ฒ์ํ ์์ธ ํ์ด์ง
@RequestMapping(value = "/board/detail", method = RequestMethod.GET)
// public String detail(int bno){
public String detail (BoardVO board, Model model) {
System.out.println(board);
// bs.detail(bno);
model.addAttribute("detail", bs.detail(board));
return "board/detail";
}
<%@ 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>
${memlist}
<table border="1">
<tr>
<td>ID</td><td>๋น๋ฐ๋ฒํธ</td><td>์ฃผ์</td><td>ํฐ๋ฒํธ</td><td>์ด๋ฉ์ผ</td><td>์ด๋ฆ</td><td>์์ผ</td>
</tr>
<c:forEach items="${memlist}" var="list">
<tr>
<td>${list.id}</td>
<td>${list.password}</td>
<td>${list.addr}</td>
<td>${list.phone}</td>
<td>${list.email}</td>
<td>${list.name}</td>
<td>${list.age}</td>
</tr>
</c:forEach> <!-- ๋ฐ๋ณตํ๋ค. -->
</table>
</body>
</html>
→ [2] MemberController.java: member -> member
package org.hj.controller;
import javax.servlet.http.HttpSession;
import org.hj.model.LoginVO; //๋ชจ๋ธ(LoginVO)์ด ์์์ ์๋ ค์ค๋ค.
import org.hj.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
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 MemberController {
@Autowired
LoginService ls; //LoginService ls = new LoginService(); ํด๋์ค๋ฅผ new๋ฅผ ์จ์ ๊ฐ์ฒดํํ ๊ฒ.
//LoginService๋ ์ธํฐํ์ด์ค์ด๋ฏ๋ก ์๋ new๋ ์ ๋๋ ๋ฐ ์์ @AutoWired ์ด์ฉํด์ ๊ฐ๋ฅํ๊ฒ ํ๋ค.
// ํ์๊ฐ์
//ํ๋ฉด ์คํ์ ์ํ ๋ฉค๋ฒ (GET๋ฐฉ์)
@RequestMapping(value = "/member", method = RequestMethod.GET) //member ๋ฉ์๋ GET ๋ฐฉ์
public String member () {
return "member/memberin"; //๋ฉค๋ฒ ํด๋์ memberin.jsp๋ฅผ ์คํํ๋ค.
}
// ํ์๊ฐ์
์๋ฒ //์ค์ ๋ก ๊ฐ์
ํ๊ธฐ ์ํ ๋ฉค๋ฒ (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๋ฅผ ์คํํ๋ค. ๊ฒ์ํ ๋ชฉ๋ก์ ๋ณด์ฌ์ค๋ค.
}
// ๋ก๊ทธ์ธ ํ์ด์ง
@RequestMapping(value = "/login", method = RequestMethod.GET)
public String login () {
System.out.println("aaaa");
return "member/login";
}
// ๋ก๊ทธ์ธ ์๋ฒ
@RequestMapping(value = "/login", method = RequestMethod.POST)
public String loginPost (HttpSession session, LoginVO member) { //member: login.jsp์ ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ์จ๋ค.
// HttpSession session = new HttpSession; ์ ()์์ HttpSession session์ ์ง์ฐ๊ณ ๋์ ์ฌ์ฉ ๊ฐ๋ฅํ๋ค.
/* System.out.println("contorller="+member);
System.out.println(ls.login(member));*/
session.setAttribute("login", ls.login(member)); //์ member์ ์์ด๋, ๋น๋ฐ๋ฒํธ๋ฅผ ๊ฐ์ ธ๊ฐ๋ค.
if (ls.login(member)==null) {
return "member/login";
} else {
session.setAttribute("login", ls.login(member)); //null์ด ์๋๋ฉด ์ธ์
์ ์ ์ฅํ๋ค.
return "redirect:/list";
//์๋ ์ฃผ์ ์ ์ํด์ค ๊ฒ ์์ด์ ์ฒ์์ ์๋ฌ๋จ (board์ list.jsp๋ก ๊ฐ๋ ๊ฒ ์๋ ๋ฏ -> member ํด๋์ list.jsp ๋ฐ๋ก ๋ง๋ค์ด์ผํ๋ค.)
//return "/board/list"; //๊ฒ์ํ ๋ฆฌ์คํธ ๋ณด๊ธฐ ์ํด ๋ด๊ฐ ์๋ ์ฝ๋๋ก ๋ฐ๊ฟ.
}
}
// ํ์๊ด๋ฆฌํ์ด์ง
@RequestMapping(value = "/list", method = RequestMethod.GET)
public String mergerlist(Model model) { //์ฒ์ ์๋ฌ ๋ฉ์์ง: ๋ฆฌํด์ด ์์ด์ผ ํ๋ค.
//model.addAttribute(String name, Object value);
//: value ๊ฐ์ฒด๋ฅผ name ์ด๋ฆ์ผ๋ก ์ถ๊ฐํจ. ๋ทฐ ์ฝ๋์์๋ name์ผ๋ก ์ง์ ํ ์ด๋ฆ์ ํตํด์ value๋ฅผ ์ฌ์ฉํจ.
model.addAttribute("memlist", ls.memlist()); //ls.memlist()->memlist:๋ฐ์ดํฐ ์ ์ฅํด์ ๋๊ธด๋ค.
return "member/list"; //memberํด๋์ list.jsp ๋ง๋ค์ด์ค๋ค.
}
// ํ์์ ๋ณด์์ ๋ฐ ์ญ์
}
→ [3] LoginService.java์ member
public void memlist(); //๋น์๋๋ค.
package org.hj.service;
import java.util.ArrayList;
import org.hj.model.LoginVO;
//์ค๊ณํ๋ค.
public interface LoginService { //์ธํฐํ์ด์ค๊ฐ new()๋ฅผ ํ ์ ์์ผ๋ฏ๋ก MemberController.java์ @Autowired๋ฅผ ์ค์
//#####์๋ก ์ถ๊ฐํจ#####
public void memreg(LoginVO member); //์ถ์๋ฉ์๋์ด๋ฏ๋ก LoginServiceImpl.java์์ ๊ตฌํํ๋ค.
public LoginVO login(LoginVO member);
public ArrayList<LoginVO> memlist(); //๋น์๋๋ค.
}
→ [4] LoginServiceImpl.java์ member from public LoginVO login (LoginVO member) -> return lm.login(member)
LoginServiceImpl.java
public void memlist() {
lm.memlist();
}
์์ ์ฝ๋๊ฐ ์๋ MemberController.java์ ls.memlist();๋ก ๊ฐ๋ค.
// ํ์๊ด๋ฆฌํ์ด์ง
@RequestMapping(value = "/list", method = RequestMethod.GET)
public String mergerlist() { //์ฒ์ ์๋ฌ ๋ฉ์์ง: ๋ฆฌํด์ด ์์ด์ผ ํ๋ค.
ls.memlist();
return "member/list"; //memberํด๋์ list.jsp ๋ง๋ค์ด์ค๋ค.
}
package org.hj.service;
import java.util.ArrayList;
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) { //์ถ์๋ฉ์๋ LoginSerive.java๋ฅผ ๊ตฌํํ๋ค.
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);
}
//๋น์ฆ๋์ค๊น์ง ๋๋ฌ๋ค. ์๋น์ค -> DB ์ฐ๊ฒฐํด์ผํ๋ค.
public ArrayList<LoginVO> memlist() {
return lm.memlist();
}
}
→ [5] LoginMapper.java์ member from public LoginVO login(LoginVO member);
package org.hj.mapper;
import java.util.ArrayList;
import org.hj.model.LoginVO;
public interface LoginMapper {
//#####์๋ก ์ถ๊ฐํจ#####
public void memreg(LoginVO member);
public LoginVO login(LoginVO member);
public ArrayList<LoginVO> memlist();
}
→ [6] LoginMapper.xml์ <select>์ #{id}, #{password}๊ฐ ์ผ์นํด์ผํ๋ค.
<?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"> <!-- ๊ตฌํ๋ฉ์๋๊ฐ ์์ผ๋๊น return์ด ์๋ค. -->
<!-- ๋์ select๋ resultType์ด๋ผ๋ ํ์
์ด return ์ญํ ์ด๋ค. -->
select id, password <!-- ๊ฒฐ๊ณผ๋ ํ ๊ฑด์ด๋ค. id๋ ๊ธฐ๋ณธํค์ด๋ค. -->
from member
where id=#{id} and password=#{password}
</select>
<!-- ํ์ ๊ด๋ฆฌ ๋ฆฌ์คํธ ํ์ด์ง -->
<select id="memlist" resultType="org.hj.model.LoginVO">
select * from member <!-- ๊ฒฐ๊ณผ๋ ์ฌ๋ฌ ๊ฑด์ด๋ค. LoginVOํ์
์ ๋ฐฐ์ด์ด ํ์ํ๋ค!-->
</select>
</mapper>
→ ๊ทธ ํ MySql์ DB์ ์ ์ฅ๋๋ค.
๊ฒฐ๊ณผ)