개발/Mini Project

[Spring] 아이디 찾기, 비밀번호 찾기

뉴NEW 2022. 12. 29. 00:30

아이디 찾기 폼

 

<main>
<div class="form-wrapper">
    <form id="searchIdForm">
        <h3>아이디 찾기</h3>
        <div class="input-wrapper" style="display:block; margin:20px auto;">
            <input type="text" id="userName" name="userName" placeholder=" 이름" required>
        </div>
        <div class="input-wrapper" style="display:block; margin:20px auto;">
            <input type="text" id="userBirth" name="userBirth" placeholder=" 생년월일 8자리(ex.19001201)" required>
        </div>
        <div id="resultIdDiv" style="display: none; font-weight: bold; font-style: italic; ">
            <p id="searchIdResult"></p>
        </div>
        <div style="display:block; margin:20px auto; padding-top:15px;">
            <button type="button" id="btnFindId">확 인</button>
        </div>
        <div class="login_append">
        <span class="txt_find">
           <a href="/user/login.do" class="link_find">로그인</a>
            |
           <a href="/user/searchPw.do" class="link_find">비밀번호 찾기</a>
            |
           <a href="/user/join.do" class="link_find">회원가입</a>
         </span>
       </div>	
    </form>
</div>
</main>

 

 

< controller >

아이디 찾기 

@Controller
@RequestMapping("/user")
public class UserController {
	@Autowired
	private UserService userService;

	// 아이디 찾기 화면으로 이동
	@RequestMapping(value = "/searchId.do", method = RequestMethod.GET)
	public String searchIdView(HttpServletRequest request, Model model,
	        UserVO searchVO) {
	    
	    return "/user/searchId";
	}
	// 아이디 찾기 결과
	@PostMapping("/searchId.do")
	@ResponseBody
	public String searchId(UserVO userVO) throws JsonProcessingException {
		String userId = userService.searchId(userVO);
		
		ObjectMapper mapper = new ObjectMapper();
		Map<String, String> resultMap = new HashMap<String, String>();
		
		if(userId != null) {
			resultMap.put("msg", "ok");
			resultMap.put("searchId", userId);
		} else {
			resultMap.put("msg", "fail");
		}
		
		String jsonStr = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(resultMap);
		
		return jsonStr;
	}
	
}

< service >

public interface UserService {
	// 아이디 찾기
	String searchId(UserVO searchVO);
		
	// 비밀번호 찾기
	String searchPw(UserVO searchVO);	
}

 

< serviceimpl >

@Service
public class UserServiceImpl implements UserService {
	@Autowired
	private UserDAO userDAO;

	// 아이디 찾기
	@Override
	public String searchId(UserVO searchVO) {
		return userDAO.searchId(searchVO);
	}
	// 비밀번호 찾기
	@Override
	public String searchPw(UserVO searchVO) {
		return userDAO.searchPw(searchVO);
	}
}

 

< DAO >

@Repository
public class UserDAO {
	@Autowired
	private SqlSessionTemplate mybatis;

	// 아이디 찾기
	public String searchId(UserVO searchVO) {
		return mybatis.selectOne("UserDAO.searchId",searchVO);
	}
	
	// 비밀번호 찾기
	public String searchPw(UserVO searchVO) {
		return mybatis.selectOne("UserDAO.searchPw",searchVO);
	}

}

 

mapper.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="UserDAO">
	<!-- 쿼리 작성 영역 -->
	<select id="searchId" parameterType="user" resultType="string">
	<![CDATA[
		SELECT USER_ID FROM AL_USER
			WHERE USER_NAME = #{userName}
			  AND USER_BIRTH = #{userBirth}
			  AND USER_USE_YN = 'Y'
	
	]]>    
	</select>
	
	<select id="searchPw" parameterType="user" resultType="string">
	<![CDATA[
		SELECT USER_PW FROM AL_USER
			WHERE USER_ID = #{userId}
			  AND USER_NAME = #{userName}
			  AND USER_BIRTH = #{userBirth}
	
	]]>    
	</select>
