ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

๐Ÿš€ ๋กœ๊ทธ์ธ, ํšŒ์›๊ฐ€์ž…, ์•„์ด๋”” ์ค‘๋ณต๊ฒ€์‚ฌ ๊ธฐ๋Šฅ ๊ตฌํ˜„ํ•ด๋ณด๊ธฐ

์ด๋ฒˆ ๊ธ€์—์„œ๋Š” JSP๋กœ ์œ„ 3๊ฐ€์ง€ ๊ธฐ๋Šฅ์— ๋Œ€ํ•ด ๊ตฌํ˜„ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค :) ์ž‘์—… ์ˆœ์„œ๋ฅผ ๋งค๊ฒจ๊ฐ€๋ฉด์„œ ์ •๋ฆฌํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

 

์šฐ์„  ๋ฏธ๋ฆฌ ํด๋” ๊ตฌ์กฐ๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด ์ง„ํ–‰ํ–ˆ์Šต๋‹ˆ๋‹ค.

0. MyBatis ์„ค์ •, Action ์ธํ„ฐํŽ˜์ด์Šค, Result ํด๋ž˜์Šค ์ƒ์„ฑ

๐Ÿ“œ src/com.app.mybatis.config (package) / config.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "https://mybatis.org/dtd/mybatis-3-config.dtd">

<configuration>
	<settings>
		<setting name="mapUnderscoreToCamelCase" value="true"/>
	</settings>

	<environments default="mysql">
		<environment id="mysql">
			<transactionManager type="JDBC"/>
			<dataSource type="POOLED">
				<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
				<property name="url" value="jdbc:mysql://localhost:3306/app"/>
				<property name="username" value="root"/>
				<property name="password" value="1234"/>
			</dataSource>
		</environment>
	</environments>
</configuration>

 

๐Ÿ“œ src/com.app.mybatis.config (package) / MyBatisConfig.java

package com.app.mybatis.config;

import java.io.IOException;
import java.io.Reader;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

public class MyBatisConfig {
	private static SqlSessionFactory sqlSessionFactory;
	
