๋กœ์ผ“๐Ÿพ
article thumbnail

 

๋งˆ์ด๋ฐ”ํ‹ฐ์Šค๋Š” ์Šคํ”„๋ง์—์„œ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์—ฐ๋™์„ ๋„์™€์ฃผ๋Š” ํ”„๋ ˆ์ž„์›Œํฌ์ž…๋‹ˆ๋‹ค.

 

๊ธฐ์กด JDBC

 

๊ธฐ์กด์—๋Š” JDBC ์—ฐ๋™ ๊ณผ์ •๋„ ๋ณต์žกํ•˜๊ณ  SQL๋ฌธ์ด ์ฝ”๋“œ๋‚ด์— ์„ž์—ฌ ์ฝ”๋“œ๊ฐ€ ์ƒ๋‹นํžˆ ๋ณต์žกํ•ด์กŒ์Šต๋‹ˆ๋‹ค. ๋Š˜ ๊ทธ๋ ‡๋“ฏ ํ”„๋ ˆ์ž„์›Œํฌ๋Š” ํŽธ์˜๋ฅผ ์œ„ํ•ด ๋งŒ๋“ค์–ด์ง‘๋‹ˆ๋‹ค. ๋งˆ์ด๋ฐ”ํ‹ฐ์Šค๋„ ๊ฒฐ๊ตญ ๊ฐœ๋ฐœ์ž๋“ค์˜ ํŽธ์˜์„ฑ์„ ์œ„ํ•˜์—ฌ ๋งŒ๋“ค์–ด์กŒ์Šต๋‹ˆ๋‹ค.

 

๋งˆ์ด๋ฐ”ํ‹ฐ์Šค์˜ ํŠน์ง•

  • sql ์‹คํ–‰ ๊ฒฐ๊ณผ๋ฅผ Map ๊ฐ์ฒด์— ๋งคํ•‘ํ•ด์ค๋‹ˆ๋‹ค.
  • sql์„ ์†Œ์Šค ์ฝ”๋“œ๊ฐ€ ์•„๋‹Œ XML๋กœ ๋ถ„๋ฆฌํ•˜์—ฌ ์ž‘์„ฑํ•ฉ๋‹ˆ๋‹ค.
  • ๋ฐ์ดํ„ฐ์†Œ์Šค ๊ธฐ๋Šฅ๊ณผ ํŠธ๋žœ์žญ์…˜ ์ฒ˜๋ฆฌ ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•ด์ค๋‹ˆ๋‹ค.

 

๊ทธ๋Ÿผ ๊ฐ„๋‹จํžˆ ๋งˆ์ด๋ฐ”ํ‹ฐ์Šค๋ฅผ ์œ„ํ•œ ๊ฐœ๋ฐœํ™˜๊ฒฝ์„ ๊ตฌ์ถ•ํ•œ ํ›„ ๊ฐ„๋‹จํ•œ sql๋ฌธ์„ ์‹คํ–‰ํ•˜์—ฌ ๊ฒฐ๊ณผ๊ฐ’์„ jsp์— ๊ฐ€์ ธ์™€๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

DBMS๋Š” ์˜ค๋ผํด์„ ์‚ฌ์šฉํ•˜์˜€์Šต๋‹ˆ๋‹ค.

 


l ๋งˆ์ด๋ฐ”ํ‹ฐ์Šค ์„ค์ •์„ ์œ„ํ•œ xml ํŒŒ์ผ๋“ค

 

๋ฐ˜๋“œ์‹œ src์— ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

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


