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

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

[62] 230328 Spring: ๋กœ๊ทธ์ธ ํ›„ ๊ฒŒ์‹œํŒ ๋ชฉ๋ก ๋ณด๊ธฐ, ํšŒ์› ๊ฐ€์ž… ํ›„ MySQL DB์— ์ถ”๊ฐ€ํ•˜๊ธฐ [K-๋””์ง€ํ„ธ ํŠธ๋ ˆ์ด๋‹ 62์ผ] ๋ณธ๋ฌธ

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

[62] 230328 Spring: ๋กœ๊ทธ์ธ ํ›„ ๊ฒŒ์‹œํŒ ๋ชฉ๋ก ๋ณด๊ธฐ, ํšŒ์› ๊ฐ€์ž… ํ›„ MySQL DB์— ์ถ”๊ฐ€ํ•˜๊ธฐ [K-๋””์ง€ํ„ธ ํŠธ๋ ˆ์ด๋‹ 62์ผ]

yjyuwisely 2023. 3. 28. 14:49

230328 Tue 62nd 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


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


๋ชฐ๋ž๋˜ ์šฉ์–ด

@Autowired๋ž€, ์Šคํ”„๋ง DI(Dependency Injection)์—์„œ ์‚ฌ์šฉ๋˜๋Š” ์–ด๋…ธํ…Œ์ด์…˜์ž…๋‹ˆ๋‹ค. ์Šคํ”„๋ง์—์„œ ๋นˆ ์ธ์Šคํ„ด์Šค๊ฐ€ ์ƒ์„ฑ๋œ ์ดํ›„ @Autowired๋ฅผ ์„ค์ •ํ•œ ๋ฉ”์„œ๋“œ๊ฐ€ ์ž๋™์œผ๋กœ ํ˜ธ์ถœ๋˜๊ณ , ์ธ์Šคํ„ด์Šค๊ฐ€ ์ž๋™์œผ๋กœ ์ฃผ์ž…๋ฉ๋‹ˆ๋‹ค. 
(์Šคํ”„๋ง ๋นˆ(Bean): ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ์—์„œ ๊ด€๋ฆฌํ•˜๋Š” ์ž๋ฐ” ๊ฐ์ฒด)
์ฆ‰, ํ•ด๋‹น ๋ณ€์ˆ˜ ๋ฐ ๋ฉ”์„œ๋“œ์— ์Šคํ”„๋ง์ด ๊ด€๋ฆฌํ•˜๋Š” Bean์„ ์ž๋™์œผ๋กœ ๋งคํ•‘ํ•ด์ฃผ๋Š” ๊ฐœ๋…์ž…๋‹ˆ๋‹ค. @Autowired๋Š” ๋ณ€์ˆ˜, Setter๋ฉ”์„œ๋“œ, ์ƒ์„ฑ์ž, ์ผ๋ฐ˜ ๋ฉ”์„œ๋“œ์— ์ ์šฉ์ด ๊ฐ€๋Šฅํ•˜๋ฉฐ <property>, <constructor-arg>ํƒœ๊ทธ์™€ ๋™์ผํ•œ ์—ญํ• ์„ ํ•ฉ๋‹ˆ๋‹ค. 

์ฐธ๊ณ : https://life-with-coding.tistory.com/433

์ฐธ๊ณ : https://catsbi.oopy.io/b2de2693-fd8c-46e3-908a-188b3dd961f3


 

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

(์งˆ์˜๋ฌธ = QUERY = ์ฟผ๋ฆฌ = SQL(Structure Query Language)) 
๋‚ด๋ถ€ ๊ณผ์ • ์˜ˆ์‹œ
์‚ฌ์šฉ์ž -> ์งˆ์˜ ( ์ฟผ๋ฆฌ๋ฌธ ) ์—ฌ๊ธฐ์„  select -> DB์„œ๋ฒ„ (select id, password from DB)
(The SELECT statement is used to select data from a database.)
์‚ฌ์šฉ์ž <- ๊ฒฐ๊ณผ <- DB์„œ๋ฒ„
์ฐธ๊ณ : https://e2xist.tistory.com/25


๊ฒŒ์‹œํŒ ๋ชฉ๋ก ๋ณด๊ธฐ

์•„๋ž˜ ์ฝ”๋“œ๋Š” ๊ฐ ์ปจํŠธ๋กค๋Ÿฌ์™€ ๋ทฐ๋ฅผ ๋ณด์—ฌ์ค€๋‹ค.
BoardController.java์™€ list.jsp

์ฝ”๋“œ) BoardController.java

package org.hj.controller;

import java.util.ArrayList;

import javax.servlet.http.HttpSession;