	static {
		String resource = "com/app/mybatis/config/config.xml";
		try {
			Reader reader = Resources.getResourceAsReader(resource);
			sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static SqlSessionFactory getSqlSessionFactory() {
		return sqlSessionFactory;
	}
}

 

๐Ÿ“œ src/com.app (package) / Action.java

package com.app;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public interface Action {
	public Result execute(HttpServletRequest req, HttpServletResponse rep) throws IOException, ServletException;
}

 

๐Ÿ“œ src/com.app (package) / Result.java

package com.app;

public class Result {
	private String path;
	private boolean isRedirect;
	
	public Result() {;}

	public String getPath() {
		return path;
	}

	public void setPath(String path) {
		this.path = path;
	}

	public boolean isRedirect() {
		return isRedirect;
	}

	public void setRedirect(boolean isRedirect) {
		this.isRedirect = isRedirect;
	}
}

1.  Table ๋งŒ๋“ค๊ธฐ

create database app;
use app;

create table tbl_member(
	member_id int unsigned auto_increment primary key,
	member_identification varchar(500) not null unique,
	member_password varchar(500) not null,
	member_name varchar(500) not null,
	member_birth date not null,
	member_phone varchar(500) not null,
	member_email varchar(500) not null unique
);

2. VO ๊ฐ์ฒด ๋งŒ๋“ค๊ธฐ

๐Ÿ“œ src/com.app.member.domain (package) / MemberVo.java

package com.app.member.domain;

public class MemberVO {
	private Long memberId;
	private String memberIdentification;
	private String memberPassword;
	private String memberName;
	private String memberBirth;
	private String memberPhone;
	private String memberEmail;
	
	public MemberVO() {;}

	public Long getMemberId() {
		return memberId;
	}

	public void setMemberId(Long memberId) {
		this.memberId = memberId;
	}

	public String getMemberIdentification() {
		return memberIdentification;
	}

	public void setMemberIdentification(String memberIdentification) {
		this.memberIdentification = memberIdentification;
	}

	public String getMemberPassword() {
		return memberPassword;
	}

	public void setMemberPassword(String memberPassword) {
		this.memberPassword = memberPassword;
	}

	public String getMemberName() {
		return memberName;
	}

	public void setMemberName(String memberName) {
		this.memberName = memberName;
	}

	public String getMemberBirth() {
		return memberBirth;
	}

	public void setMemberBirth(String memberBirth) {
		this.memberBirth = memberBirth;
	}

	public String getMemberPhone() {
		return memberPhone;
	}

	public void setMemberPhone(String memberPhone) {
		this.memberPhone = memberPhone;
	}

	public String getMemberEmail() {
		return memberEmail;
	}

	public void setMemberEmail(String memberEmail) {
		this.memberEmail = memberEmail;
	}

	@Override
	public String toString() {
		return "MemberVO [memberId=" + memberId + ", memberIdentification=" + memberIdentification + ", memberPassword="
				+ memberPassword + ", memberName=" + memberName + ", memberBirth=" + memberBirth + ", memberPhone="
				+ memberPhone + ", memberEmail=" + memberEmail + "]";
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((memberId == null) ? 0 : memberId.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		MemberVO other = (MemberVO) obj;
		if (memberId == null) {
			if (other.memberId != null)
				return false;
		} else if (!memberId.equals(other.memberId))
			return false;
		return true;
	}
}

3. Mapper.xml ๋งŒ๋“ค๊ณ  config.xml์— ๋“ฑ๋กํ•˜๊ธฐ

๐Ÿ“œ src/com.app.mybatis.mapper (package) / memberMapper.xml

ํ•ด๋‹น ํŒŒ์ผ์—์„œ๋Š” ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง์„ ๊ตฌํ˜„ํ•˜๋ฉด์„œ ํ•„์š”ํ•œ DB์— ์ ‘๊ทผํ•˜๋Š” ์ฟผ๋ฆฌ๋ฌธ์„ ์—ฌ๊ธฐ์— ์ž‘์„ฑํ•˜๊ณ  DAO์—์„œ๋Š” ๋ฐ”๋กœ ๋ถˆ๋Ÿฌ์„œ ์‚ฌ์šฉํ•˜๊ธฐ๋งŒ ํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์šฐ์„  ํ‹€๋งŒ ์ž‘์„ฑํ•ด ๋‘ก๋‹ˆ๋‹ค.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="member">
</mapper>

์ด์ œ ์—ฌ๊ธฐ์— ์ž‘์„ฑํ•œ ์ฟผ๋ฆฌ๋ฌธ์„ MyBatis๊ฐ€ ์•Œ ์ˆ˜ ์žˆ๋„๋ก MyBatis ๊ด€๋ จ ํŒจํ‚ค์ง€์— ์žˆ๋Š” config.xml์— ๋“ฑ๋กํ•ด์ค๋‹ˆ๋‹ค.

 

๐Ÿ“œ src/com.app.mybatis.config (package) / config.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "https://mybatis.org/dtd/mybatis-3-config.dtd">

<configuration>
	<settings>
		<setting name="mapUnderscoreToCamelCase" value="true"/>
	</settings>

	<environments default="mysql">
		<environment id="mysql">
			<transactionManager type="JDBC"/>
			<dataSource type="POOLED">
				<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
				<property name="url" value="jdbc:mysql://localhost:3306/app"/>
				<property name="username" value="root"/>
				<property name="password" value="1234"/>
			</dataSource>
		</environment>
	</environments>
    
        <mappers>
                <mapper resource="com/mybatis/mapper/memberMapper.xml"/>
        </mappers>
</configuration>

4. Mapper์—์„œ SQL๋ฌธ ์ž‘์„ฑํ•˜๊ธฐ (1) ํšŒ์›๊ฐ€์ž…์„ ์œ„ํ•œ

๐Ÿ“œ src/com.app.mybatis.mapper (package) / memberMapper.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="member">
	<insert id="insert">
		insert into tbl_member
		(member_identification, member_password, member_name, member_birth, member_phone, member_email)
		values(#{memberIdentification}, #{memberPassword}, #{memberName}, #{memberBirth}, #{memberPhone}, #{memberEmail})
	</insert>
</mapper>

5. DAO ๋งŒ๋“ค์–ด์„œ ์„œ๋น„์Šค ๊ด€๋ จ ๋ฉ”์†Œ๋“œ ์„ ์–ธํ•˜๊ธฐ (1) ํšŒ์›๊ฐ€์ž…์„ ์œ„ํ•œ

๐Ÿ“œ src/com.app.member.dao (package) / MemberDAO.java

package com.app.member.dao;

public class MemberDAO {
    public SqlSession sqlSession;
    
    public MemberDAO() {
        sqlSession = MyBatisConfig.getSqlSessionFactory().openSession(true);
    }
    
    // ํšŒ์›๊ฐ€์ž…
    public void insert(MemberVO memberVO) {
        sqlSession.insert("member.insert", memberVO);
    }
}

6. Controller ๋งŒ๋“ค๊ธฐ with Action ์ธํ„ฐํŽ˜์ด์Šค (1) ํšŒ์›๊ฐ€์ž…์„ ์œ„ํ•œ

Controller ์ด๋ฆ„์˜ ๊ฒฝ์šฐ ์„œ๋น„์Šค๋ช… + (์—ฐ์‚ฐ์ด ํ•„์š”ํ•œ Controller๋ผ๋ฉด Ok) + Controller ๋กœ ์ง“๋Š” ๊ฑธ๋กœ ํ–ˆ์Šต๋‹ˆ๋‹ค.

 

๐Ÿ“œ src/com.app.member.controller (package) / JoinOkController.java

package com.app.member.controller;

public class JoinOkController implements Action {
    @Override
    public Result execute(HttpServletRequest req, HttpServletResponse resp) throw IOException, ServletException {
        MemberDAO memberDAO = new MemberDAO();
        MemberVO memberVO = new MemberVO();
        Result result = new Result();
        
        memberVO.setMemberIdentification(req.getParameter("memberIdentification"));
        memberVO.setMemberPassword(req.getParameter("memberPassword"));
        memberVO.setMemberName(req.getParameter("memberName"));
        memberVO.setMemberBirth(req.getParameter("memberBirth"));
        memberVO.setMemberPhone(req.getParameter("memberPhone"));
        memberVO.setMemberEmail(req.getParameter("memberEmail"));
        
        memberDAO.insert(memberVO);
        
        result.setRedirect(true);
        result.setPath(req.getContextPath() + "/login.member");
    }
}

7. FrontController์—์„œ ์•Œ๋งž์€ Controller์˜ execute()๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค

๊ฐ ํด๋ผ์ด์–ธํŠธ๋“ค์€ Front Controller์—๊ฒŒ ์š”์ฒญ์„ ๋ณด๋‚ด๊ณ , Front Controller๋Š” ๊ฐ ์š”์ฒญ์— ๋งž๋Š” ์ปจํŠธ๋กค๋Ÿฌ๋ฅผ ์ฐพ์•„์„œ ํ˜ธ์ถœ์‹œํ‚ต๋‹ˆ๋‹ค. ๊ณตํ†ต ์ฝ”๋“œ์— ๋Œ€ํ•ด์„œ๋Š” Front Controller์—์„œ ์ฒ˜๋ฆฌํ•˜๊ณ , ์„œ๋กœ ๋‹ค๋ฅธ ์ฝ”๋“œ๋“ค๋งŒ ๊ฐ Controller์—์„œ ์ฒ˜๋ฆฌํ•  ์ˆ˜์žˆ๋„๋ก ํ•ฉ๋‹ˆ๋‹ค.

 

๐Ÿ“œ src/com.app.member (package) / MemberFrontController.java

package com.app.member;

public class MemberFrontController extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        
        String target = req.getRequestURI().replace(req.getContextPath() + "/", "").split("\\.")[0];
        Result result = null;
        
        if(target.equals("joinOk")) {
           result = new JoinOkController().excute(req, resp);
        }
        
        if(result !== null) {
            if(result.isRedirect()) {
                resp.sendRedirect(result.getPath());
            } else {
                req.getRequestDispatcher(result.getPath()).forward(req, resp);
            }
        }
    }
    
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) {
        doGet(req, resp);
    }
}

์—ฌ๊ธฐ๊นŒ์ง€ ํ•˜๋ฉด ํšŒ์›๊ฐ€์ž… ๊ตฌํ˜„์„ ๋์ด๊ณ , ์ด์ œ ์•ž์œผ๋กœ ์„œ๋น„์Šค ํ•˜๋‚˜๋ฅผ ๊ตฌํ˜„ํ•˜๋ ค๊ณ  ํ•˜๋ฉด ๋‹ค์‹œ 4๋กœ ๋Œ์•„๊ฐ€๋ฉด์„œ ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง์„ ์ž‘์„ฑํ•˜๋ฉด ๋˜๊ฒ ์Šต๋‹ˆ๋‹ค :)

