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

회원 목록에 수정 버튼 만들기

<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>
			<td><a th:href="@{/member/modifyMember(memberId=${l.memberId}, memberName=${l.memberName})}">수정</a></td>
			<td><a href="#">삭제</a></td>
		</tr>
		<tr th:unless="${not #lists.isEmpty(memberList)}">
			<td colspan="7">등록된 회원의 정보가 없습니다.</td>
		</tr>
	</tbody>
</table>

 

<!-- /member/modifyMember?memberId=id001&memberName=홍01 -->
get 방식으로 넘기는게 타임리프 구문에선 () 괄호 안에 쉼표로 구분해서 넣기

 

실행 화면

 

 

수정화면

MemberMapper 인터페이스 작성

//회원별 회원정보 조회
public Member selectMember(String memberId);

 

MemberMapper.xml 쿼리 작성

<resultMap type="Member" id="memberResultMap">
	<id column="m_id" property="memberId" />
	<result column="m_pw" property="memberPw" />
	<result column="m_name" property="memberName" />
	<result column="m_level" property="memberLevel" />
	<result column="m_email" property="memberEmail" />
	<result column="m_addr" property="memberAddr" />
	<result column="m_reg_date" property="memberRegDate" />
</resultMap>

<select id="selectMember" parameterType="String" resultMap="memberResultMap">
	/*회원별 회원정보 조회*/
	SELECT
		 m.m_id
		,m.m_pw		
		,m.m_name
		,m.m_level	
		,m.m_email	
		,m.m_addr	
		,m.m_reg_date
	FROM
		tb_member AS m
	WHERE
		m.m_id = #{memberId};
</select>

 

MemberService 작성

public Member selectMember(String memberId) {		
	return memberMapper.selectMember(memberId);
}

 

 

MemberController 작성

/**
 * 회원 수정화면
 */
@GetMapping("/modifyMember")
public String modifyMember(Model model
						,@RequestParam(name="memberId", required = false) String memberId
						,@RequestParam(name="memberName", required = false) String memberName) {
	
    log.info("회원 수정화면 폼 쿼리스트링 memberId : {}", memberId);
	log.info("회원 수정화면 폼 쿼리스트링 memberName : {}", memberName);
    
	Member updateMember = memberService.selectMember(memberId);
	List<MemberLevel> memberLevelList = memberService.getMemberLevelList();
	
	model.addAttribute("title", "회원수정화면");
	model.addAttribute("updateMember", updateMember);
	model.addAttribute("memberLevelList", memberLevelList);
	
	
	return "member/modifyMember";
}

 

 

수정 화면 만들기

<!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">
		<form th:action="@{/member/modifyMember}" method="post" >
			<table border="1">
				<tbody>
					<tr>
						<td>회원아이디</td>
						<td>
							<input type="text" name="memberId" readonly th:value="${updateMember.memberId}">
						</td>
					</tr>
					<tr>
						<td>회원비밀번호</td>
						<td>
							<input type="text" name="memberPw" th:value="${updateMember.memberPw}" >
						</td>
					</tr>
					<tr>
						<td>회원이름</td>
						<td>
							<input type="text" name="memberName" th:value="${updateMember.memberName}">
						</td>
					</tr>
					<tr>
						<td>회원권한</td>
						<td>
							<select name="memberLevel" style="width:100%;" >
								<th:block th:if="${not #lists.isEmpty(memberLevelList)}" th:each="l : ${memberLevelList}" >
									<option th:value="${l.levelNum}" th:selected="${l.levelNum==updateMember.memberLevel}">[[${l.levelName}]]</option>
								</th:block>
								<th:block th:unless="${not #lists.isEmpty(memberLevelList)}">
									<option>등록된 회원등급이 없습니다.</option>
								</th:block>
							</select>
						</td>
					</tr>
					<tr>
						<td>회원이메일</td>
						<td>
							<input type="text" name="memberEmail" th:value="${updateMember.memberEmail}">
						</td>
					</tr>
					<tr>
						<td>회원주소</td>
						<td>
							<input type="text" name="memberAddr" th:value="${updateMember.memberAddr}">
						</td>
					</tr>
				</tbody>
				<tfoot>
					<tr>
						<td colspan="2">
							<button  type="submit" style="width:100%; height:30px; line-height: 30px;" >회원수정</button>
						</td>
					</tr>
				</tfoot>
			</table>
		</form>
	</th:block>
</html>

 

실행 결과

 

 

 

수정처리

 

MemberMapper 인터페이스 작성

//회원 수정 처리
public void modifyMember(Member member);

 

 

MemberMapper.xml 쿼리 작성

<update id="modifyMember" parameterType="Member">
	/*회원정보수정*/
	UPDATE tb_member
	<trim prefix="SET" suffixOverrides=",">
		<if test="memberPw != null and memberPw != ''">m_pw = #{memberPw},</if>			
		<if test="memberLevel != null and memberLevel != ''">m_Level = #{memberLevel},</if>			
		<if test="memberName != null and memberName != ''">m_Name = #{memberName},</if>			
		<if test="memberEmail != null and memberEmail != ''">m_Email = #{memberEmail},</if>			
		<if test="memberAddr != null and memberAddr != ''">m_Addr = #{memberAddr}</if>			
	</trim>
	WHERE
		m_id = #{memberId};
</update>

 

 

MemberService 작성

public void  modifyMember(Member member) {
	memberMapper.modifyMember(member);
}

 

 

MemberController 작성

/**
 * 회원수정처리
 */
@PostMapping("/modifyMember")
public String modifyMember(Member member) {
	
	log.info("회원수정에서 입력받은 데이터: {}", member);
	memberService.modifyMember(member);
	
	return "redirect:/member/memberList";
}

 

원본 실행 시

금액변경 후 실행 결과