import org.hj.model.AttachFileVO;
import org.hj.model.BoardVO;
import org.hj.model.CriteriaVO;
import org.hj.model.PageVO;
import org.hj.service.BoardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

@Controller
public class BoardController {
	
	// ๋น„์ฆˆ๋‹ˆ์Šค ๋ชจ๋ธ์„ ์ปจํŠธ๋กค๋Ÿฌ์— ์—ฐ๊ฒฐํ•˜๊ธฐ
	@Autowired
	BoardService bs;

	@RequestMapping(value = "/board/list", method = RequestMethod.GET)
	// ๊ฒŒ์‹œํŒ ๋ชฉ๋ก ๋ฆฌ์ŠคํŠธ
	public String list (Model model, CriteriaVO cri) {
		System.out.println(cri);
		// list.jsp ์‹คํ–‰ ํ•  ๋•Œ select ๋œ ๊ฒฐ๊ณผ๋ฅผ ๊ฐ€์ ธ์™€๋ผ
		model.addAttribute("list", bs.list(cri));
		// list.jsp ์‹คํ–‰ ํ•  ๋•Œ PageVO์— ์ €์žฅ๋˜์–ด ์žˆ๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์™€๋ผ.
		//                           ์ƒ์„ฑ์ž ํ˜ธ์ถœ(๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ 2๊ฐœ์ธ ์ƒ์„ฑ์ž)
		// boardํ…Œ์ด๋ธ”(๊ฒŒ์‹œํŒํ…Œ์ด๋ธ”)์— ์ „์ฒด ๊ฑด์ˆ˜(selectํ•ด์„œ)๋ฅผ ์•„๋ž˜์— 190๋Œ€์‹ ์— ๋Œ€์ž…
		int total=bs.total(cri);
		//model.addAttribute("paging", new PageVO(cri, 190));
		model.addAttribute("paging", new PageVO(cri, total));
		return "board/list";
	}
	// ๊ฒŒ์‹œํŒ ์ƒ์„ธ ํŽ˜์ด์ง€
	@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";
	}
	// ๊ฒŒ์‹œํŒ ์ˆ˜์ •
	@RequestMapping(value = "/board/modify", method = RequestMethod.POST)
	public String modify (BoardVO board, RedirectAttributes rttr) {
		bs.modify(board);
		rttr.addAttribute("bno", board.getBno());
		// ์ˆ˜์ •ํ•˜๊ณ  ๋‚œ ๋’ค ํ™•์ธ์„ ์œ„ํ•ด ์ƒ์„ธํŽ˜์ด์ง€๋กœ ํ™”๋ฉด์ด๋™
		return "redirect:/detail";
	}
	
	// ๊ฒŒ์‹œํŒ ์‚ญ์ œ
	@RequestMapping(value = "/board/remove", method = RequestMethod.POST)
	public String remove (BoardVO board) {
		bs.remove(board);
		return "redirect:/list";
	}
	
	// ๊ฒŒ์‹œํŒ ๊ธ€์“ฐ๊ธฐ ํŽ˜์ด์ง€ (ํ™”๋ฉด)
	@RequestMapping(value = "/write", method = RequestMethod.GET)
	public String write () {
		return "board/write";
	}
	// ๊ฒŒ์‹œํŒ ๊ธ€์“ฐ๊ธฐ ํŽ˜์ด์ง€ (insert ์ด๋ฃจ์–ด์ง)
	@RequestMapping(value = "/write", method = RequestMethod.POST)
	public String writePost (BoardVO board) {
		System.out.println(board);
		// ๋น„์ฆˆ๋‹ˆ์Šค ์˜์—ญ ์—ฐ๊ฒฐํ•œ ํ›„ BoardService
		bs.write(board);
		return "redirect:/list";
	}
	// ํ•ด๋‹น๊ฒŒ์‹œ๋ฌผ์˜ ์ฒจ๋ถ€ํŒŒ์ผ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ajax๋กœ ์ „์†ก
	@RequestMapping(value = "/attachlist", method = RequestMethod.GET)
	public ResponseEntity<ArrayList<AttachFileVO>> uploadAjaxPost(int bno) {
		
		return new ResponseEntity<>(bs.attachlist(bno),HttpStatus.OK);
	}

}


์ฝ”๋“œ) ๊ฒŒ์‹œํŒ ๋ชฉ๋ก์„ ๋ณด์—ฌ์ค€๋‹ค.
 list.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>   
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script type="text/javascript" src="/resources/js/list.js"></script>
</head>
<body>

