▶Main.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>
<a href = "Join.jsp">회원가입</a>
<br>
<a href = "Login.jsp">로그인</a>
<br>
<a href = "SelectAl.jsp">전체 회원 확인하기</a>
<br>
<a href = "Delete.jsp">회원탈퇴</a>
</body>
</html>
▶SqlSessionManager.java
<?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">
<!-- namespace : mapper 식별자(고유의 이름) -->
<!-- 절대 중복되면 안된다. 중복되는 순간 mapper를 찾지 못한다. -->
<mapper namespace="com.smhrd.model.DAO" >
<!-- Query문 작성해서 넣는 공간 -->
<!-- parameterType : mybais-config.xml 안에 alias로 정의해놨다! -->
<insert id="memberinsert" parameterType="IotMember">
insert into iotmember values(#{id},#{pw},#{age},#{nickname})
<!-- IotMember.java에 있는 필드명임 -->
</insert>
<delete id ="memberdelete" parameterType="IotMember">
delete from iotmember where id= #{id} and pw = #{pw}
</delete>
</mapper>
▶DAO.java
package com.smhrd.model;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import com.smhrd.database.SqlSessionManager;
public class DAO {
private SqlSessionFactory sqlSessionFactory = SqlSessionManager.getSqlSession();
private SqlSession sqlSession = null;
public int join(IotMember vo) {
int row = 0;
try {
// 1. db 연결
// openSession(boolean) --> 안쪽에 true를 넣으면 auto commit 가능
// openSession() --> insert, delete, update 할때마다 commit을 직접 해줘야 함!
sqlSession= sqlSessionFactory.openSession(true);
// 2. 실행
row = sqlSession.insert("com.smhrd.model.DAO.memberinsert", vo);
}catch (Exception e) {
e.printStackTrace();
}finally {
// 3. 자원반납
sqlSession.close();
}
return row;
}
public int delete(IotMember vo) {
int row = 0;
try {
// 1. 연결
sqlSession= sqlSessionFactory.openSession(true);
row = sqlSession.delete("com.smhrd.model.DAO.memberdelete", vo);
}catch (Exception e) {
e.printStackTrace();
}finally {
sqlSession.close();
}
return row;
}
}
▶DeleteCon.java
package com.smhrd.controller;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.smhrd.model.DAO;
import com.smhrd.model.IotMember;
@WebServlet("/DeleteCon")
public class DeleteCon extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 1. 데이터 가져오기
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html; charset=UTF-8");
PrintWriter out = response.getWriter();
String id = request.getParameter("id");
String pw = request.getParameter("pw");
// 2. 데이터 하나로 묶기
IotMember vo = new IotMember(id, pw);
// 3. DB안에 있는 데이터를 삭제하기
// --> mybatis 흐름에 대해서 고민해보기
// --> 어떤파일들을 수정해야할까?(DAO, MemberMapper.xml)
DAO dao = new DAO();
int row = dao.delete(vo);
// 4. 삭제 성공했다면 Main.jsp로 이동
if(row > 0) {
response.sendRedirect("Main.jsp");
// 5. 실패했다면 회원탈퇴 실패라고 출력!
}else {
out.print("회원탈퇴 실패");
}
}
}
'Maven Project' 카테고리의 다른 글
회원가입 실습 (0) | 2022.05.18 |
---|---|
전체회원 확인하기 (0) | 2022.05.17 |
로그인 (0) | 2022.05.17 |
회원가입 (0) | 2022.05.16 |
Maven Project (0) | 2022.05.13 |