ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [MyBatis] MyBatis 로그인 만들기
    개발환경/Mybatis 2022. 12. 28. 19:18
    728x90

     

     

    https://bluechanyeong.tistory.com/entry/MyBatis-MyBatis-%ED%9A%8C%EC%9B%90%EA%B0%80%EC%9E%85-%EB%A7%8C%EB%93%A4%EA%B8%B0

     

    [MyBatis] MyBatis 회원가입 만들기

    https://bluechanyeong.tistory.com/entry/MyBatis-MyBatis-%EC%82%AC%EC%9A%A9%ED%95%98%EA%B8%B0 [MyBatis] MyBatis 사용하기 MyBatis는 쿼리 기반 웹 애플리케이션을 개발할 때 가장 많이 사용되는 SQL 매퍼(Mapper) 프레임워크이다

    bluechanyeong.tistory.com

    위 게시글에 이어서 로그인 기능을 구현해보자.

     

     

     

    MemberDAO.java

    // 생략
    
    public class MemberDAO {
    
    	// 생략
    
    	public int idcheck(MemberDTO member){
    		// 생략
    	}
    	
    	public int join(MemberDTO member){
    		// 생략
    	}
        
        // 로그인 로직 추가
        public MemberDTO login(MemberDTO member) {
    		HashMap<String, String> dataMap = new HashMap();
    		dataMap.put("mem_userid", member.getUserid());
    		dataMap.put("mem_userpw", member.getUserpw());
    		dataMap = sqlsession.selectOne("Member.login", dataMap);
    		
    		if(dataMap != null) {
    			member.setIdx(Integer.parseInt(String.valueOf(dataMap.get("mem_idx"))));
    			member.setUserid(dataMap.get("mem_userid"));
    			member.setUsername(dataMap.get("mem_name"));
    			return member;
    		}
    		return null;
    	}
    
    }

     

     

     

     

    member.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="Member">
       <select id="idcheck" parameterType="map" resultType="int">
        select count(*) from tb_member where mem_userid=#{mem_userid};
      </select>
      
      <insert id="join" parameterType="map">
        insert into tb_member 
        (mem_userid, mem_userpw, mem_name, mem_hp, mem_email, mem_hobby, mem_ssn1, mem_ssn2,
        mem_zipcode, mem_address1, mem_address2, mem_address3) 
        values 
        (#{mem_userid}, #{mem_userpw}, #{mem_name}, #{mem_hp}, #{mem_email} ,#{mem_hobby} ,
        #{mem_ssn1} ,#{mem_ssn2} ,#{mem_zipcode} ,#{mem_address1} ,#{mem_address2} ,#{mem_address3});
      </insert>
      
      <!-- 사용자 조회 쿼리 추가 -->
      <select id="login" parameterType="map" resultType="map">
        select mem_idx, mem_userid, mem_name from tb_member where mem_userid=#{mem_userid} and mem_userpw=#{mem_userpw};
      </select>
    </mapper>

     

     

     

     

    login.jsp

    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
    <%
    	String userid = null;
    	String name = null;
    	if(session.getAttribute("userid") != null){
    		userid = (String)session.getAttribute("userid");
     		name = (String)session.getAttribute("name");
    	}
    %>
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>로그인</title>
    </head>
    <body>
    	<h2>로그인</h2>
    <%
    	if(userid == null){
    %>
    	<form method="post" action="login_ok.jsp">
    		<p>아이디 : <input type="text" name="userid"></p>
    		<p>비밀번호 : <input type="password" name="userpw"></p>
    		<p><input type="submit" value="로그인"></p>
    		<p>회원이 아니신가요? <a href='./regist.jsp'>회원가입</a></p>
    	</form>
    <%
    	}else{
    %>
    	<h3><%=userid%>(<%=name%>)님 환영합니다.</h3>
    	<p>
    		<input type="button" value="로그아웃" onclick="location.href='logout.jsp'">
    	</p>
    <%
    	}
    %>
    </body>
    </html>

     

     

     

     

    login_ok.jsp

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <%@ page import="java.sql.*"%>
    
    <jsp:useBean id="member" class="com.blue.member.MemberDTO"/>
    <jsp:setProperty property="*" name="member"/>
    <jsp:useBean id="dao" class="com.blue.member.MemberDAO"/>
    <%
    	if((member = dao.login(member)) != null){
    		session.setAttribute("userid", member.getUserid());
    		session.setAttribute("name", member.getUsername());
    		session.setAttribute("idx", member.getIdx());
    %>
    	<script>
    		alert('로그인 되었습니다');
    		location.href="login.jsp";
    	</script>
    <%			
    	}else{	// 로그인 실패
    %>
    	<script>
    		alert('아이디 또는 비밀번호를 확인하세요');
    		history.back();
    	</script>
    <%
    	}	
    %>

     

     

     

     

    logout.jsp

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <%
    	session.invalidate();
    %>
    <script>
    	alert('로그아웃 되었습니다');
    	location.href='login.jsp';
    </script>

     

     

     

     

    로그인 페이지

     

     

     

    로그인 성공

    728x90

    '개발환경 > Mybatis' 카테고리의 다른 글

    Oracle JDBC 의존성 선정방법  (0) 2023.11.28
    [MyBatis] MyBatis 회원가입 만들기  (0) 2022.12.26
    [MyBatis] MyBatis 사용하기  (0) 2022.12.21

    댓글

Designed by Tistory.