<h1>๊ฒŒ์‹œํŒ ๋ชฉ๋ก์ž…๋‹ˆ๋‹ค</h1>
<input type="button" value="๊ธ€์“ฐ๊ธฐ" onclick="location.href='http://localhost:8080/write'">
<form id="searchForm" action="/board/list" method="get">

	<select name="type">
		<option value="T">์ œ๋ชฉ</option>
		<option value="C">๋‚ด์šฉ</option>
		<option value="TC">์ œ๋ชฉ+๊ฒŒ์‹œ๊ธ€</option>
	</select>
	<input type="text" name="keyword" value="${paging.cri.keyword}">
	<input type="text" name="pageNum" value="${paging.cri.pageNum}">
	<input type="text" name="amount" value="${paging.cri.amount}">
	<input type="button" value="๊ฒ€์ƒ‰">
</form>

<table border="1">
	<tr>
		<td>๊ฒŒ์‹œํŒ๋ฒˆํ˜ธ</td> <td>์ œ๋ชฉ</td> <td>์ž‘์„ฑ์ผ์ž</td> <td>์กฐํšŒ์ˆ˜</td>
	</tr>
	
	<!-- for๋ฌธ ์‹œ์ž‘ -->
	<c:forEach items="${list}" var="boardlist">
		<tr>
			<td>${boardlist.bno}</td>
			<td><a href="/board/detail?bno=${boardlist.bno}">${boardlist.title}</a></td>
			<td>${boardlist.regdate}</td>
			<td>${boardlist.cnt}</td>
		</tr>
	</c:forEach>
	<!-- for๋ฌธ ๋ -->
</table>
<!-- prev(์ด์ „)์ด true์ด๋ฉด ์ด์ „๋ฒ„ํŠผ ํ™œ์„ฑํ™” -->
<c:if test="${paging.prev}">
	<a href="/board/list?type=${paging.cri.type}&keyword=${paging.cri.keyword}&pageNum=${paging.startPage-1}&amount=${paging.cri.amount}">์ด์ „</a>
</c:if>

<!-- begin(1)์ด end(10)๋  ๋™์•ˆ ๋ฐ˜๋ณต(1์ผ 10์ผ ๋  ๋™์•ˆ ๋ฐ˜๋ณต) -->
<c:forEach begin="${paging.startPage}" end="${paging.endPage}" var="num">
 	<a href="/board/list?type=${paging.cri.type}&keyword=${paging.cri.keyword}&pageNum=${num}&amount=${paging.cri.amount}">${num}</a>
</c:forEach>

<!-- next(๋‹ค์Œ)์ด true์ด๋ฉด ๋‹ค์Œ๋ฒ„ํŠผ ํ™œ์„ฑํ™” -->
<c:if test="${paging.next}">
	<a href="/board/list?type=${paging.cri.type}&keyword=${paging.cri.keyword}&pageNum=${paging.endPage+1}&amount=${paging.cri.amount}">๋‹ค์Œ</a>
</c:if>

</body>
</html>

View
NAVER ํ™”๋ฉด
http://naver.com/login?id=aaa&password=1234 

------------------->
(๋ฐ์ดํ„ฐ ๋ณด๋‚ธ๋‹ค.)
Controller
(๋ฐ์ดํ„ฐ ๋ฐ›๋Š”๋‹ค.)
-------------------> Model
String id
String password
http://naver.com 
------------------->
----->
์„œ๋น„์Šค
(ex. BoardService from BoardController.java)
๋กœ์ง ๋ถ€๋ถ„, ๋…ผ๋ฆฌ ํŒ๋‹จ
(ex. id, password๊ฐ€ DB์™€ ์ผ์น˜ํ•˜๋Š”์ง€ ํŒ๋‹จ)
<-----
|
|

|
v
๋„ค์ด๋ฒ„ ๋ฉ”์ธํŽ˜์ด์ง€
<-------------------
  DB
(Mapper)

ํšŒ์›๊ฐ€์ž…์€ INSERTํ•˜๊ณ  DB์—์„œ ๋๋‚œ๋‹ค. 
๋กœ๊ทธ์ธ์ด๋ฉด ํ™”๋ฉด์—์„œ id, password ๋˜์ง€๊ณ  Controller, Model์—์„œ ๋„˜์–ด๊ฐ€์„œ DB์—์„œ SELECTํ•œ๋‹ค.

๋ฉ”์„œ๋“œ์˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ํ†ตํ•ด์„œ ์ €์žฅํ•œ๋‹ค. ์ˆœ์„œ๋Œ€๋กœ View -> Controller -> Model -> DB๋กœ ๊ฐ„๋‹ค.
๋ฉ”์„œ๋“œ์˜ ๋ฆฌํ„ด ์—ญํ• ์€ ๋ฐ˜๋Œ€๋กœ DB -> Model -> Controller -> View ์ˆœ์œผ๋กœ ๊ฐ€๋Š” ๊ฒƒ์ด๋ฉฐ, ์‚ฌ์šฉ์ž๊ฐ€ ์•Œ ์ˆ˜ ์žˆ๋‹ค. (ex. ~๋‹˜ ํ™˜์˜ํ•ฉ๋‹ˆ๋‹ค. ๊ฐ™์€ ํ™”๋ฉด)