</mapper>

 

 

< script >

 

아이디 찾기

<script>
    $(function() {
        $("#btnFindId").on("click", function() {
            console.log($("#searchIdForm").serialize());

            $.ajax({
                url: "/user/searchId.do",
                type: "post",
                data: $("#searchIdForm").serialize(),
                success: function(obj) {
                    const data = JSON.parse(obj);

                    console.log(data);

                    $("#resultIdDiv").show();

                    if(data.msg == "ok") {
                        $("#searchIdResult").text("아이디는 " + data.searchId +" 입니다.");
                    } else{
                        $("#searchIdResult").text("존재하지 않는 사용자입니다.");
                    }
                },
                error: function(e) {
                    console.log(e);
                }
            });
        });
    });

</script>

 

아이디 찾기

 

비밀번호 찾기 폼

 

<main>
<div class="form-wrapper">
    <form id="searchPwForm">
        <h3>비밀번호 찾기</h3>
        <div class="input-wrapper" style="display:block; margin:20px auto;">
            <input type="text" id="userId" name="userId" placeholder=" 아이디" required>
        </div>
        <div class="input-wrapper" style="display:block; margin:20px auto;">
            <input type="text" id="userName" name="userName" placeholder=" 이름" required>
        </div>
        <div class="input-wrapper" style="display:block; margin:20px auto;">
            <input type="text" id="userBirth" name="userBirth" placeholder=" 생년월일 8자리(ex.19001201)" required>
        </div>
        <div id="resultPwDiv" style="display: none; font-weight: bold; font-style: italic;">
            <p id="searchPwResult"></p>
        </div>
        <div style="display:block; margin:20px auto; padding-top:15px;">
            <button type="button" id="btnFindPw">확 인</button>
        </div>

        <div class="login_append">
        <span class="txt_find">
           <a href="/user/login.do" class="link_find">&nbsp;&nbsp;&nbsp;로그인</a>
            |
           <a href="/user/searchId.do" class="link_find">아이디 찾기</a>
            |
           <a href="/user/join.do" class="link_find">회원가입</a>
         </span>
       </div>	
    </form>
</div>
</main>

 

< controller >

 

비밀번호 찾기

@Controller
@RequestMapping("/user")
public class UserController {
	@Autowired
	private UserService userService;
	
	// 비밀번호 찾기 화면으로 이동
	@RequestMapping(value = "/searchPw.do", method = RequestMethod.GET)
	public String searchPwView(HttpServletRequest request, Model model,
	        UserVO searchVO) {
	    
	    return "/user/searchPw";
	}
	
	// 비밀번호 찾기 결과
	@PostMapping("/searchPw.do")
	@ResponseBody
	public String searchPw(UserVO userVO) throws JsonProcessingException {
		String userPw = userService.searchPw(userVO);
		
		ObjectMapper mapper = new ObjectMapper();
		Map<String, String> resultMap = new HashMap<String, String>();
		
		if(userPw != null) {
			resultMap.put("msg", "ok");
			resultMap.put("searchPw", userPw);
		} else {
			resultMap.put("msg", "fail");
		}
		
		String jsonStr = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(resultMap);
		
		return jsonStr;
	}

}

 

 

< script >

 

비밀번호 찾기

<script>
    $(function() {
        $("#btnFindPw").on("click", function() {
            console.log($("#searchPwForm").serialize());

            $.ajax({
                url: "/user/searchPw.do",
                type: "post",
                data: $("#searchPwForm").serialize(),
                success: function(obj) {
                    const data = JSON.parse(obj);

                    console.log(data);

                    $("#resultPwDiv").show();

                    if(data.msg == "ok") {
                        $("#searchPwResult").text("비밀번호는 " + data.searchPw +" 입니다.");
                    } else{
                        $("#searchPwResult").text("아이디가 존재하지 않습니다.");
                    }
                },
                error: function(e) {
                    console.log(e);
                }
            });
        });
    });

</script>

비밀번호 찾기