<configuration>
	<!-- DB๋กœ ์ „๋‹ฌํ•˜๊ฑฐ๋‚˜ ์ „๋‹ฌ ๋ฐ›์„ ๋•Œ์˜ ๋ ˆ์ฝ”๋“œ ํ˜•ํƒœ -->
	<typeAliases>
    	<!-- ๋ ˆ์ฝ”๋“œ์˜ ํ˜•ํƒœ๋ฅผ memberVO๋กœ ํ•ฉ๋‹ˆ๋‹ค. -->
		<typeAlias type="com.spring.ex01.MemberVO" alias="memberVO"></typeAlias>
	</typeAliases>
	
	<environments default="development">
		<environment id="development">
			<transactionManager type="JDBC"></transactionManager>
			<dataSource type="POOLED">
				<property name="driver" value="oracle.jdbc.driver.OracleDriver"></property>
				<property name="url" value="JDBC:oracle:thin:@localhost:1521:XE"></property>
				<property name="username" value="๊ณ„์ •๋ช…"></property>
				<property name="password" value="๋น„๋ฐ€๋ฒˆํ˜ธ"></property>
			</dataSource>
		</environment>
	</environments>
	
	<!-- sql๋ฌธ์ด ์žˆ๋Š” xml์„ ์ฝ์–ด๋“ค์ž…๋‹ˆ๋‹ค. ํŒจํ‚ค์ง€ ์ด๋ฆ„ ์ „๋ถ€ ์ž‘์„ฑํ•ด์ค˜์•ผ ํ•ฉ๋‹ˆ๋‹ค. -->
	<mappers>
		<mapper resource="mybatis/mappers/member.xml"></mapper>
	</mappers>
</configuration>



 

SqlMapConfig.xml ์„ ์ด์šฉํ•˜์—ฌ ๋งˆ์ด๋ฐ”ํ‹ฐ์Šค ์„ค์ •์„ ํ•ด์ค๋‹ˆ๋‹ค. 

 

<typeAlias>๋ฅผ ์ด์šฉํ•˜์—ฌ ๋ ˆ์ฝ”๋“œ์˜ ํ˜•ํƒœ๋ฅผ ์ง€์ •ํ•ด ์ค„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ <mappers> ํƒœ๊ทธ๋ฅผ ์ด์šฉํ•˜์—ฌ SqlMapConfig.xml์—์„œ sql๋ฌธ์ด ์ •์˜๋˜์–ด์žˆ๋Š” 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="mapper.member">
	<!-- typeAlias์—์„œ ์ง€์ •ํ•œ memberVO ๋นˆ์— ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. -->
    <!-- type="java.util.HashMap" ์œผ๋กœ ๋ฐ›์„์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. -->
	<resultMap id="memResult" type="memberVO">
		<!-- column์— memberVO์™€ ๊ฐ™์€ ์†์„ฑ ๊ฐ’์„ ๋„ฃ์–ด์ค๋‹ˆ๋‹ค. -->
		<result property="id" column="id"></result>
		<result property="pwd" column="pwd"></result>
		<result property="name" column="name"></result>
		<result property="email" column="email"></result>
		<result property="joinDate" column="joinDate"></result>
	</resultMap>
	
	<!-- ๋ฐ˜ํ™˜๋˜๋Š” ๊ฐ’์„ memResult์— ๋‹ด์Šต๋‹ˆ๋‹ค. -->
	<select id="selectAllMemberList" resultMap="memResult">
		<!-- <![CDATA[...]] ์ฟผ๋ฆฌ๋ฌธ์˜ >,< ๋“ฑ์„ ๊ตฌ๋ถ„ํ•ด์ฃผ๊ธฐ ์œ„ํ•ด-->
		<![CDATA[
			SELECT 
				  ID
				, PW AS pwd
				, NAME
				, EMAIL
				, JOINDATE
			FROM TEST_TABLE
		]]>
	</select>
</mapper>

 

SqlMapConfig.xml์˜ mappers์—์„œ ์„ค์ •ํ•œ member.xml ์ž…๋‹ˆ๋‹ค. <![CDATA[]] >๋Š” >,<,>=,<= ๊ฐ™์€ ํŠน์ˆ˜๋ฌธ์ž๋ฅผ ์ธ์‹ํ•˜์ง€ ๋ชปํ• ์ˆ˜๋„ ์žˆ์–ด์„œ ์ด๋ฅผ ๋ฐฉ์ง€ํ•˜๊ณ ์ž ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