์„œ๋น„์Šค ์˜ˆ์‹œ์˜ BoardService from BoardController.java

	// ๋น„์ฆˆ๋‹ˆ์Šค ๋ชจ๋ธ์„ ์ปจํŠธ๋กค๋Ÿฌ์— ์—ฐ๊ฒฐํ•˜๊ธฐ
	@Autowired
	BoardService bs;

3-tier ์„œ๋น„์Šค์ฐธ๊ณ 

์ค‘๊ฐ„์— ์„œ๋น„์Šค(๋กœ์ง ๋ถ€๋ถ„)๋ฅผ ๋„ฃ๋Š”๋‹ค.
์„œ๋น„์Šค๋ฅผ ๋นผ๋„ ํ™”๋ฉด์€ ๋Œ์•„๊ฐ„๋‹ค. ์œ ์ง€๋ณด์ˆ˜ ์ฐจ์›์—์„œ ์ฒ˜๋ฆฌํ•œ๋‹ค. 
์œ ์ง€๋ณด์ˆ˜ํ•  ๋•Œ ์„œ๋น„์Šค๋งŒ ๋ณด๋ฉด ๋œ๋‹ค. 

BoardController.java๊ฐ€ BoardServiceImpl.java๋ž‘ ์—ฐ๊ฒฐํ•˜๊ณ 
BoardServiceImpl.java๋Š” DB์™€ ์—ฐ๊ฒฐ๋œ๋‹ค.

BoardServiceImpl.java๋Š” DB์™€ ์—ฐ๊ฒฐ๋œ๋‹ค.

BoardService.java (์ธํ„ฐํŽ˜์ด์Šค: ์„ค๊ณ„๋ฅผ ํ•œ๋‹ค. ์ถ”์ƒ ๋ฉ”์„œ๋“œ๋ฅผ ๋งŒ๋“ ๋‹ค. ;๋กœ ๋๋‚ฌ๋‹ค.)
-> BoardServiceImpl.java (๊ตฌํ˜„: ์ƒ์†์„ ๋ฐ›์•„์„œ ๊ตฌํ˜„ํ•œ๋‹ค. {}๋กœ ๋‹ซ๋Š”๋‹ค. )

์ฝ”๋“œ) BoardService.java

package org.hj.service;

import java.util.ArrayList;

import org.hj.model.AttachFileVO;
import org.hj.model.BoardVO;
import org.hj.model.CriteriaVO;

public interface BoardService { //์ธํ„ฐํŽ˜์ด์Šค: ์„ค๊ณ„๋ฅผ ํ•œ๋‹ค. ์ถ”์ƒ ๋ฉ”์„œ๋“œ๋ฅผ ๋งŒ๋“ ๋‹ค. ;๋กœ ๋๋‚ฌ๋‹ค.

	// ๊ธ€์“ฐ๊ธฐ ์„ค๊ณ„          (BoardVO : ๊ฒŒ์‹œํŒ์ •๋ณด+ํŒŒ์ผ์—…๋กœ๋“œ์ •๋ณด)
	public void write(BoardVO board);
	// ๊ธ€ ๋ชฉ๋ก ๋ฆฌ์ŠคํŠธ ์„ค๊ณ„
	public ArrayList<BoardVO> list(CriteriaVO cri);
	// ๊ธ€ ์ƒ์„ธ ๋‚ด์šฉ ๋ณด๊ธฐ ์„ค๊ณ„
	public BoardVO detail(BoardVO board);
	// ๊ธ€ ์ˆ˜์ • ์„ค๊ณ„
	public void modify(BoardVO board);
	// ๊ธ€ ์‚ญ์ œ ์„ค๊ณ„
	public void remove(BoardVO board);
	// boardํ…Œ์ด๋ธ” ์ „์ฒด๊ฑด์ˆ˜ ์„ค๊ณ„
	public int total(CriteriaVO cri);
	// ์ฒจ๋ถ€ํŒŒ์ผ ์กฐํšŒ ์„ค๊ณ„
	public ArrayList<AttachFileVO> attachlist (int bno);
}



์ฝ”๋“œ) BoardServiceImpl.java

package org.hj.service;

import java.util.ArrayList;

