๋ง์ด๋ฐํฐ์ค๋ ์คํ๋ง์์ ๋ฐ์ดํฐ๋ฒ ์ด์ค ์ฐ๋์ ๋์์ฃผ๋ ํ๋ ์์ํฌ์ ๋๋ค.
๊ธฐ์กด์๋ JDBC ์ฐ๋ ๊ณผ์ ๋ ๋ณต์กํ๊ณ SQL๋ฌธ์ด ์ฝ๋๋ด์ ์์ฌ ์ฝ๋๊ฐ ์๋นํ ๋ณต์กํด์ก์ต๋๋ค. ๋ ๊ทธ๋ ๋ฏ ํ๋ ์์ํฌ๋ ํธ์๋ฅผ ์ํด ๋ง๋ค์ด์ง๋๋ค. ๋ง์ด๋ฐํฐ์ค๋ ๊ฒฐ๊ตญ ๊ฐ๋ฐ์๋ค์ ํธ์์ฑ์ ์ํ์ฌ ๋ง๋ค์ด์ก์ต๋๋ค.
๋ง์ด๋ฐํฐ์ค์ ํน์ง
- sql ์คํ ๊ฒฐ๊ณผ๋ฅผ Map ๊ฐ์ฒด์ ๋งคํํด์ค๋๋ค.
- sql์ ์์ค ์ฝ๋๊ฐ ์๋ XML๋ก ๋ถ๋ฆฌํ์ฌ ์์ฑํฉ๋๋ค.
- ๋ฐ์ดํฐ์์ค ๊ธฐ๋ฅ๊ณผ ํธ๋์ญ์ ์ฒ๋ฆฌ ๊ธฐ๋ฅ์ ์ ๊ณตํด์ค๋๋ค.
๊ทธ๋ผ ๊ฐ๋จํ ๋ง์ด๋ฐํฐ์ค๋ฅผ ์ํ ๊ฐ๋ฐํ๊ฒฝ์ ๊ตฌ์ถํ ํ ๊ฐ๋จํ sql๋ฌธ์ ์คํํ์ฌ ๊ฒฐ๊ณผ๊ฐ์ jsp์ ๊ฐ์ ธ์๋ณด๊ฒ ์ต๋๋ค.
DBMS๋ ์ค๋ผํด์ ์ฌ์ฉํ์์ต๋๋ค.
l ๋ง์ด๋ฐํฐ์ค ์ค์ ์ ์ํ xml ํ์ผ๋ค
<?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 ๊ฒฐ๊ณผ
์ฝ์ด์ฃผ์ ์ ๊ฐ์ฌํฉ๋๋ค.
์ง๋ฌธ์ ์ธ์ ๋ ํ์ํฉ๋๋ค.
"๋ ๋ฐ๋์ ๋ฐฑ์๋ ์์ด ๋ ๊ฑฐ์ผ"
'...' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Java] null ๊ณผ "" ์ฐจ์ด๋ ๋ญ๊น? (0) | 2021.01.07 |
---|---|
[JS] javascript:void(0) ์ฌ์ฉํ๋ ์ด์ (0) | 2021.01.07 |
[Spring] ๊ด์ ์งํฅ ํ๋ก๊ทธ๋จ(AOP) ์ดํดํ๊ธฐ (0) | 2021.01.03 |
[Spring] ์คํ๋ง DI, IoC ์ดํดํ๊ธฐ (4) | 2021.01.02 |
[Java] equals() == ์ฐจ์ด ์ฝ๊ฒ ์ดํดํ๊ธฐ (2) | 2020.12.31 |