-
[MyBatis] MyBatis 로그인 만들기개발환경/Mybatis 2022. 12. 28. 19:18728x90
위 게시글에 이어서 로그인 기능을 구현해보자.
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