<resultMap>์—์„œ ๊ฐ ์†์„ฑ๋“ค์„ ์ง€์ •ํ•ด์ค๋‹ˆ๋‹ค. ๋ฐ˜๋“œ์‹œ memberVO์— ๋Œ€ํ•œ setter/getter๊ฐ€ ์กด์žฌํ•˜์—ฌ์•ผ ํ•ฉ๋‹ˆ๋‹ค.

 

<select> ๊ฒฐ๊ณผ๋ฅผ resultMap์— ๋‹ด์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ <select>ํƒœ๊ทธ์˜ id๋ฅผ ์ด์šฉํ•˜์—ฌ DAO์—์„œ sql๋ฌธ์„ ํ˜ธ์ถœํ•ฉ๋‹ˆ๋‹ค.

 

resultMap์„ ์‚ฌ์šฉํ•˜๊ธฐ ์‹ซ๋‹ค๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.

 

<!-- ๋„ค์ž„์ŠคํŽ˜์ด์Šค๋ฅผ ์ •ํ•ด์ค๋‹ˆ๋‹ค. ๋„ค์ž„์ŠคํŽ˜์ด์Šค๋Š” ์ค‘๋ณต๋˜๋ฉด ์•ˆ๋ฉ๋‹ˆ๋‹ค. -->
<mapper namespace="mapper.member">
	<!-- typeAlias์—์„œ ์ง€์ •ํ•œ memberVO ๋นˆ์— ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. -->
	<resultMap id="memResult" type="memberVO">
		<!-- column์— memberVO์™€ ๊ฐ™์€ ์†์„ฑ ๊ฐ’์„ ๋„ฃ์–ด์ค๋‹ˆ๋‹ค. -->
		<result property="id" column="id"></result>
		<result property="pwd" column="pwd"></result>
		<result property="name" column="name"></result>
		<result property="email" column="email"></result>
		<result property="joinDate" column="joinDate"></result>
	</resultMap>
	
	<!-- ๋ฐ˜ํ™˜๋˜๋Š” ๊ฐ’์„ memResult์— ๋‹ด์Šต๋‹ˆ๋‹ค. -->
	<select id="selectAllMemberList" resultType="memberVO">
		<!-- <![CDATA[...]] ์ฟผ๋ฆฌ๋ฌธ์˜ >,< ๋“ฑ์„ ๊ตฌ๋ถ„ํ•ด์ฃผ๊ธฐ ์œ„ํ•ด-->
		<![CDATA[
			SELECT 
				  ID
				, PW AS pwd
				, NAME
				, EMAIL
				, JOINDATE
			FROM TEST_TABLE
		]]>
	</select>
</mapper>

 

<select> ํƒœ๊ทธ์—์„œ resultType๊ณผ resultMap์€ ๋‹ค๋ฆ…๋‹ˆ๋‹ค. resultMap์€ <resultMap> ํƒœ๊ทธ๋ฅผ ์ด์šฉํ•˜์—ฌ ์œ„์— ์„ ์–ธํ•ด์ฃผ์–ด์•ผ ํ•˜์ง€๋งŒ resultType์€ ๊ทธ๋Ÿด ํ•„์š”๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค.

 

package com.spring.ex01;

import java.sql.Date;

public class MemberVO {
	private String id;
	private String pwd;
	private String name;
	private String email;
	private Date joinDate;

	public MemberVO() {
		
	}

	public MemberVO(String id, String pwd, String name, String email) {
		this.id = id;
		this.pwd = pwd;
		this.name = name;
		this.email = email;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getPwd() {
		return pwd;
	}

	public void setPwd(String pwd) {
		this.pwd = pwd;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public Date getJoinDate() {
		return joinDate;
	}

	public void setJoinDate(Date joinDate) {
		this.joinDate = joinDate;
	}

}

 

MemberVO ์ž…๋‹ˆ๋‹ค. 

 

package com.spring.ex01;

import java.io.IOException;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/mem.do")
public class MemberServlet extends HttpServlet {
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doHandle(request, response);

	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doHandle(request, response);
	}

	private void doHandle(HttpServletRequest request, HttpServletResponse response)	throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		MemberDAO dao = new MemberDAO();
		List<MemberVO> membersList = dao.selectAllMemberList();
		
		request.setAttribute("membersList", membersList);
		RequestDispatcher dispatch = request.getRequestDispatcher("test01/listMembers.jsp");
		dispatch.forward(request, response);
	}
}

 

