์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
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๐ณ๐
[62] 230328 Spring: ๋ก๊ทธ์ธ ํ ๊ฒ์ํ ๋ชฉ๋ก ๋ณด๊ธฐ, ํ์ ๊ฐ์ ํ MySQL DB์ ์ถ๊ฐํ๊ธฐ [K-๋์งํธ ํธ๋ ์ด๋ 62์ผ] ๋ณธ๋ฌธ
[62] 230328 Spring: ๋ก๊ทธ์ธ ํ ๊ฒ์ํ ๋ชฉ๋ก ๋ณด๊ธฐ, ํ์ ๊ฐ์ ํ MySQL DB์ ์ถ๊ฐํ๊ธฐ [K-๋์งํธ ํธ๋ ์ด๋ 62์ผ]
yjyuwisely 2023. 3. 28. 14:49230328 Tue 62nd class
Ch. 18 MVC ํจํด ๊ตฌํ
์ง๋: Ch. 18 MVC ํจํด ๊ตฌํ์ ๊ธฐ๋ฐํ ์์ฒด ์์
(๊ต์ฌ: ์ต๋ฒ๊ท ์ JSP 2.3 ์น ํ๋ก๊ทธ๋๋ฐ: ๊ธฐ์ด๋ถํฐ ์ค๊ธ๊น์ง, ์ ์: ์ต๋ฒ๊ท )
์ฑ
์์ ์ฝ๋: https://github.com/madvirus/jsp23
์ ์ ๋ธ๋ก๊ทธ: https://javacan.tistory.com/
์ค๋ ๋ฐฐ์ด ๊ฒ ์ค ๊ธฐ์ตํ ๊ฒ์ ์ ๋ฆฌํ๋ค.
๋ชฐ๋๋ ์ฉ์ด
@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์ ์ฐ๊ฒฐ๋๋ค.
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 ๋ ์ง๋ก ์ฌ๋ผ๊ฐ ๊ฒ ๋ณด์ธ๋ค.
์ฝ๋) 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