반응형
1) [DTO] MyMember.java
package xyz.itwill.dto;
//create table mymember(id varchar2(20) primary key, name varchar2(50), phone varchar2(20), email varchar2(100));
/*
이름 널? 유형
----- -------- -------------
ID NOT NULL VARCHAR2(20)
NAME VARCHAR2(50)
PHONE VARCHAR2(20)
EMAIL VARCHAR2(100)
*/
public class MyMember {
private String id;
private String name;
private String phone;
private String email;
public MyMember() {
// TODO Auto-generated constructor stub
}
public String getId() {return id;}
public void setId(String id) {this.id = id;}
public String getName() {return name;}
public void setName(String name) {this.name = name;}
public String getPhone() {return phone;}
public void setPhone(String phone) {this.phone = phone;}
public String getEmail() {return email;
}public void setEmail(String email) {this.email = email;}
}
2) [Mapper] MyMemberMapper.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>">
<!-- XML 기반의 매퍼파일과 Interface 기반의 매퍼파일을 바인딩하여 하나의 매퍼로 동작되도록 설정하는 방법 -->
<!-- => XML 기반의 매퍼파일에서 mapper 엘리먼트의 namespace 속성값과 같은 이름으로 interface 매퍼파일 설정 -->
<!-- => XML 기반의 매퍼파일에서 SQL 명령을 등록한 엘리먼트와 동일한 형식으로 Interface 기반의
매퍼 파일에 추상메소드 작성 -->
<mapper namespace="xyz.itwill.mapper.MyMemberMapper">
<!-- DAO 클래스의 메소드에서 Interface 기반의 매퍼 파일에 추상 메소드를 호출할 경우 동일한
형식의 XML 기반의 매퍼 파일에 엘리먼트에 등록된 SQL 명령을 전달하여 실행 -->
<!-- => 엘리먼트의 id 속성값과 동일한 이름의 추상메소드 선언 -->
<!-- => 엘리먼트의 parameterType 속성값과 동일한 자료형으로 추상메소드의 매개변수를 선언 -->
<!-- => 엘리먼트의 resultType 속성값과 동일한 자료형으로 추상메소드의 반환형을 선언 -->
<insert id="insertMember" parameterType="MyMember">
insert into mymember values (#{id},#{name},#{phone},#{email})
</insert>
<update id="updateMember" parameterType="MyMember">
update mymember set name=#{name}, phone=#{phone}, email=#{email} where id=#{id}
</update>
<delete id="deleteMember" parameterType="string">
delete from mymember where id=#{id}
</delete>
<select id="selectMember" parameterType="string" resultType="MyMember">
select * from mymember where id=#{id}
</select>
<select id="selectMemberList" resultType="MyMember">
select * from mymember order by id
</select>
</mapper>
3) [Mapper] MyMemberMapper.java
package xyz.itwill.mapper;
import java.util.List;
import xyz.itwill.dto.MyMember;
public interface MyMemberMapper {
int insertMember(MyMember member);
int updateMember(MyMember member);
int deleteMember(String id);
MyMember selectMember(String id);
List<MyMember> selectMemberList();
}
4) [DAO] MyMemberDAO.java
package xyz.itwill.dao;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import xyz.itwill.dto.MyMember;
import xyz.itwill.mapper.MyMemberInterfaceMapper;
import xyz.itwill.mapper.MyMemberMapper;
public class MyMemberDAO {
private static MyMemberDAO _dao;
private MyMemberDAO() {
// TODO Auto-generated constructor stub
}
static {
_dao = new MyMemberDAO();
}
public static MyMemberDAO getDAO() {
return _dao;
}
//SqlSessionFactory를 반환하는 메소드
private SqlSessionFactory getSessionFactory() {
String resource = "mybatis-config.xml";
InputStream inputStream = null;
try {
inputStream = Resources.getResourceAsStream(resource);
} catch (IOException e) {
throw new IllegalArgumentException();
}
return new SqlSessionFactoryBuilder().build(inputStream);
}
//회원정보를 전달받아 MYMEMBER 테이블에 삽입하고 삽입행의 갯수를 반환하는 메소드
public int insertMember(MyMember member) {
SqlSession sqlSession = getSessionFactory().openSession(true);
try {
return sqlSession.getMapper(MyMemberMapper.class).insertMember(member);
} finally {
sqlSession.close();
}
}
//회원정보를 전달받아 MYMEMBER 테이블에 저장된 회원정보를 변경하고 변경행의 갯수를 반환하는 메소드
public int updateMember(MyMember member) {
SqlSession sqlSession = getSessionFactory().openSession(true);
try {
return sqlSession.getMapper(MyMemberMapper.class).updateMember(member);
} finally {
sqlSession.close();
}
}
//아이디를 전달받아 MYMEMBER 테이블에 저장된 회원정보를 삭제하고 삭제행의 갯수를 반환하는 메소드
public int deleteMember(String id) {
SqlSession sqlSession = getSessionFactory().openSession(true);
try {
return sqlSession.getMapper(MyMemberMapper.class).deleteMember(id);
} finally {
sqlSession.close();
}
}
//아이디를 전달받아 MYMEMBER 테이블에 저장된 회원정보를 검색하여 반환하는 메소드
public MyMember selectMember(String id) {
SqlSession sqlSession = getSessionFactory().openSession(true);
try {
return sqlSession.getMapper(MyMemberMapper.class).selectMember(id);
} finally {
sqlSession.close();
}
}
//MYMEMBER 테이블에 저장된 모든 회원정보를 검색하여 반환하는 메소드
public List<MyMember> selectMemberList() {
SqlSession sqlSession = getSessionFactory().openSession(true);
try {
return sqlSession.getMapper(MyMemberMapper.class).selectMemberList();
} finally {
sqlSession.close();
}
}
}
5) 출력할 JSP
(1) 회원목록 페이지
memberDisplay.jsp
<%@page import="xyz.itwill.dao.MyMemberDAO"%>
<%@page import="xyz.itwill.dao.MyMemberInterfaceDAO"%>
<%@page import="xyz.itwill.dao.MyMemberXMLDAO"%>
<%@page import="xyz.itwill.dto.MyMember"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
//List<MyMember> memberList = MyMemberXMLDAO.getDAO().selectMemberList();
//List<MyMember> memberList = MyMemberInterfaceDAO.getDAO().selectMemberList();
List<MyMember> memberList = MyMemberDAO.getDAO().selectMemberList();
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "<http://www.w3.org/TR/html4/loose.dtd>">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>MYBATIS</title>
</head>
<body>
<h1 align="center">회원목록</h1>
<hr>
<table align="center" border="1" cellspacing="0" width="300">
<tr>
<th width="140">아이디</th>
<th width="160">이름</th>
</tr>
<!-- 회원목록 출력 => 반복문 시작 -->
<%for(MyMember member:memberList){ %>
<tr align="center">
<td width="140"><%=member.getId() %></td>
<td width="160"><a href="memberView.jsp?id=<%=member.getId() %>"><%=member.getName() %></a></td>
</tr>
<%} %>
<!-- 반복문 종료 -->
<tr align="right">
<td colspan="2">
<button type="button"
onclick="location.href='memberInputForm.jsp';">회원등록</button>
</td>
</tr>
</table>
</body>
</html>
(2) 회원상세 페이지
memberView.jsp
<%@page import="xyz.itwill.dao.MyMemberXMLDAO"%>
<%@page import="xyz.itwill.dto.MyMember"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String id = request.getParameter("id");
MyMember member = MyMemberXMLDAO.getDAO().selectMember(id);
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "<http://www.w3.org/TR/html4/loose.dtd>">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>MYBATIS</title>
</head>
<body>
<h1 align="center">회원정보</h1>
<hr>
<table align="center" border="1" cellspacing="0" width="300">
<tr>
<th width="100">아이디</th>
<td align="center" width="200"><%=member.getId() %></td>
</tr>
<tr>
<th width="100">이름</th>
<td align="center" width="200"><%=member.getName() %></td>
</tr>
<tr>
<th width="100">전화번호</th>
<td align="center" width="200"><%=member.getPhone() %></td>
</tr>
<tr>
<th width="100">이메일</th>
<td align="center" width="200"><%=member.getEmail() %></td>
</tr>
<tr align="right">
<td colspan="2">
<button type="button" onclick="location.href='memberUpdateForm.jsp?id=<%=member.getId() %>';">회원변경</button>
<button type="button" onclick="location.href='memberDeleteAction.jsp?id=<%=member.getId() %>';">회원삭제</button>
<button type="button" onclick="location.href='memberDisplay.jsp';">회원목록</button>
</td>
</tr>
</table>
</body>
</html>
(3) 회원등록 페이지
memberInputForm.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "<http://www.w3.org/TR/html4/loose.dtd>">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>MYBATIS</title>
<script type="text/javascript" src="<http://code.jquery.com/jquery-1.12.4.min.js>"></script>
<script type="text/javascript">
$(document).ready(function() {
$("#id").focus();
$("#submitBtn").click(function() {
if($("#id").val()=="") {
alert("아이디를 입력해 주세요");
$("#id").focus();
return;
}
if($("#name").val()=="") {
alert("이름을 입력해 주세요");
$("#name").focus();
return;
}
if($("#phone").val()=="") {
alert("전화번호를 입력해 주세요");
$("#phone").focus();
return;
}
if($("#email").val()=="") {
alert("이메일을 입력해 주세요");
$("#email").focus();
return;
}
$("#input").attr("method","POST").attr("action"
,"memberInputAction.jsp").submit();
});
$("#resetBtn").click(function() {
$("#input").each(function() {
this.reset();
});
$("#id").focus();
});
$("#displayBtn").click(function() {
location.href="memberDisplay.jsp";
});
});
</script>
</head>
<body>
<h1 align="center">회원등록</h1>
<form id="input">
<table align="center" border="1" cellspacing="0" width="300">
<tr>
<th width="100">아이디</th>
<td align="center" width="200">
<input type="text" name="id" id="id">
</td>
</tr>
<tr>
<th width="100">이름</th>
<td align="center" width="200">
<input type="text" name="name" id="name">
</td>
</tr>
<tr>
<th width="100">전화번호</th>
<td align="center" width="200">
<input type="text" name="phone" id="phone">
</td>
</tr>
<tr>
<th width="100">이메일</th>
<td align="center" width="200">
<input type="text" name="email" id="email">
</td>
</tr>
<tr>
<th colspan="2">
<button type="button" id="submitBtn">입력전송</button>
<button type="button" id="resetBtn">다시입력</button>
<button type="button" id="displayBtn">회원정보</button>
</th>
</tr>
</table>
</form>
</body>
</html>
(4) 회원등록 처리
memberInputAction.jsp
- 등록성공 시
- 등록실패 시
<%@page import="xyz.itwill.dao.MyMemberXMLDAO"%>
<%@page import="xyz.itwill.dto.MyMember"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
request.setCharacterEncoding("utf-8");
String id = request.getParameter("id");
String name = request.getParameter("name");
String phone = request.getParameter("phone");
String email = request.getParameter("email");
MyMember member = new MyMember();
member.setId(id);
member.setName(name);
member.setPhone(phone);
member.setEmail(email);
try{
//SqlSession 객체를 이용하여 매퍼에 등록된 SQL명령을 전달하여 실행한 경우 문제가
//발생되면 PersistenceException 발생 - 예외처리
//=> 원래 예외는 Service클래스에서 잡는 것이 좋음(model2 기반으로 만들 경우)
MyMemberXMLDAO.getDAO().insertMember(member);
response.sendRedirect("memberDisplay.jsp");
} catch (Exception e){
//response.sendRedirect("memberInputForm.jsp");
out.println("<script type='text/javascript'>");
out.println("alert('이미 사용 중인 아이디를 입력하여 회원등록이 실패 하였습니다.');");
//자바스크립트의 History객체 : 기존의 URL을 저장하는 객체
//=>자바스크립트의 History.go(Number) : Number만큼 이동 -1: 한번뒤로가기 -2:두번뒤로가기 1:한번앞으로가기
out.println("history.back();"); //out.println("history.go(-1);");
out.println("</script>");
}
%>
(5) 회원변경 페이지
memberUpdateForm.jsp
<%@page import="xyz.itwill.dao.MyMemberXMLDAO"%>
<%@page import="xyz.itwill.dto.MyMember"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String id = request.getParameter("id");
MyMember member = MyMemberXMLDAO.getDAO().selectMember(id);
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "<http://www.w3.org/TR/html4/loose.dtd>">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>MYBATIS</title>
<script type="text/javascript" src="<http://code.jquery.com/jquery-1.12.4.min.js>"></script>
<script type="text/javascript">
$(document).ready(function() {
$("#id").focus();
$("#submitBtn").click(function() {
if($("#name").val()=="") {
alert("이름을 입력해 주세요");
$("#name").focus();
return;
}
if($("#phone").val()=="") {
alert("전화번호를 입력해 주세요");
$("#phone").focus();
return;
}
if($("#email").val()=="") {
alert("이메일을 입력해 주세요");
$("#email").focus();
return;
}
$("#input").attr("method","POST").attr("action"
,"memberUpdateAction.jsp").submit();
});
$("#resetBtn").click(function() {
$("#input").each(function() {
this.reset();
});
$("#id").focus();
});
$("#displayBtn").click(function() {
location.href="memberDisplay.jsp";
});
});
</script>
</head>
<body>
<h1 align="center">회원변경</h1>
<form id="input">
<input type="hidden" name="id" value="<%=member.getId()%>">
<table align="center" border="1" cellspacing="0" width="300">
<tr>
<th width="100">아이디</th>
<td align="center" width="200">
<%=member.getId() %>
</td>
</tr>
<tr>
<th width="100">이름</th>
<td align="center" width="200">
<input type="text" name="name" id="name" value="<%=member.getName() %>">
</td>
</tr>
<tr>
<th width="100">전화번호</th>
<td align="center" width="200">
<input type="text" name="phone" id="phone" value="<%=member.getPhone() %>">
</td>
</tr>
<tr>
<th width="100">이메일</th>
<td align="center" width="200">
<input type="text" name="email" id="email" value="<%=member.getEmail() %>">
</td>
</tr>
<tr>
<th colspan="2">
<button type="button" id="submitBtn">변경전송</button>
<button type="button" id="resetBtn">다시입력</button>
<button type="button" id="displayBtn">회원정보</button>
</th>
</tr>
</table>
</form>
</body>
</html>
(6) 회원변경 처리
memberUpdateAction.jsp
<%@page import="xyz.itwill.dao.MyMemberXMLDAO"%>
<%@page import="xyz.itwill.dto.MyMember"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
request.setCharacterEncoding("utf-8");
String id = request.getParameter("id");
String name = request.getParameter("name");
String phone = request.getParameter("phone");
String email = request.getParameter("email");
MyMember member = new MyMember();
member.setId(id);
member.setName(name);
member.setPhone(phone);
member.setEmail(email);
MyMemberXMLDAO.getDAO().updateMember(member);
response.sendRedirect("memberDisplay.jsp");
%>
(7) 회원삭제 처리
memberDeleteAction.jsp
<%@page import="xyz.itwill.dao.MyMemberXMLDAO"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String id = request.getParameter("id");
MyMemberXMLDAO.getDAO().deleteMember(id);
response.sendRedirect("memberDisplay.jsp");
%>
반응형
'framework > mybatis' 카테고리의 다른 글
[mybatis] 8. XML+INTERFACE 매퍼(매퍼바인딩) 2 (MYUSER, MYCOMMENT, MYREPLY) (0) | 2024.07.21 |
---|---|
[mybatis] 7. XML+INTERFACE 매퍼(매퍼바인딩) 1 (MYUSER) (1) | 2024.07.21 |
[mybatis] 5. 인터페이스 기반의 매퍼파일 (MyMember 출력 프로그램) (1) | 2024.07.20 |
[mybatis] 4. XML기반의 매퍼파일 (MyMember 출력 프로그램) (0) | 2024.07.20 |
[mybatis] 3. XML기반의 매퍼파일 (Student 출력 프로그램) (0) | 2024.07.20 |