본문 바로가기

DB/iBatis

iBatis 사용방법 정리

1.1. web site

1.2. manual

 

1.3. log4j 설정

log4j.logger.com.ibatis=ERROR log4j.logger.com.ibatis.common.jdbc.SimpleDataSource=ERROR log4j.logger.com.ibatis.common.jdbc.ScriptRunner=ERROR log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate=ERROR
log4j.logger.java.sql.Connection=ERROR log4j.logger.java.sql.Statement=ERROR log4j.logger.java.sql.PreparedStatement=ERROR log4j.logger.java.sql.ResultSet=ERROR

1.4. 설정법

1.4.1. library download

1.4.2. SqlmapConfig.xml 설정

  • WEB-INF/classes 경로에 위치한다.
  • Data Source 사용시
  • Data Source 사용하지 않고 직접 설정시
    •  

    • 아래 "DS_TEST"라고 적혀 있는 부분을 자신이 설정한 Data Source 이름을 넣으면 된다.
      <?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> <settings cacheModelsEnabled="false" enhancementEnabled="false" lazyLoadingEnabled="false" maxRequests="64" maxSessions="20" maxTransactions="10" useStatementNamespaces="true" />

      <transactionManager type="JDBC"> <dataSource type="JNDI"> <property name="DataSource" value="DS_TEST" /> </dataSource> </transactionManager>
      <sqlMap resource="Sqlmap.xml"/>
      </sqlMapConfig>

1.4.3. Sqlmap.xml 설정

  • WEB-INF/classes 경로에 위치한다. 
  •  

  •  

  • query 설정 (parameterClass에 지정되어 있는것은 적합하게 만들어야 한다.
    • insert
      <insert id="insertUtsExtract" parameterClass="com.voce.sktucc.mh.mif2.bean.UtsJob2">
      <selectKey resultClass="int" keyProperty="jobid">
      SELECT SEQ_MH_EXTRACT_ID.NEXTVAL AS jobid FROM DUAL
      </selectKey>
      <![CDATA[
      INSERT INTO T_UTS_EXTRACT(jobid, srcpath, snaps, snapspath, width,
      height, registered, uniqueid)
      values(#jobid#, #srcpath#, #snaps#, #snapspath#, #width#,
      #height#, CAST(sysdate AS TIMESTAMP), #uniqueid#)
      ]]>
      </insert>
    • update
      <update id="updateExtractJobResetForJobid" parameterClass="com.voce.sktucc.mh.mif2.bean.UtsJob2">
      <![CDATA[
      UPDATE T_UTS_EXTRACT SET assigned = null, started = null, ended = null, result
      WHERE jobid = #jobid#
      ]]>
      </update>

    • select
      <select id="getJob" resultClass="com.voce.sktucc.mh.mif2.bean.UtsJob2">
      <![CDATA[
      SELECT jobid, srcpath, dstpath, vcodec, vwidth,
      vheight, vfps, vbitrate, acodec, asample,
      abitrate, callback, assigned, registered, started,
      cookie, reporturl, uniqueid
      FROM T_UTS_JOB2
      WHERE ROWNUM = 1 AND assigned IS NULL ORDER BY registered
      ]]>
      </select>

1.4.4. 사용예

  • SqlMapClient 초기화

    SqlMapClient sqlmap = null;

    try
    {
    Reader reader = Resources.getResourceAsReader("SqlmapConfig.xml");
    sqlmap = SqlMapClientBuilder.buildSqlMapClient(reader);
    }
    catch (Exception e)
    {
    System.out.println(e, e);
    }

  • Query 호출
    • 아래 내용들의 "namespace.id"은 SqlMap.xml에 설정 되어 있는 값을 넣으면 된다.

      <sqlMap namespace="uccmh">
      <insert id="insertUtsExtract" parameterClass="com.voce.sktucc.mh.mif2.bean.UtsJob2">
      <![CDATA[
      INSERT INTO T_UTS_EXTRACT(jobid, srcpath)
      values(#jobid#, #srcpath#)
      ]]>
      </insert>

      <update id="updateJobCancel" parameterClass="com.voce.sktucc.mh.mif2.bean.UtsJob2">
      <![CDATA[
      UPDATE T_UTS_JOB2 SET canceljob = #canceljob#
      WHERE jobid = #jobid#
      ]]>
      </update>
      </sqlMap>
      • updateJobCancel query를 실행하고자 한다면
        UtsJob2 job = new UtsJob2();
        ...
        sqlmap.update("uccmh.updateJobCancel", job);

      • insertUtsExtract query를 실행하고자 한다면
        UtsJob2 job = new UtsJob2();
        ...
        sqlmap.insert("uccmh.insertUtsExtract", job);

    • insert
      try { sqlmap.startTransaction(); jobid = sqlmap.insert("namespace.id", job).toString(); sqlmap.commitTransaction(); } catch (Exception e) { System.out.println(e, e); } finally { try { sqlmap.endTransaction(); } catch (Exception e) { } }
    • update
      try { sqlmap.startTransaction(); sqlmap.update("namespace.id", job); sqlmap.commitTransaction(); } catch (Exception e) { System.out.println(e, e); } finally { try { sqlmap.endTransaction(); } catch (Exception e) { } }

    • select
      try { sqlmap.startTransaction(); sqlmap.queryForObject("namespace.id"); } catch (Exception e) { System.out.println(e, e); } finally { try { sqlmap.endTransaction(); } catch (Exception e) { } }

'DB > iBatis' 카테고리의 다른 글

IBATIS > Dynamic SQL  (1) 2008.11.07
The SqlMap API  (1) 2008.07.07
iBatis sqlMap 설정하기  (0) 2008.07.03
IBATIS 에서 LIKE 검색 기능 이용하기  (0) 2008.07.03
Websphere 6 + iBatis 연동 설정  (0) 2008.06.10