import org.hj.mapper.BoardAttachMapper;
import org.hj.mapper.BoardMapper;
import org.hj.model.AttachFileVO;
import org.hj.model.BoardVO;
import org.hj.model.CriteriaVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

	@Service
	public class BoardServiceImpl implements BoardService { //๊ตฌํ˜„: ์ƒ์†์„ ๋ฐ›์•„์„œ ๊ตฌํ˜„ํ•œ๋‹ค. {}๋กœ ๋‹ซ๋Š”๋‹ค. 
		
		@Autowired
		BoardMapper bm;	// board ํ…Œ์ด๋ธ” mapper //DB(๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค)๋ถ€๋ถ„์ด๋‹ค. ์„œ๋น„์Šค ์•ˆ์— DB๋ฅผ ํฌํ•จ์‹œ์ผœ ๋†“์•˜๋‹ค.
		@Autowired
		BoardAttachMapper bam;// attachํ…Œ์ด๋ธ” mapper
		
		// BoardService ์—์„œ ์„ค๊ณ„๋˜์–ด์ง„ write ์ถ”์ƒ๋ฉ”์„œ๋“œ๋ฅผ ๊ตฌํ˜„
		public void write(BoardVO board) {
			// BoardMapper์— ์žˆ๋Š” write๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœ
			// ๋ฉ”์„œ๋“œ์˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ํ†ตํ•ด BoardVO ๊ฐ’์„
			// BoardMapper์˜ write ๋ฉ”์„œ๋“œ๋กœ ์ „๋‹ฌ
			bm.write(board);
			
			board.getAttach().forEach(attach->{
				System.out.println("service="+attach);
			// AttachFileVO์˜ bno์— BoardVO์˜ bno๋ฅผ ์ €์žฅ 
				attach.setBno(board.getBno());
				bam.insert(attach);				
			});
		}
		
	
		// BoardService์—์„œ ์„ค๊ณ„๋˜์–ด์ง„ list์ถ”์ƒ๋ฉ”์„œ๋“œ๋ฅผ ๊ตฌํ˜„
		public ArrayList<BoardVO> list(CriteriaVO cri) {
			return bm.list(cri);
		}
	
		@Transactional
		public BoardVO detail(BoardVO board) {
			// ์ƒ์„ธํŽ˜์ด์ง€ ์กฐํšŒ ํ•  ๋•Œ
			// ์กฐํšŒ์ˆ˜ + 1 update
			bm.cntup(board);
			return bm.detail(board);
		}
		// BoardService์—์„œ ์„ค๊ณ„๋˜์–ด์ง„ modify ์ถ”์ƒ๋ฉ”์„œ๋“œ๋ฅผ ๊ตฌํ˜„
		public void modify(BoardVO board) {
			bm.modify(board);
		}
		// BoardService์—์„œ ์„ค๊ณ„๋˜์–ด์ง„ remove ์ถ”์ƒ๋ฉ”์„œ๋“œ๋ฅผ ๊ตฌํ˜„
		public void remove(BoardVO board) {
			bm.remove(board);
		}
		public int total(CriteriaVO cri) {
			return bm.total(cri);
		}
		// ์ฒจ๋ถ€ํŒŒ์ผ ์กฐํšŒ ๊ตฌํ˜„
		public ArrayList<AttachFileVO> attachlist (int bno){
			return bam.attachlist(bno);
		}
}



BoardMapper.xml
์— SQL์ด ๋“ค์–ด๊ฐ€์žˆ๋‹ค.


