everyday com-eat
카테고리
작성일
2022. 3. 18. 17:36
작성자
갱수터
728x90

회원 목록 2022.02.22 - [Framework/Spring] - (Springboot) 🆁 Mybatis 회원 목록 조회

회원 수정 2022.03.18 - [Framework/Spring] - (Springboot) 🆄 Mybatis 회원 수정 화면 및 처리

 

 

회원목록에서 삭제 버튼 만들고 경로 연결하기

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" 
	xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
	layout:decorate="~{layout/default}">
	<!-- 사용자 정의 title -->
	<th:block layout:fragment="customTitle">
		<title th:text="${title}"></title>
	</th:block>
	<th:block layout:fragment="customContents">
		<table border="1">
			<thead>
				<tr>
					<th>회원아이디</th>
					<th>회원비밀번호</th>
					<th>회원이름</th>
					<th>회원등급</th>
					<th>회원이메일</th>
					<th>회원주소</th>
					<th>회원등록날짜</th>
					<th>수정</th>
					<th>삭제</th>
				</tr>
			</thead>
			<tbody>
				<tr th:if="${not #lists.isEmpty(memberList)}" th:each="l : ${memberList}">
					<td th:text="${l.memberId}"></td>
					<td th:text="${l.memberPw}"></td>
					<td th:text="${l.memberName}"></td>
					<td th:text="${l.memberLevel}"></td>
					<td th:text="${l.memberEmail}"></td>
					<td th:text="${l.memberAddr}"></td>
					<td th:text="${l.memberRegDate}"></td>
					<!-- /member/modifyMember?memberId=id001&memberName=홍01 -->
					<td><a th:href="@{/member/modifyMember(memberId=${l.memberId}, memberName=${l.memberName})}">수정</a></td>
					<td><a th:href="@{/member/removeMember(memberId=${l.memberId})}">삭제</a></td>
				</tr>
				<tr th:unless="${not #lists.isEmpty(memberList)}">
					<td colspan="7">등록된 회원의 정보가 없습니다.</td>
				</tr>
			</tbody>
		</table>
	</th:block>
</html>

 

 

삭제 화면

 

삭제화면 만들기

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" 
	xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
	layout:decorate="~{layout/default}">
	<!-- 사용자 정의 title -->
	<th:block layout:fragment="customTitle">
		<title th:text="${title}"></title>
	</th:block>
	
	<!-- 사용자 정의 Script -->
	<th:block layout:fragment="customScript">
		<script>
			$(function(){
				$('#removeBtn').click(function(){
					var confirmResult = confirm('정말로 탈퇴하시겠습니까?');
					
					if(confirmResult){
						$('#removeForm').submit();
					}
				});
			});
		</script>	
	</th:block>
		
	<th:block layout:fragment="customContents">
		<form id="removeForm" th:action="@{/member/removeMember}" method="post" >
			<table border="1">
				<tbody>
					<tr>
						<td>회원아이디</td>
						<td>
							<input type="text" name="memberId" readonly th:value="${memberId}">
						</td>
					</tr>
					<tr>
						<td>회원비밀번호</td>
						<td>
							<input type="text" name="memberPw" >
						</td>
					</tr>
				</tbody>
				<tfoot>
					<tr>
						<td colspan="2">
							<button id="removeBtn" type="button" style="width:100%; height:30px; line-height: 30px;" >회원탈퇴</button>
						</td>
					</tr>
				</tfoot>
			</table>
			<div th:if="${not #strings.isEmpty(result)}">
				<p th:text=${result} style="color:red;"></p>
			</div>
		</form>
	</th:block>
</html>
table 아래 div -> 비밀번호 틀렸을 때 알림문구 나올 곳

실행 결과


상품삭제 버튼 눌렀을 때


삭제 실패 시 나오는 알림 문구

 

 

MemberController 작성

/**
 * 회원탈퇴화면
 */
