iBatis + Spring을 이용한 미니 방명록 작성하기
------------------------------------------------------------------------
1. Eclipse에서 ibatistest 라는 톰캣 프로젝트 생성
-------------------------------------------------------------------------
------------------------------------------------------------------------
2. iBatis 설치
------------------------------------------------------------------------
http://ibatis.apache.org/javadownloads.cgi
lib아래 ibatis-2.3.0.677.jar 파일을 WEB-INF/lib에 위치
------------------------------------------------------------------------
3. 기타 jar 파일들 (log4j, commons 관련, spring framework 관련)은 아래와 같다. 적절히 추가하자.
------------------------------------------------------------------------
commons-collections-3.2.jar
commons-dbcp-1.2.1.jar
commons-dbcp-1.2.1.jar
commons-logging.jar
commons-pool-1.3.jar
jstl.jar
log4j-1.2.9.jar
ojdbc14.jar
spring.jar
spring-webmvc.jar
------------------------------------------------------------------------
4. WEB-INF/web.xml
------------------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<servlet>
<servlet-name>guestbook</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>guestbook</servlet-name>
<url-pattern>*.guest</url-pattern>
</servlet-mapping>
</web-app>
------------------------------------------------------------------------
5. /WEB-INF/guestbook-servlet.xml
------------------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean name="questbookController" class="guestbook.controller.GuestbookController">
<property name="guestbookManager" ref="guestbookManager" />
</bean>
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/list.guest">questbookController</prop>
</props>
</property>
</bean>
</beans>
------------------------------------------------------------------------
6. applicationContext.xml
------------------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="/WEB-INF/SqlMapConfig.xml"/>
</bean>
<bean id="guestbookDao" class="guestbook.dao.GuestbookDaoImpl">
<property name="sqlMapClient" ref="sqlMapClient" />
</bean>
<bean name="guestbookManager" class="guestbook.service.GuestbookManagerImpl">
<property name="guestbookDao" ref="guestbookDao" />
</bean>
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName">
<value>oracle.jdbc.driver.OracleDriver</value>
</property>
<property name="url">
<value>jdbc:oracle:thin:@203.236.213.72:1521:dadamita</value>
</property>
<property name="username">
<value>scott</value>
</property>
<property name="password">
<value>tiger</value>
</property>
</bean>
</beans>
------------------------------------------------------------------------
7. log4j.xml
------------------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%p - %m%n"/>
</layout>
</appender>
</log4j:configuration>
------------------------------------------------------------------------
8. 실습용 테이블 작성
------------------------------------------------------------------------
Oracle에서 scott/tiger로 로그인 후
SQL> create table guestbook (
no number constraints pk_guestbook_no primary key,
password varchar2(10),
name varchar2(20),
subject varchar2(100),
content varchar2(500),
write_date varchar2(20)
);
테이블 생성할 때 PK이름 위에서 처럼 주세요… 나중에 SEELCT에서 그 이름으로 힌트 사용합니다.
실습을 위해 데이터 한 건 입력하시구요…
SQL> insert into guestbook values (1,'1111','이종철','연습','연습입니다.',to_char(sysdate,'yyyymmdd'));
SQL> Commit;
------------------------------------------------------------------------
9. WEB-INF/src/SqlMapConfig.xml
------------------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMapConfig PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-config-2.dtd">
<sqlMapConfig>
<typeAlias alias="GuestbookVo" type="guestbook.vo.GuestbookVo" />
<sqlMap resource="guestbook.xml" />
</sqlMapConfig>
------------------------------------------------------------------------
10. /WEB-INF/src/guestbook/vo/GuestbookVo.java
------------------------------------------------------------------------
package guestbook.vo;
public class GuestbookVo {
private int no;
private String password;
private String name;
private String subject;
private String content;
private String write_date;
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getNo() {
return no;
}
public void setNo(int no) {
this.no = no;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
public String getWrite_date() {
return write_date;
}
public void setWrite_date(String write_date) {
this.write_date = write_date;
}
}
------------------------------------------------------------------------
11. /WEB-INF/src/guestbook/dao/GuestbookDao.java
------------------------------------------------------------------------
package guestbook.dao;
import java.util.List;
public interface GuestbookDao {
public List getContents();
}
------------------------------------------------------------------------
12. /WEB-INF/src/guestbook/dao/GuestbookDaoImpl.java
------------------------------------------------------------------------
package guestbook.dao;
import guestbook.vo.GuestbookVo;
import java.util.List;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;
public class GuestbookDaoImpl extends SqlMapClientDaoSupport implements GuestbookDao {
public List getContents() {
return (List) getSqlMapClientTemplate().queryForList("getContents");
}
}
------------------------------------------------------------------------
13. /WEB-INF/src/guestbook.sql을 작성
------------------------------------------------------------------------
column을 java에 있는 bean에 매핑하기 위해서 만드는 데 getContents는 Dao에서 호출하는 이름과 같아야 한다.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-2.dtd">
<sqlMap namespace="Guestbook">
<resultMap id="resultGuestbook" class="GuestbookVo">
<result property="no" column="no" />
<result property="name" column="name" />
<result property="subject" column="subject" />
<result property="content" column="content" />
<result property="write_date" column="write_date" />
</resultMap>
<select id="getContents" resultMap="resultGuestbook">
SELECT /*+ index_desc(guestbook pk_guestbook_no) */
no, name, subject, content, write_date
FROM guestbook
WHERE no is not null
</select>
</sqlMap>
------------------------------------------------------------------------
14. /WEB-INF/src/guestbook/service/GuestbookManager.java 작성
------------------------------------------------------------------------
package guestbook.service;
import guestbook.vo.GuestbookVo;
import java.util.List;
public interface GuestbookManager {
public List getContents();
}
------------------------------------------------------------------------
15. /WEB-INF/src/guestbook/service/GuestbookManager.Impl.java 작성
------------------------------------------------------------------------
package guestbook.service;
import guestbook.dao.GuestbookDao;
import guestbook.vo.GuestbookVo;
import java.util.List;
public class GuestbookManagerImpl implements GuestbookManager{
GuestbookDao guestbookDao;
public void setGuestbookDao(GuestbookDao guestbookDao) {
this.guestbookDao = guestbookDao;
}
public List getContents() {
return guestbookDao.getContents();
}
}
------------------------------------------------------------------------
16. 이번에는 Controller를 작성한다.
------------------------------------------------------------------------
/WEB-INF/src/guestbook/controller/GuestBookController.java
GuestbookManager를 통해 Database에서 데이터를 가져와 ModelAndView를 리턴 하는데 contentslist 라는 객체를 list.jsp로 넘겨주게 된다.
package guestbook.controller;
import guestbook.service.GuestbookManager;
import guestbook.vo.GuestbookVo;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
public class GuestbookController implements Controller {
private GuestbookManager guestbookManager;
public void setGuestbookManager(GuestbookManager guestbookManager) {
this.guestbookManager = guestbookManager;
}
public ModelAndView handleRequest(HttpServletRequest arg0, HttpServletResponse arg1)
throws Exception {
List contentslist = guestbookManager.getContents();
return new ModelAndView("list", "contentslist", contentslist);
}
}
------------------------------------------------------------------------
17. list.jsp
------------------------------------------------------------------------
Tomcat 5.5 아래()Tomcat 5.0) 에서 JSTL을 사용하실 때 1번과 같은 방법으로 Taglib을 기술했지만 5.5이상에서는 2번과 같은 방법으로 JSTL Taglib을 사용해야 합니다.
1. <%@ uri="http://java.sun.com/jstl/core" prefix="c" %>
2. <%@ uri="http://java.sun.com/jstl/core_rt" prefix="c" %>
[ibatistest/list.j네]
<%@ page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c" %>
<c:forEach var="contentslist" items="${contentslist}">
<div>
<p>이름 : ${contentslist.name}</p>
<p>제목 : ${contentslist.subject}</p>
<p>내용 : ${contentslist.content}</p>
<p>작성일 : ${contentslist.write_date}</p>
</div>
<hr />
</c:forEach>
------------------------------------------------------------------------
18. [결과확인]http://localhost/ibatistest/list.guest
------------------------------------------------------------------------
이름 : 이종철
제목 : 연습
내용 : 연습입니다.
작성일 : 20080511
댓글 없음:
댓글 쓰기