์ฝ”๋“œ) BoardMapper.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.BoardMapper">
   
  		<!-- ์ œ๋ชฉ, ๋‚ด์šฉ์„ boardํ…Œ์ด๋ธ” insert -->
  		<insert id="write">
  			<selectKey keyProperty="bno" order="BEFORE" resultType="int">
  				select max(bno)+1 bno
				from board
  			</selectKey>

  			insert into board(bno,title, content)
			values(#{bno},#{title}, #{content})
  		</insert>

  		
  		<select id="list" resultType="org.hj.model.BoardVO">
  			
  			select *
			from(
				select @rownum:=@rownum+1 rownum, b.*
				from board b, (select @rownum:=0) as tmp
				<if test="keyword != null">
					<choose>
						<when test="type=='T'.toString()">
							 where title like concat('%',#{keyword},'%')
						</when>
						<when test="type=='C'.toString()">
							where content like concat('%',#{keyword},'%')
						</when>
						<otherwise>
							where title like concat('%',#{keyword},'%')
					      OR  content like concat('%',#{keyword},'%')
						</otherwise>
					
					</choose>

			    </if>
			    order by bno desc
			) as boardlist
			<![CDATA[
			where rownum > (#{pageNum}-1)*#{amount} and rownum <= #{pageNum}*#{amount}
			]]>
  		</select>
  		
  		<select id="detail" resultType="org.hj.model.BoardVO">
  			select * from board
  			where bno=#{bno}
  		</select>
  		<update id="cntup">
  			update board
			set cnt = cnt + 1
			where bno = #{bno}
  		</update>
  		
  		
  		<update id="modify">
  			update board
  			set title=#{title}, content=#{content}
  			where bno=#{bno}
  		</update>
  		
  		<delete id="remove">
  			delete from board
  			where bno=#{bno}
  		</delete>
  		<select id="total" resultType="int">
  			select count(*) from board
  			<if test="keyword != null">
						
			<choose>
				<when test="type=='T'.toString()">
					 where title like concat('%',#{keyword},'%')
				</when>
				<when test="type=='C'.toString()">
					where content like concat('%',#{keyword},'%')
				</when>
				<otherwise>
					where title like concat('%',#{keyword},'%')
			      OR  content like concat('%',#{keyword},'%')
				</otherwise>
			
			</choose>
			
			
			</if>
  		</select>
  		
  </mapper>

์‚ฌ์šฉ์ž๊ฐ€ ๋ณด๋Š” ๊ฒƒ)

http://www.naver.com ----------> Controller ----------> Naver ๋ฉ”์ธํŽ˜์ด์ง€

 

ํšŒ์›๊ฐ€์ž… ์˜ˆ์‹œ)

ํšŒ์›๊ฐ€์ž…์€ insertํ•˜๊ณ  DB์—์„œ ๋๋‚œ๋‹ค. 
์‚ฌ์šฉ์ž -> ์งˆ์˜ ( ์ฟผ๋ฆฌ๋ฌธ ) ์—ฌ๊ธฐ์„  insert -> DB์„œ๋ฒ„
(์‚ฌ์šฉ์ž๊ฐ€ insert๋ฅผ DB์„œ๋ฒ„๋กœ ๋ณด๋‚ธ๋‹ค.)
์ฐธ๊ณ : https://e2xist.tistory.com/25

Naver ํšŒ์›๊ฐ€์ž…
(http://www.naver.com)
id [abcd]
pw [1234]
name [์ •์ž๋ฐ”]

ํšŒ์›๊ฐ€์ž…
(form ํƒœ๊ทธ ์“ด๋‹ค. action์œผ๋กœ Controller๋กœ ๊ฐ„๋‹ค.)
[Model]

Member
String id [abcd]
String password [1234]
String name [์ •์ž๋ฐ”]


Member
Controller
[Model]

Member
String id [abcd]
String password [1234]
String name [์ •์ž๋ฐ”]

DB(Mapper)
^
|
insert  |           
|
----------> ----------> Service

์˜ˆ์‹œ ์ฝ”๋“œ) LoginMapper.xml ์ผ๋ถ€

	<insert id="memreg">
  		insert into member (id, password, name, birthday, gender)
  		values(#{id},#{password},#{name},sysdate(),"f");
	</insert>

๋กœ๊ทธ์ธ ์˜ˆ์‹œ) 

๋กœ๊ทธ์ธ์€ ํ™”๋ฉด์—์„œ id, password ๋˜์ง€๊ณ  Controller, Model์—์„œ ๋„˜์–ด๊ฐ€์„œ DB์—์„œ selectํ•œ๋‹ค. ๊ทธ ๊ฒฐ๊ณผ๋ฅผ ๊ฐ€์ง€๊ณ  ๋กœ๊ทธ์ธ ํ•ด๋„ ๋œ๋‹ค, ์•ˆ ๋œ๋‹ค๋ฅผ ํŒ๋‹จํ•œ๋‹ค. 
(์งˆ์˜๋ฌธ = QUERY = ์ฟผ๋ฆฌ = SQL(Structure Query Language)) 
๋‚ด๋ถ€ ๊ณผ์ • ์˜ˆ์‹œ
์‚ฌ์šฉ์ž -> ์งˆ์˜ ( ์ฟผ๋ฆฌ๋ฌธ ) ์—ฌ๊ธฐ์„  select -> DB์„œ๋ฒ„ (select id, password from DB)
(The SELECT statement is used to select data from a database.)
(์‚ฌ์šฉ์ž๊ฐ€ select๋ฅผ DB์„œ๋ฒ„๋กœ ๋ณด๋‚ธ๋‹ค.)

์‚ฌ์šฉ์ž <- ๊ฒฐ๊ณผ <- DB์„œ๋ฒ„
์ฐธ๊ณ : https://e2xist.tistory.com/25

selectํ•œ ๊ฒƒ์„ ํ™”๋ฉด์œผ๋กœ ๋ฟŒ๋ ค์ฃผ๊ธฐ ์œ„ํ•ด ๋‹ค์‹œ ๋ณด๋‚ด๋‹ˆ๊นŒ ํ™”์‚ดํ‘œ๋Š” ์–‘๋ฐฉํ–ฅ์ด๋‹ค.
select๋œ ๊ฒฐ๊ณผ๋ฅผ ํ™”๋ฉด๊นŒ์ง€ ๊ฐ€์ ธ๊ฐ„๋‹ค.(ํ™”์‚ดํ‘œ ๋ฐฉํ–ฅ ๋‹ค์‹œ ๋˜๋Œ์•„๊ฐ)

Naver ๋กœ๊ทธ์ธ
(http://www.naver.com)
id [abcd]
pw [1234]
name [์ •์ž๋ฐ”]

๋กœ๊ทธ์ธ


[Model]

Member
String id [abcd]
String password [1234]

Member
Controller
[Model]

Member
String id [abcd]
String password [1234]

DB(Mapper)
 ^  
 | |
   select | |            
 | |
   v
---------->
<----------
---------->
<----------
Service
^
                   | Model ๊ฐ์ฒด
--
[Model]

Member
String id [abcd]
String password [1234]
[Model]

Member
String id [abcd]
String password [1234]

์˜ˆ์‹œ ์ฝ”๋“œ) LoginMapper.xml ์ผ๋ถ€ (์กฐ๊ฑด ๊ฒ€์‚ฌ)

	<select id="login" resultType="org.hj.model.LoginVO">
  		select id, password
  		from member
  		where id=#{id} and password=#{password}
	</select>

์ฐธ๊ณ : https://beautify-log.tistory.com/57

์ฐธ๊ณ : https://jione-e.tistory.com/122


Model ๊ฐ์ฒด๋Š” ์•„๋ž˜ ์ฝ”๋“œ์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.
์ฝ”๋“œ) 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 ๋ฉ”์„œ๋“œ, (๋งค๊ฐœ๋ณ€์ˆ˜ํƒ€์ž…,๋งค๊ฐœ๋ณ€์ˆ˜๋ช…)
	public String home(Locale locale, Model model) { //์ง€๊ธˆ์€ ์“ฐ์ž„์ด ์—†๋‹ค. (๊ทธ๋ƒฅ ์žˆ๋Š” ๊ฒƒ.)
		return "home"; //home์ด๋ผ๋Š” ๋ฉ”์„œ๋“œ๋ฅผ ์‹คํ–‰ํ•œ๋‹ค. //๋ฉ”๋ชจ์žฅ์˜ URL์ฃผ์†Œ๊ฐ€ ํ˜ธ์ถœํ•œ ๊ฒƒ์ด๋‹ค. 
						// /WEB-INF/views/home.jsp
	}
	@RequestMapping(value = "/drop", method = RequestMethod.GET)
	// ์ ‘๊ทผ์ œ์–ด์ž  ๋ฐ˜ํ™˜ํƒ€์ž…  ๋ฉ”์„œ๋“œ๋ช…(๋งค๊ฐœ๋ณ€์ˆ˜, ๋งค๊ฐœ๋ณ€์ˆ˜)
		public String drop() {
			return "drop";
		}
}

ํšŒ์› ๊ฐ€์ž… ํ›„ id, password, name์„ MySQL DB์— ์ถ”๊ฐ€ํ•˜๊ธฐ

์•„๋ž˜๋Š” MySQL์— ๋ฐ์ดํ„ฐ๊ฐ€ ์ถ”๊ฐ€๋˜๋„๋ก ์ˆ˜์ •๋œ ์ฝ”๋“œ์ด๋‹ค.
์ฃผ์„์— ํ‘œ์‹œ๋ฅผ ํ•ด๋’€๋‹ค.

์ฝ”๋“œ) MemberController.java

ํ™”๋ฉด ์‹คํ–‰, ์„œ๋ฒ„๋Š” ๊ฐ Get, Post ๋ฐฉ์‹์ธ๋ฐ ๋‘˜ ๋‹ค Get์ด๋ฉด ์ถฉ๋Œ์ด ์ผ์–ด๋‚˜์„œ ๊ทธ๋ ‡๋‹ค.

	@Autowired
	LoginService ls; //LoginService ls = new LoginService(); ํด๋ž˜์Šค๋ฅผ new๋ฅผ ์จ์„œ ๊ฐ์ฒดํ™”ํ•œ ๊ฒƒ

 

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";
	}
	
	// ํšŒ์›๊ฐ€์ž…์„œ๋ฒ„ //์‹ค์ œ๋กœ ๊ฐ€์ž…ํ•˜๊ธฐ ์œ„ํ•œ ๋ฉค๋ฒ„ (POST๋ฐฉ์‹)
	@RequestMapping(value = "/member", method = RequestMethod.POST) //๋‘˜ ๋‹ค GET์ด๋ฉด ์ถฉ๋Œ์ด ์ผ์–ด๋‚œ๋‹ค. ๋ฉ”์„œ๋“œ ๋ฐฉ์‹์ด POST์ด๋‹ค.
	//			int	a //member๋Š” ์ฐธ์กฐ ๋ณ€์ˆ˜
	public String memberPost (LoginVO member) {
		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) {
		
/*		System.out.println("contorller="+member);
		System.out.println(ls.login(member));*/
		
		session.setAttribute("login", ls.login(member));
		
		if (ls.login(member)==null) {
			return "member/login";
			} else {	
				return "redirect:/list";	
			}
		
		
		}
	
	// ํšŒ์›๊ด€๋ฆฌํŽ˜์ด์ง€
	// ํšŒ์›์ •๋ณด์ˆ˜์ • ๋ฐ ์‚ญ์ œ
	
}


์ฝ”๋“œ) LoginServiceImpl.java

	@Autowired
	LoginMapper lm;
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);
	}
}


