by 이동국(fromm0골뱅이gmail.com) - http://cafe.naver.com/deve.cafe

<div class="note"> 이 문서는 개인적으로 Spring(ver1.1.4)과 iBATIS SQLMaps(ver 2.0.9b)를 연동하면서 셋팅했던 파일들에 대한 간단히 정리하는 문서입니다. 현재 작업중이고 언제든지 내용이 변경될수 있습니다. 문제가 될수 있는 부분에 대해서의 조언을 환영합니다. </div>

web.xml#

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" 
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/applicationContext-ibatis.xml</param-value>    
  </context-param>

...........

</web-app>

spring과 ibatis연동설정이 위치하는 xml파일의 위치를 지정한다.

applicationContext-ibatis.xml#

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

<beans>
  <!-- iBATIS SQLMaps의 설정파일 위치를 지정한다. 
  
  class값은 
  SQLMaps 1.x버전을 사용할때는 org.springframework.orm.ibatis.SqlMapFactoryBean
  SQLMaps 2.x버전을 사용할때는 org.springframework.orm.ibatis.SqlMapClientFactoryBean 를 사용한다.
  -->
  <bean id="sqlMap" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
    <property name="configLocation"><value>WEB-INF/sqlmap-config.xml</value></property>
  </bean>

  <!-- dataSource를 사용하는것에 대한 정보를 나타낸다. 
  여기서 사용될수 있는 dataSource타입은 다른 문서를 참조하길 바란다. 
  
  여기선 apache의 DBCP Connection pooling을 사용하는 것이다. -->
  <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName"><value>com.mysql.jdbc.Driver</value></property>
    <property name="url"><value><![CDATA[jdbc:mysql://localhost/test?useUnicode=true&characterEncoding=euckr]]></value></property>
    <property name="username"><value>fromm0</value></property>
    <property name="password"><value>passwd</value></property>
  </bean>
  
  <!-- DB연결에 관련된 설정을 DataSource형태로 지정을 했기 때문에 트랜잭션 관리를 
  org.springframework.jdbc.datasource.DataSourceTransactionManager 가 담당하도록 지정한다. -->
  <bean id="myTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
      <property name="dataSource"><ref local="dataSource"/></property>
  </bean>  
  
  <!-- 각각의 메소드 별로 트랜잭션관리 속성을 지정한다. -->
  <bean id="guestService" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">  
    <property name="transactionManager"><ref local="myTransactionManager"/></property>
    <property name="target"><ref local="guestTarget"/></property>
    <property name="transactionAttributes">
      <props>
        <prop key="get*">PROPAGATION_REQUIRED,readOnly,-OrderException</prop>
        <prop key="add*">PROPAGATION_REQUIRED,-OrderException,-OrderMinimumAmountException</prop>
        <prop key="update*">PROPAGATION_REQUIRED,-OrderException,-OrderMinimumAmountException</prop>
        <prop key="del*">PROPAGATION_REQUIRED,-OrderException,-OrderMinimumAmountException</prop>
      </props>
    </property>
  </bean>  
  
  <!-- 소위 Spring을 사용하게 되는 비지니스 객체의 클래스를 지정하는 부분이다. 
  즉 여기선 gikim.dongguk.guestboard.spring.GuestSpringImpl 클래스가 Spring의 여러가지 기능을 담당하게 되는것이다.
  그리고 관리하게 되는 DAO는 guestDAO로 지정한다. 
  -->
  <bean id="guestTarget" class="gikim.dongguk.guestboard.spring.GuestSpringImpl">
    <property name="guestDAO"><ref local="guestDAO"/></property>
  </bean>  
  
  <!-- DAO에 관련된 셋팅이다. 실제로 iBATIS SQLMaps를 사용하게 되는 클래스를 지정하게 된다. 
  DB정보인 dataSource값과. iBATIS SQLMaps설정파일의 위치에 해당하는 sqlMapClient를 지정한다. 
  
  여기서 iBATIS SQLMaps 1.x는 두번째 property값의 name을 sqlMap로 2.x는 sqlMapClient로 지정해 주어야 한다.   
  - 이 부분은 확인중입니다. 
  -->
  <bean id="guestDAO" class="gikim.dongguk.guestboard.dao.IbatisGuestDAOImpl">
    <property name="dataSource"><ref local="dataSource"/></property>
    <property name="sqlMapClient"><ref local="sqlMap"/></property>
  </bean>
</beans>

1. dataSource설정부분의 다른 문서는 Object-Relational Mapping with SQLMaps의 "connection 그리고 트랜잭션 관리" 부분을 참조하면 된다.

sqlmap-config.xml#

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMapConfig PUBLIC "-//iBATIS.com//DTD SQL Map Config 2.0//EN" 
"http://www.ibatis.com/dtd/sql-map-config-2.dtd">
<sqlMapConfig>
  <!-- 설정 파일내에서 name=value형태로 지정한다. 이 파일은 classpath에 상대적인 위치로 지정해야 한다. 
  현재는 지정하지 않은 상태이다. 
  
  <properties resource="database.properties"/>
  -->
  <!-- 이 셋팅은 SqlMapClient를 제어하는 셋팅값이다. 주로 트랜잭션 관리에 사용된다. -->
    <settings
    cacheModelsEnabled="true"
    enhancementEnabled="true"
    lazyLoadingEnabled="true"
    maxRequests="32"
    maxSessions="10"
    maxTransactions="5"
    useStatementNamespaces="false"
  />
  
  <!-- 완전한 경로를 가진 class이름을 짧은 이름으로 대체 
  여기서 typeAlias를 개별의 xml파일에 위치한다.
  -->
  
  <!-- db에 관련된 DataSource에 관련된 셋팅이다. 
  이는 Spring의 사용으로 applicationContext-ibatis.xml에 내용이 이동되었다. 
    -->
    
    <!-- 객체와 DB table을 맵핑하기 위한 xml파일의 위치를 지정하는 부분이다. -->
    <sqlMap resource="gikim/dongguk/guestboard/Guest.xml"/>
</sqlMapConfig>

Guest.xml#

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE sqlMap PUBLIC "-//iBATIS.com//DTD SQL Map 2.0//EN" "http://www.ibatis.com/dtd/sql-map-2.dtd">
<sqlMap namespace="Guest">
  <typeAlias alias="guest" type="gikim.dongguk.guestboard.Guest"/>
  <cacheModel id="oneDayProduct" type="LRU">
      <flushInterval hours="24"/>
  </cacheModel>
    <select id="getGuest"
        parameterClass="int" resultClass="guest">
            SELECT num, 
              title, 
              content, 
              writer, 
              passwd, 
              clickcnt, 
              writeday
      FROM guestboard
            WHERE num = #num#
    </select>
    <select id="getList"
        parameterClass="int" resultClass="guest">
      SELECT num, 
              title, 
              content, 
              writer, 
              passwd, 
              clickcnt, 
              writeday
      FROM guestboard
    </select>
  <update id="updateGuest" parameterClass="guest">
    update guestboard SET
      title=#title# ,
      content=#content# ,
      writer=#writer#
    where num=#num#
  </update>
</sqlMap>

Guest.java#

package gikim.dongguk.guestboard;


/**
 @author DongGuk
 *  
 */
public class Guest implements java.io.Serializable {
    private int num;

    private String title;

    private String content;

    private String writer;

    private String passwd;

    private String writeday;

    private int clickcnt;

    public Guest() {

    }

    /**
     @return
     */
    public int getClickcnt() {
        return clickcnt;
    }

    /**
     @return
     */
    public String getContent() {
        return content;
    }

    /**
     @return
     */
    public int getNum() {
        return num;
    }

    /**
     @return
     */
    public String getPasswd() {
        return passwd;
    }

    /**
     @return
     */
    public String getTitle() {
        return title;
    }

    /**
     @return
     */
    public String getWriteday() {
        return writeday;
    }

    /**
     @return
     */
    public String getWriter() {
        return writer;
    }

    /**
     @param i
     */
    public void setClickcnt(int i) {
        clickcnt = i;
    }

    /**
     @param string
     */
    public void setContent(String string) {
        content = string;

    }

    /**
     @param i
     */
    public void setNum(int i) {
        num = i;
    }

    /**
     @param string
     */
    public void setPasswd(String string) {
        passwd = string;
    }

    /**
     @param string
     */
    public void setTitle(String string) {
        title = string;
    }

    /**
     @param string
     */
    public void setWriteday(String string) {
        writeday = string;
    }

    /**
     @param string
     */
    public void setWriter(String string) {
        writer = string;
    }
}

IbatisGuestDAOImpl.java#

/*
 * Created on 2005. 02. 28.
 *
 */
package gikim.dongguk.guestboard.dao;

import gikim.dongguk.guestboard.*;

import java.util.*;

import org.apache.log4j.Logger;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

/**
 @author DongGuk Lee
 
 */
public class IbatisGuestDAOImpl extends SqlMapClientDaoSupport implements GuestDAO {
    private static Logger logger = Logger.getLogger(IbatisGuestDAOImpl.class.getName());

    public IbatisGuestDAOImpl() {
        super();
    }

    public Guest getGuest(int num) {
        Guest guest = (Guest)getSqlMapClientTemplate().queryForObject("getGuest"new Integer(num));
        if (guest == null) {
            guest = new Guest();
        }
        
        return guest;
    }

    public Guest getLastGuest() {
        return new Guest();
    }

    public Object addGuest(final Guest guest) {
        return null;
    }

    public void updateGuest(final Guest guest) {
      int result = 0;
      result = getSqlMapClientTemplate().update("updateGuest", guest);
    }

    public Object delGuest(final int num) {
        return null;
    }

    public List getList(SearchInfo info) {
        List list = getSqlMapClientTemplate().queryForList("getList"new Integer("0"));        
        if (list == null) {
            list = new ArrayList();
        }
        
        return list;
    }
}

Add new attachment

Only authorized users are allowed to upload new attachments.
« This page (revision-1) was last changed on 06-Apr-2006 09:45 by UnknownAuthor