@GetMapping("/removeMember")
public String removeMember(Model model, @RequestParam(name="memberId", required = false) String memberId
										, @RequestParam(name="result", required = false) String result) {
	
	
	model.addAttribute("title", "회원탈퇴화면");
	model.addAttribute("memberId", memberId);
	
	if(result != null) model.addAttribute("result", result);

	
	return "member/removeMember";
}

 

 

 

삭제처리

 

MemberService 작성

public void removeMember(Member member) {
	//권한
	String memberLevel = member.getMemberLevel();
	String memberId = member.getMemberId();
	
	//권한별 삭제 프로세스
	if("2".equals(memberLevel)) {	//판매자
		memberMapper.removeOrderBySellerId(memberId);
		memberMapper.removeGoodsBySellerId(memberId);
		
	}else if("3".equals(memberLevel)) {		//구매자
		memberMapper.removeOrder(memberId);
	}
	
	//공통사항 삭제 프로세스
	memberMapper.removeLoginHistory(memberId);
	memberMapper.removeMember(memberId);
}

 

MemberMapper 인터페이스 작성

//상품코드에 따른 구매이력 삭제
public void removeOrderBySellerId(String memberId);

//상품코드에 따른 구매이력 삭제
public void removeGoodsBySellerId(String memberId);

//구매자가 구매한 이력 
public void removeOrder(String memberId);

//회원의 로그인 이력 삭제
public void removeLoginHistory(String memberId);

//회원 삭제
public void removeMember(String memberId);

 

MemberMapper.xml 쿼리 작성

<delete id="removeMember" parameterType="String">
	/*회원 삭제*/
	DELETE
	FROM
		tb_member
	WHERE
		m_id = #{memberId}
</delete>

<delete id="removeLoginHistory" parameterType="String">
	/*회원 로그인 이력 삭제*/
	DELETE
	FROM
		tb_login
	WHERE
		login_id = #{memberId}
</delete>

<delete id="removeOrder" parameterType="String">
	/*구매자가 구매한 구매 이력 삭제*/
	DELETE
	FROM
		tb_order
	WHERE
		o_id = #{memberId};
</delete>

<delete id="removeGoodsBySellerId" parameterType="String">
	/*판매자가 등록한 상품 삭제*/
	DELETE
	FROM
		tb_goods
	WHERE
		g_seller_id = #{memberId};
</delete>

<delete id="removeOrderBySellerId" parameterType="String">
	/*상품코드에 따른 구매 삭제 */
	DELETE
		o
	FROM
		tb_goods AS g
		INNER JOIN
		tb_order AS o
		ON 
		g.g_code = o.o_g_code
	WHERE
		g.g_seller_id = #{memberId};
</delete>

 

 

MemberController 작성

/**
 * 회원탈퇴처리
 * defaultValue = "" 기본값 공백으로두면 if 조건식 세울 때 memberPw != null 조건을 안 넣어도 됨
 */
@PostMapping("/removeMember")
public String removeMember(@RequestParam(name="memberId", required = false) String memberId
				,@RequestParam(name="memberPw", required = false, defaultValue = "") String memberPw
				,RedirectAttributes reAttr) {
	
	log.info("회원 탈퇴 처리 memberId : {}", memberId);
	log.info("회원 탈퇴 처리 memberPw : {}", memberPw);
	
	Member member = memberMapper.selectMember(memberId);

	if(member != null && member.getMemberPw() != null && memberPw.equals(member.getMemberPw())) {
		memberService.removeMember(member);
		log.info("회원탈퇴성공");
		
		return "redirect:/member/memberList";
	}
	
	reAttr.addAttribute("memberId", memberId);
	reAttr.addAttribute("result", "회원의 정보가 일치하지 않습니다.");
	log.info("회원탈퇴실패");
	
	/*/member/removeMember?memberId=id001&result=회원의정보가일치하지않습니다.*/
	return "redirect:/member/removeMember";		
	
}