์ฝ”๋“œ) LoginMapper.java

package org.hj.mapper;

import org.hj.model.LoginVO;

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


์ฝ”๋“œ) LoginService.java

package org.hj.service;

import org.hj.model.LoginVO;

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


์ฝ”๋“œ) 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">
  		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>

๊ฒฐ๊ณผ) ํšŒ์›๊ฐ€์ž…์„ ํ•˜๋ฉด ๋ฐ”๋กœ ๊ทธ ๋‹ค์Œ์— ๊ฒŒ์‹œํŒ ๋ชฉ๋ก์ด ๋‚˜์˜จ๋‹ค.

ํšŒ์› ๊ฐ€์ž… ํ™”๋ฉด
๊ฒŒ์‹œํŒ ๋ชฉ๋ก ํ™”๋ฉด

MySQL DB(member)์— 2023-03-28 ๋‚ ์งœ๋กœ ์˜ฌ๋ผ๊ฐ„ ๊ฒŒ ๋ณด์ธ๋‹ค.

SQL์˜ member ํ…Œ์ด๋ธ”

์ฝ”๋“œ) login.jsp
์œ„์น˜ (SpringEx\src\main\webapp\WEB-INF\views\member\login.jsp)

<%@ 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>
</head>
<body>

<h1>๋กœ๊ทธ์ธ</h1>