4. Mapper์—์„œ SQL๋ฌธ ์ž‘์„ฑํ•˜๊ธฐ (2) ๋กœ๊ทธ์ธ์„ ์œ„ํ•œ (3) ์•„์ด๋”” ์ค‘๋ณต๊ฒ€์‚ฌ๋ฅผ ์œ„ํ•œ

๐Ÿ“œ src/com/app.mybatis.mapper (package) / memberMapper.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="member">
	<insert id="insert">
		insert into tbl_member
		(member_identification, member_password, member_name, member_birth, member_phone, member_email)
		values(#{memberIdentification}, #{memberPassword}, #{memberName}, #{memberBirth}, #{memberPhone}, #{memberEmail})
	</insert>
    
    	<select id="login" resultType="long">
		select member_id from tbl_member 
		where member_identification = #{memberIdentification} and member_password = #{memberPassword}
	</select>
	
	<select id="selectIdentification" resultType="string">
		select member_identification from tbl_member where member_identification = #{memberIdentification}
	</select>
</mapper>

5. DAO ๋งŒ๋“ค์–ด์„œ ์„œ๋น„์Šค ๊ด€๋ จ ๋ฉ”์†Œ๋“œ ์„ ์–ธํ•˜๊ธฐ (2)๋กœ๊ทธ์ธ์„ ์œ„ํ•œ (3) ์•„์ด๋”” ์ค‘๋ณต๊ฒ€์‚ฌ๋ฅผ ์œ„ํ•œ

๐Ÿ“œ src/com.app.member.dao (package) / MemberDAO.java

package com.app.member.dao;

import java.util.HashMap;

import org.apache.ibatis.session.SqlSession;

import com.app.member.domain.MemberVO;
import com.app.mybatis.config.MyBatisConfig;

public class MemberDAO {
	public SqlSession sqlSession;
	
	public MemberDAO() {
		sqlSession = MyBatisConfig.getSqlSessionFactory().openSession(true);
	}
	
//	ํšŒ์›๊ฐ€์ž…
	public void insert(MemberVO memberVO) {
		sqlSession.insert("member.insert", memberVO);
	}
	
//	์•„์ด๋”” ์ค‘๋ณต๊ฒ€์‚ฌ
	public String selectIdentification(String memberIdentification) {
		return sqlSession.selectOne("member.selectIdentification", memberIdentification);
	}
	
//	๋กœ๊ทธ์ธ
	public Long login(String memberIdentification, String memberPassword) {
		HashMap<String, String> loginMap = new HashMap<String, String>();
		loginMap.put("memberIdentification", memberIdentification);
		loginMap.put("memberPassword", memberPassword);
		
		return sqlSession.selectOne("member.login", loginMap);
	}
}

 

6. Controller ๋งŒ๋“ค๊ธฐ (2)๋กœ๊ทธ์ธ์„ ์œ„ํ•œ (3)์•„์ด๋”” ์ค‘๋ณต๊ฒ€์‚ฌ๋ฅผ ์œ„ํ•œ

๐Ÿ“œ src/ com.app.member.controller (package) / LoginOkConroller.java

package com.app.member.controller;

public class LoginOkController implements Action {
    @Override
    public Result execute(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        MemberDAO memberDAO = new MemberDAO();
        Long memberId = memberDAO.login(req.getParameter("memberIdentification"), req.getParameter("memberPassword"));
        HttpSession session = req.getSession();
        Result result = new Result();
        
        result.setRedirect(true);
        
        if(memberId == null) {
            // ๋กœ๊ทธ์ธ ์‹คํŒจ
            result.setPath(req.getContextPath() + "/login.member?login=false");
        } else {
            session.setAttribute("memberId", memberId);
            result.setPath(req.getContextPath() + "/list.board");
        }
        return result;
    }
}

 

๐Ÿ“œ src/ com.app.member.controller (package) / CheckIdOkController.java

package com.app.member.controller;

public class CheckIdOkController implements Action {
    @Override
    public Result execute(HttpServeltRequest req, HttpServletResponse resp) throws IOException, ServletException {
        MemberDAO memberDAO = new MemberDAO();
        String memberIdentification = memberDAO.selectIdentification(req.getParameter("memberIdentification"));
        boolean check = memberIdentification == null;
        
        return null;
    }
}

7. FrontController์—์„œ ์•Œ๋งž์€ Controller์˜ execute()๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค

๐Ÿ“œ src/com.app.member (package) / MemberFrontController.java

package com.app.member;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.app.Result;
import com.app.member.controller.JoinOkController;
import com.app.member.controller.LoginOkController;

public class MemberFrontController extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setCharacterEncoding("UTF-8");
		
		String target = req.getRequestURI().replace(req.getContextPath() + "/", "").split("\\.")[0];
		Result result = null;
		
		if(target.equals("join")){
			result = new Result();
			result.setPath("templates/member/join.jsp");
			
		} else if(target.equals("joinOk")){
			result = new JoinOkController().execute(req, resp);
			
		} else if(target.equals("login")){
			result = new Result();
			result.setPath("templates/member/login.jsp");
			
		} else if(target.equals("loginOk")) {
			result = new LoginOkController().execute(req, resp);
			
		} else if(target.equals("logout")) {
			req.getSession().invalidate();
			result = new Result();
			result.setPath("templates/member/login.jsp");
		}
		
		
		if(result != null) {
			if(result.isRedirect()) {
				resp.sendRedirect(result.getPath());
			}else {
				req.getRequestDispatcher(result.getPath()).forward(req, resp);
			}
		}
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doGet(req, resp);
	}
}

 


์œ„์ฒ˜๋Ÿผ 0, 1, 2, 3๊นŒ์ง€๋Š” ๊ธฐ๋ณธ์„ธํŒ…์œผ๋กœ ํ•ด๋†“๊ณ  ์ƒˆ๋กœ์šด ์„œ๋น„์Šค๊ฐ€ ํ•„์š”ํ•  ๋•Œ๋งˆ๋‹ค 4, 5, 6, 7์„ ๋ฐ˜๋ณตํ•˜๋ฉด ๋˜๊ฒ ์Šต๋‹ˆ๋‹ค :)

728x90
LIST