๐ TODO : Ajax๋ฅผ ์ฌ์ฉํ , ๋๊ธ ๋ฑ๋ก , ๋๊ธ ์์ , ์ญ์
๐ project : ์ธํ ๋ฆฌ์ ์ด - SpringBootMybatisMini
form : content.jsp
dto : MbAnswerDto
controller : MbanswerController.java
Mapper : mbanswersql.xml
service : MbanswerMapperInter.java
์คํ๋ง๋ถํธ ๋์๊ด์ ๊ฐ๋จ
- ํด๋ผ์ด์ธํธ๊ฐ JSP ํ์ด์ง์ ๋ฐ์ดํฐ๋ฅผ ์ ๋ ฅํ๊ณ ์ ์ถํฉ๋๋ค.
- JSP ํ์ด์ง์์ ์ปจํธ๋กค๋ฌ์๊ฒ ์์ฒญ์ ๋ณด๋ ๋๋ค.
- ์ปจํธ๋กค๋ฌ๋ ์์ฒญ์ ๋ฐ๊ณ ํ์ํ ์์ ์ ์ํํฉ๋๋ค.
- ์ปจํธ๋กค๋ฌ๋ ์๋น์ค๋ก ๋ฐ์ดํฐ ์ฒ๋ฆฌ๋ฅผ ์์ํฉ๋๋ค.
- ์๋น์ค๋ ํ์ํ ๋น์ฆ๋์ค ๋ก์ง์ ์คํํ๊ณ ๋ฐ์ดํฐ๋ฒ ์ด์ค์์ ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ์ต๋๋ค.
- ์๋น์ค๊ฐ ๋ฐ์ดํฐ ์ฒ๋ฆฌ๋ฅผ ์๋ฃํ๋ฉด, ๊ฒฐ๊ณผ๋ฅผ ์ปจํธ๋กค๋ฌ๋ก ๋ฐํํฉ๋๋ค.
- ์ปจํธ๋กค๋ฌ๋ JSP ํ์ด์ง๋ก ๋ฐ์ดํฐ๋ฅผ ์ ๋ฌํฉ๋๋ค.
- JSP ํ์ด์ง๋ ๋ฐ์ดํฐ๋ฅผ ์ด์ฉํ์ฌ ๋์ ์น ํ์ด์ง๋ฅผ ์์ฑํ๊ณ ํด๋ผ์ด์ธํธ์๊ฒ ๋ฐํํฉ๋๋ค.
๊ฒฐ๋ก : ๋ฐ์ดํฐ ์ฒ๋ฆฌ ํ ๋ฐํ๋๋ ๊ณณ์ ์ปจํธ๋กค๋ฌ์ด๋ค.
์คํ๋ง๋ถํธ๊ฐ ๋์ํ๋ ๊ณผ์ ๋๋ก ์ค๋ช ์ ํด๋ณด๊ฒ ์ต๋๋ค.
form
<div style="margin : 50px 150px;">
<table class="table table-bordered" style="width:800px;">
<tr>
<td>
<h4><b>${dto.subject}</b>
<span style="font-size:0.7em; color:gray; float: right">
๐ ์กฐํ์ : ${dto.readcount}
๐ฐ ์์ฑ์ผ : <fmt:formatDate value="${dto.writeday}" pattern="yyyy-MM-dd HH:mm:ss"/>
</span>
</h4>
<span><b id="myid" myid="${sessionScope.myid}">์์ฑ์ : ${dto.name} (${dto.myid})</b></span>
<c:if test="${dto.uploadfile!= 'no'}">
<span style="float:right">
<a href="download?clip=${dto.uploadfile}">
<i class="bi bi-arrow-down-circle"> </i><b>${dto.uploadfile}</b>
</a>
</span>
</c:if>
</td>
</tr>
<tr>
<td>
<c:if test="${bupload ==true}">
<img src="../savefile/${dto.uploadfile}" style="width:350px; height:400px;">
</c:if>
<br><br>
<pre>
${dto.content}
</pre>
<br>
<b>์กฐํ ์ : ${dto.readcount}</b>
<b>๋๊ธ ์ : <span class="acount"></span></b>
</td>
</tr>
<%--๋๊ธ--%>
<tr>
<td>
<input type="hidden" id="num" value="${dto.num}">
<div class="alist"></div>
<c:if test="${sessionScope.loginok != null}">
<div class="aform">
<div>
<input type="text" class="form-control" style="width:500px; display: inline-flex" placeholder="๋๊ธ ์
๋ ฅ" id="content" >
<button type="button" class="btn btn-outline-danger" id="btnansweradd" num="${dto.num}">๋ฑ๋ก</button>
</div>
</div>
</c:if>
</td>
</tr>
<c:if test="${sessionScope.loginok!=null}">
<tr>
<td>
<button type="button" class="btn btn-outline-dark" onclick="location.href='form?currnePage=${currentPage}'" style="width:100px;">๊ธ์์ฑ</button>
<button type="button" class="btn btn-outline-dark" onclick="location.href='list?currnePage=${currentPage}'" style="width:100px;">๋ชฉ๋ก</button>
<c:if test="${sessionScope.loginok!=null and sessionScope.myid ==dto.myid}">
<button type="button" class="btn btn-outline-dark" onclick="location.href='update?num=${dto.num}¤tPage=${currentPage}'" style="width:100px;">์์ </button>
<button type="button" class="btn btn-outline-dark" onclick="location.href='delete?num=${dto.num}¤tPage=${currentPage}'" style="width:100px;">์ญ์ </button>
</c:if>
</td>
</tr>
</c:if>
</table>
</div>
</body>
- ์ด form์์๋ client๊ฐ ์์ฒญํ ๋ฐ์ดํฐ๋ฅผ list๋ก ๋์ด ๊ฒ์ด๊ณ , ๊ทธ list์์ ๋๊ธ์ ๋ฌ๊ธฐ ์ํ form์
๋๋ค.
- form์์ <div.alist> ์ฌ๊ธฐ div์ ๋๊ธ์ ๋์ธ ์์ ์ ๋๋ค. ์ด div์ ๋๊ธ์ ๋์ฐ๊ธฐ ์ํด์ ajax๋น๋๊ธฐ๋ฐฉ์์ผ๋ก ์ฒ๋ฆฌ๋ฅผ ํฉ๋๋ค.
๋ค์์ผ๋ก๋ Ajax๋ฅผ ์ฒ๋ฆฌํ๋ ค๋ฉด Controller , service ,mapper์์ ๋ฏธ๋ฆฌ ๋ฉ์๋๋ฅผ ๋ง๋ค๊ณ sql์์ฑ์ ๋๋ด๋์ผ์ง ๋ฐ๋ก๋ฐ๋ก ajax์ฒ๋ฆฌ๋ฅผ ํ ์ ์์ผ๋ ๋ฏธ๋ฆฌ ๋ง๋ค์ด ๋ณด๊ฒ ์ต๋๋ค
#1) dto
import java.sql.Timestamp;
import org.apache.ibatis.type.Alias;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
@Data
@Alias("mbdto")
public class MbAnswerDto {
private String idx;
private String num;
private String name;
private String myid;
private String content;
@JsonFormat(pattern = "yyyyMMdd")
private Timestamp writeday;
}
- DB์ ์์ฑํด๋ column ๋ค์ด๋ ์ด๋ฆ์ ๊ฐ๊ฒ ์์ฑ์ ํด์ค์ผ ํฉ๋๋ค (datatype์ ๋ฌ๋ผ๋ ์๊ด์์ต๋๋ค)
- ์ฌ๊ธฐ์ ์ค์ํ๊ฑด idx๋ -> primary key์ด๊ณ
- num์ Mbanswer๋ง๊ณ ๊ทธ์ ์ ์์ฑํ memboard num์ด๋ foreign key๋ฅผ ์ฃผ์ด ์ฐธ์กฐ๋ฅผ ์ํจ ๊ฒ ์ ๋๋ค.
- num์ ๋๊ธ์ ๋ฌ๊ธฐ ์ , ์์ฑ ๋ ๊ธ์ ๋ฒํธ๋ฅผ ์๋ฏธํ๋ ๊ฒ์
๋๋ค
- ์ฆ ์์ฑ๋ ๊ธ์ num์ผ๋ก ๊ตฌ๋ณ์ด๊ณ , idx๋ ๊ทธ ์์ฑ๋ ๊ธnum ์ ๋๊ธ์ ๋ค๋ ๊ฒ์ด๊ธฐ ๋๋ฌธ์ ์ฐจ๋ณํ๋ฅผ ๋ฌ์ผํจ.
#2) service ์์ฑ
-์ค์ํ๊ฑด service๋ฅผ ์ง์ ์ ์ผ๋ก ์์ฑํ์ง๋ ์์์ง๋ง, ์๋น์ค ์ญํ ์ ํ ์ ์๋ ํด๋์ค ์ ๋๋ค
- ์ ์ผ๊น?? ๋ผ๊ณ ์๊ฐํด๋ดค๋๋ฐ ๊ทธ๊ฒ์ ๋ํ ๋ต๋ณ์ ๋ค์ ๊ณต๋ถํ๊ณ ์ ๋ก๋ ํ๊ฒ ์ต๋๋ค.
package boot.data.mapper;
import java.util.List;
import org.apache.ibatis.annotations.Mapper;
import boot.data.dto.MbAnswerDto;
@Mapper
public interface MbanswerMapperInter {
public void insertMbanswer(MbAnswerDto dto);
public List<MbAnswerDto> getAllAnswers(String num); //num ๊ฐ์ ๋ฐ๋ฅธ ์ ๋ณด ๋๊ธฐ๊ธฐ
public MbAnswerDto getAnswer(String idx); //idx๊ฐ์ ๋ฐ๋ฅธ answer ๊ฐ๋ค ๊ฐ์ ธ์ค๊ธฐ
public void updateMbanswer(MbAnswerDto dto);
public void deleteMbanswer(String idx);
}
- @Mapper ์ด๋
ธํ
์ด์
์ -> Marker์ด๋ค. ์ฆ ์ด๊ฒ mapper๋ฅผ ์ฒ๋ฆฌํ๋ ๊ณณ์ด๋ค ๋ผ๋๊ฑธ ์๋ ค์ฃผ๋ ์ด๋
ธํ
์ด์
- @Mapper์ ์์ ์ด๋ ธํ ์ด์ ์ @Repository ์ด๋ค -> @Repository๋ bean์ ๋ฑ๋ก์ ์์ผ์ค๋ค.
#3) mapper
<?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="boot.data.mapper.MbanswerMapperInter">
<insert id="insertMbanswer" parameterType="mbdto">
insert into mbanswer(num,name,myid,content,writeday) values (#{num},#{name},#{myid},#{content},now())
</insert>
<select id="getAllAnswers" resultType="mbdto" parameterType="String">
select * from mbanswer where num=#{num} order by idx desc;
</select>
<select id="getAnswer" resultType="mbdto" parameterType="String">
select * from mbanswer where idx=#{idx}
</select>
<update id="updateMbanswer" parameterType="mbdto">
update mbanswer set content=#{content} where idx=#{idx}
</update>
<delete id="deleteMbanswer" parameterType="String">
delete from mbanswer where idx=#{idx}
</delete>
</mapper>
- mapper์ ๋ค์ด๊ฐ๋ id๋ค์ service์์ ์์ฑํ ๋ฉ์๋ ์ด๋ฆ์ด๋ ๊ผญ ๊ฐ๊ฒ ํด์ฃผ์ด์ผ ํ๋ค. 4
#4) controller
package boot.data.controller;
import java.util.List;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import boot.data.dto.MbAnswerDto;
import boot.data.mapper.MbanswerMapperInter;
import boot.data.service.MemberService;
@RestController
@RequestMapping("/mbanswer")
public class MbanswerController {
@Autowired
MbanswerMapperInter mapper;
@Autowired
MemberService memberService;
@PostMapping("/ainsert")
public void insert(@ModelAttribute MbAnswerDto dto, HttpSession httpSession) {
//์ธ์
์์ ์ ์ฅํ ์์ด๋๋ฅผ dto์ ์ ์ฅ์ํจ๋ค.
String myid = (String)httpSession.getAttribute("myid");
dto.setMyid(myid);
//์ธ์
์์ ์ ์ฅ๋ ์ด๋ฆ์ dto์ ์ ์ฅ์ํจ๋ค.
String name =memberService.getName(myid);
dto.setName(name);
mapper.insertMbanswer(dto);
}
@GetMapping("/alist")
public List<MbAnswerDto> alist(@RequestParam String num)
{
return mapper.getAllAnswers(num);
}
@GetMapping("/mbdelete")
public void adelete(@RequestParam String idx) {
mapper.deleteMbanswer(idx);
}
}
- Ajax๋ก ๋ฐ์ดํฐ๋ฅผ ์ฒ๋ฆฌํ ๋๋ @Responsebody || @RestController ๋ฅผ ๊ผญ ์ ์ธํ๊ณ ํด์ฃผ์ด์ผ ํ๋ค ์๋๋ฉด ์ปดํ์ผ์๋ฌ
- insertํ ๋๋ @Modelattribute๋ฅผ ํตํด MbanswerDto ๊ฐ์ ๋ค ๋ณด๋ด์ค๋ค.
- ๊ทธ๋ฆฌ๊ณ httpsession์ ์ฌ์ฉํด์ , ์ธ์ ์์ ์ ์ฅํ ์์ด๋๋ฅผ ๋ณด๋ด์ค dto์ ๋ด์์ ๋ณด๋ด์ค๋ค.
- ๋ค๋ฅธ๋ฐฉ์์ผ๋ก ๋ฏธ๋ฆฌ ๋ง๋ค์ด๋ ๋ฉ์๋๋ฅผ ํตํด์ name๊ฐ์ ๊ฐ์ด dto์ ๋ด์์ ๋ณด๋ด์ค๋ค.
- List๋ก ์ถ๋ ฅํ ๋๋ ๊ฐ๋จํ๊ฒ, ๋ฐํํ๋ ๊ฐ์ ๋น์ฐํ dto๊ฐ๋ค์ด๊ณ , ์ด dto๊ฐ๋ค์ list์ ๋ด์์ ๋ฐํํด์ค๋ค
- ๊ทธ๋ฆฌ๊ณ num๊ฐ์ ๋ง๋ list๋ฅผ ์ถ๋ ฅํด์ผ ํ๊ธฐ ๋๋ฌธ์ num๊ฐ ๋ํ ๋งค๊ฐ๋ณ์ ๊ฐ์ผ๋ก ๊ฐ์ด ๋ณด๋ด์ฃผ๊ณ service์์ ๋ฏธ๋ฆฌ ์์ฑํด๋ ๋ฉ์๋์ num์ ๋ด์์ return ํด์ค๋ค
- delete๋ ๊ฐ๋จํ๊ฒ ๋๊ธ์ primary key์ธ idx์ ๋ฐ๋ผ์ ์ญ์ ํด์ค๋ค.
#5) ๋ง์ง๋ง์ผ๋ก form์์ ์ ๋ชจ๋ ๋ฐ์ดํฐ๋ค์ ๊ฐ์ง๊ณ ajax ์ฒ๋ฆฌํ๋ ์ฝ๋๋ฅผ ๋ณด์ฌ๋๋ฆฌ๊ฒ ์ต๋๋ค.
<script>
$(function(){
list();
$("#btnansweradd").click(function (){
var num = $(this).attr("num")
var content = $("#content").val();
var myid = $("#myid").attr("myid");
loginok = "${sessionScope.loginok}";
//alert(num+"_"+content+"_"+myid+"_"+loginok);
if(content.length==0) {
alert("๋๊ธ์ ์
๋ ฅํด์ฃผ์ธ์");
return;
}
$.ajax({
type:"post",
url:"/mbanswer/ainsert",
dataType:"html",
data : {"num":num,"content":content},
success:function (data){
alert("๋ฑ๋ก๋์์ต๋๋ค.")
//๋๊ธ ์์ ์ฐฝ ๋์ฐ๊ธฐ
list();
//์
๋ ฅ๊ฐ ์ด๊ธฐํ
$("#content").val("");
}
})
})
//๋๊ธ ์ญ์
$(document).on("click","#del",function(){
var idx= $(this).attr("idx");
var a = confirm("์ญ์ ํ์๊ฒ ์ต๋๊น?");
//alert(idx);
if(a) {
$.ajax({
type:"get",
dataType :"json",
url : "/mbanswer/mbdelete",
data : {"idx":idx},
success:function(){
location.reload();
}
})
}
})
// $("i.update").click(function(){
//
// })
})
function list() {
var num = $("#num").val();
//alert(num);
var loginok = "${sessionScope.loginok}";
var myid = "${sessionScope.myid}";
//alert(loginok + myid);
$.ajax({
type:"get",
dataType :"json",
url : "/mbanswer/alist",
data : {"num":num},
success:function(res){
$("span.acount").text(res.length);
var s = "";
$.each(res, function (i, dto) {
s += "<br>";
s += "<table class='table table-striped' style='width:800px; height:100px;'>"
s += "<input type='hidden' id='idx' value='idx'>";
s += "<tr>";
s += "<td><b> ๐ ์์ฑ์ : " + dto.name + "</b>" + "<span class='day' style='float:right'> ์์ฑ๋ ์ง : "+dto.writeday+" ";
if(loginok!=null || myid ==dto.myid) {
s += "<i class='bi bi-trash3-fill' id='del' idx='"+dto.idx+"' style='cursor: pointer'></i> <i class='bi bi-clipboard-check update' style='cursor: pointer'></i></span></td>";
}
s += "</tr>";
s += "<tr>";
s += "<td> ๐ ๋๊ธ : " +dto.content + "</td>";
s += "</tr>"
s += "</table>"
});
$("div.alist").html(s);
}
})
//๋๊ธ ์์ ์ฐฝ ๋์ฐ๊ธฐ
}
</script>
- ๋๊ธ ๋ฑ๋ก ๋ฒํผ์ ๋๋ฅด๋ฉด, ์ด๋ฒคํธ๊ฐ ๋ฐ์ํ๊ณ ๋๊ธ์ ํด๋ฆญํด์ ์๋ฒ๋ก ์ ์ก๋ ๋๋ list๋ก ์ถ๋ ฅ ๋ ๊ฐ์ ๋ฏธ๋ฆฌ ์ค์ ํด๋๊ณ ajax ์ฒ๋ฆฌ๋ฅผ ํด์ค์ผํฉ๋๋ค. ex) var content = $("#content").val();
- ๋๊ธ ์ฐฝ์ด null๊ฐ ์ฆ ๋๊ธ์ฐฝ์ ์๋ฌด๊ฒ๋ ์์ด ์๋ฒ๋ก ์ ์กํ๋ คํ๋ฉด ๋๊ธ ์ ๋ ฅํ๋ผ๋ ์๋ฆผ์ด ๋ฌ๋ค
- ๊ทธ๋ฆฌ๊ณ ์ ๊ณผ์ ์ด ์ ๋์๋ค๋ฉด ajax๋ฅผ ํตํด์ ๋ฐ์ดํฐ๋ฅผ ์ฒ๋ฆฌํด์ค๋ค.
๋ค์์ผ๋ก๋ ajax์ฒ๋ฆฌ๋ ๋ฐ์ดํฐ๋ค์ list๋ก ๋์๋๋ค.
- ์ด๊ฒ ๋ํ ajax๋ก ์ฒ๋ฆฌ ํ๊ธฐ ๋๋ฌธ์ ๊ธฐ์กด์๋ ๋๊ธ๋ค์ด ๋ค ๋ณด์ด๊ณ , ๋ด๊ฐ ๋๊ธ์ ๋ฑ๋กํ๋ฉด์ ๋ฐ๋ก ๋งจ์์ ์ฌ๋ผ์ค๊ฒํ๋ค.
- $("span.acount").text(res.length); ์ด ์ฝ๋๋ ๋๊ธ์ ์๋ฅผ ์ถ๋ ฅํ๊ธฐ ์ํ ์ฝ๋์ด๋ค.
๋ค์์ ์ญ์ ์ ๋๋ค.
- ์ญ์ ํ๋ ๋ฒํผ์ ๋ฏธ๋ฆฌ idx๊ฐ์ ์ฌ์ด๋์ต๋๋ค. ๊ทธ๋ฆฌ๊ณ ๊ทธ ๋ฒํผ์ด ํด๋ฆญ๋๋ฉด์ idx๊ฐ์ ๊ฐ์ ธ์ค๋๊ฒ ์ค์ํฉ๋๋ค
- var idx= $(this).attr("idx");
- ๊ทธ๋ฆฌ๊ณ ๋ฌด๋ํ๊ฒ ajax์ฒ๋ฆฌ๋ฅผํด์ ์ญ์ ํ๋ฉด ๋์ ๋๋ค
๋ค์์ ์์ ์ ๋๋ค.