<form action="/login" method="post">
์•„์ด๋”” <input type="text" name="id">
๋น„๋ฐ€๋ฒˆํ˜ธ <input type="text" name="password"><br>

<input type="submit" value="๋กœ๊ทธ์ธ"> <!-- ์•„์ง ์„ค์ •์„ ์•ˆ ํ•ด์„œ ์•ˆ ๋œฌ๋‹ค. -->
<input type="button" value="ํšŒ์›๊ฐ€์ž…" onclick="location.href='http://localhost:8080/member'">
</form>

</body>
</html>

๊ฒฐ๊ณผ)

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

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

๋กœ๊ทธ์ธ ํ–ˆ์„ ๋•Œ์˜ ์ฝ˜์†”์—์„œ ๋‚˜์˜ค๋Š” ์˜ค๋ฅ˜ ๋ฉ”์‹œ์ง€๋“ค

์ฝ˜์†”์—์„œ ์˜ค๋ฅ˜๋ฅผ ๋ณผ ์ˆ˜ ์žˆ๋Š”๋ฐ, ์•„์ด๋””, ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅ ์•ˆ ํ•˜๋ฉด ์ œ์ผ ์•„๋ž˜์˜ ์˜ค๋ฅ˜ ๋ฉ”์‹œ์ง€๋“ค์ด ๋‚˜์˜จ๋‹ค.

service=LoginVO [id=, password=, addr=null, phone=null, email=null, name=null, age=0]
service return=null


์ง์ ‘ ์•„์ด๋””, ๋น„๋ฐ€๋ฒˆํ˜ธ(id=hi0328, password=1234)๋ฅผ ์ž…๋ ฅํ•œ ๊ฒฝ์šฐ, ์œ„์˜ ์˜ค๋ฅ˜ ๋ฉ”์‹œ์ง€๋“ค์ด ๋‚˜์˜จ๋‹ค.

service=LoginVO [id=hi0328, password=1234, addr=null, phone=null, email=null, name=null, age=0]
service return=LoginVO [id=hi0328, password=1234, addr=null, phone=null, email=null, name=null, age=0]
service=LoginVO [id=hi0328, password=1234, addr=null, phone=null, email=null, name=null, age=0]
service return=LoginVO [id=hi0328, password=1234, addr=null, phone=null, email=null, name=null, age=0]
WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/list] in DispatcherServlet with name 'appServlet'
aaaa

์ง์ ‘ id, password ์ž…๋ ฅํ–ˆ์„ ๋•Œ์˜ ์˜ค๋ฅ˜ ํŽ˜์ด์ง€


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