List<MemberVO> membersList = dao.selectAllMemberList(); ๋ฅผ ์ด์šฉํ•˜์—ฌ dao์— ์ ‘๊ทผํ•ฉ๋‹ˆ๋‹ค.

 

package com.spring.ex01;

import java.io.Reader;
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;

public class MemberDAO {
	public static SqlSessionFactory sqlMapper = null;

	private static SqlSessionFactory getInstance() {
		if (sqlMapper == null) {
			try {
				String resource = "mybatis/SqlMapConfig.xml";
				Reader reader = Resources.getResourceAsReader(resource);
				sqlMapper = new SqlSessionFactoryBuilder().build(reader);
				reader.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return sqlMapper;

	}

	public List<MemberVO> selectAllMemberList() {
		sqlMapper = getInstance();
		SqlSession session = sqlMapper.openSession();
		List<MemberVO> memlist = null;
		memlist = session.selectList("mapper.member.selectAllMemberList");
		return memlist;
	}
}

 

session.selectList("mapper.member.selectAllMemberList"); ๋ฅผ ์ด์šฉํ•˜์—ฌ mapper.member์™€ ์—ฐ๋™์„ ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

 

member.xml์—์„œ์˜ namespace์ธ mapper.member์™€ <select>ํƒœ๊ทธ์˜ id์ธ selectAllMemberList๋ฅผ ํ•ฉ์ณ์„œ ์—ฐ๋™ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" 
    isELIgnored="false"  %>
 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"  %>
<c:set var="contextPath"  value="${pageContext.request.contextPath}"  />

<%
  request.setCharacterEncoding("UTF-8");
%>    


<html>
<head>
<meta charset=UTF-8">
<title>ํšŒ์› ์ •๋ณด ์ถœ๋ ฅ์ฐฝ</title>
</head>
<body>
<table border="1"  align="center"  width="80%">
    <tr align="center"   bgcolor="lightgreen">
      <td ><b>์•„์ด๋””</b></td>
      <td><b>๋น„๋ฐ€๋ฒˆํ˜ธ</b></td>
      <td><b>์ด๋ฆ„</b></td>
      <td><b>์ด๋ฉ”์ผ</b></td>
      <td><b>๊ฐ€์ž…์ผ</b></td>
   </tr>
   
 <c:forEach var="member" items="${membersList}" >     
   <tr align="center">
      <td>${member.id}</td>
      <td>${member.pwd}</td>
      <td>${member.name}</td>
      <td>${member.email}</td>
      <td>${member.joinDate}</td>
      
    </tr>
  </c:forEach>   
</table>
<a  href="${contextPath}/member/memberForm.do"><h1 style="text-align:center">ํšŒ์›๊ฐ€์ž…</h1></a>
</body>
</html>

 

jsp์ž…๋‹ˆ๋‹ค.

 


l ๊ฒฐ๊ณผ

 

jsp์—์„œ์˜ ์ถœ๋ ฅ ๊ฒฐ๊ณผ

 

memlist์•ˆ์— ์žˆ๋Š” ๋ฐ์ดํ„ฐ ๊ฐ’

 

 

 

 

 

์ฝ์–ด์ฃผ์…”์„œ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค.

์งˆ๋ฌธ์€ ์–ธ์ œ๋‚˜ ํ™˜์˜ํ•ฉ๋‹ˆ๋‹ค.

 

"๋‚œ ๋ฐ˜๋“œ์‹œ ๋ฐฑ์—”๋“œ ์™•์ด ๋ ๊ฑฐ์•ผ"

profile on loading

Loading...