Java에서 SQL Maps and Data Access Objects (DAO)로 알려진 Database 연동 플랫폼인 iBatis를 정리 한다.

 

iBatis 환경 설정

  • ORM(Object Relational Mapping)
    • 데이터베이스의 레코드를 직접 Java Class에 매핑하고 XML 형태의 SQL 문을 실행하는 Persistence Layer를 담당하는 Framework 개발 모델
  • /WEB-INF/classes/sqlMapConfig.xml : DB 정보와 mapper file 정보 등의 환경 설정 파일
  • mapper file (~Map.xml) : 실제 SQL문을 작성하는 XML 파일

sqlMapConfig.xml

settings

  • cacheModelsEnabled : true. 캐시 가능
  • enhancementEnabled : false. 런타임시 바이트 코드 기능 향상
  • lazyLoadingEnabled : true
  • maxRequests : 512
  • maxSessions : 128
  • maxTransactions : 32
  • useStatementNamespaces : false

typeAlias

<typeAlias alias="Account" type="org.apache.domain.Account" />

typeHandler

<typeHandler javaType="String" callback="com.StringTypeHandler"/>

properties

<properties resource="SqlMapConfig.properties" />

sqlMap

<sqlMap resource="sqlMaps/board.xml" />

transactionManager

<transactionManager type="JDBC">

<dataSource type="DBCP">

<property name="JDBC.Driver" value="${driver}" />

<property name="JDBC.ConnectionURL" value="${url}" />

<property name="JDBC.Username" value="${username}" />

<property name="JDBC.Password" value="${password}" />

<property name="JDBC.DefaultAutoCommit" value="false" />

</dataSource>

</transactionManager>

Mapper file

resultMap

<resultMap class="boardbean" id="boardbean">

<result property="postId" column="post_Id"/>

<result property="seq" column="SEQ" jdbcType="INTEGER" />

<result property="detail" resultMap="Catetory.ChildMap"/>

</resultMap>

parameterMap

<parameterMap class="boardbean" id="boardbean">

<parameter property="postId" />

</parameterMap>

select

id

 

parameterClass

전달되는 parameter의 class를 지정

parameterMap

전달되는 parameter

resultClass

반환되는 resultMap의 class를 지정

resultMap

반환되는 값

cacheModel

 

Table Join시

  • resultMap 선언

<resultMap id="parent" class="parentMap">

<result property="child" resultMap="catetory.childMap"/>

</resultMap>

  • SQL문 선언

from parentTable a

left outer join childTable b on a.SEQ = b.PARENT_SEQ

<select id="readDeletedScheduleListAfterSyncTime" parameterClass="map" resultMap="schedulerResult">

select *

from parentTable a

left outer join childTable b on a.SEQ = b.PARENT_SEQ

where mail_user_seq = #mailUserSeq#

and outlook_sync = 'delete'

and modify_time <![CDATA[ >= ]]> #syncTime#

order by start_date asc

limit #skipResult#, #maxResult#

</select>

selectKey

<selectKey resultClass="int" keyProperty="bbsId">

select case when max(bbs_id) is null then 1 else max(bbs_id)+1 end

from tscheduler

</selectKey>

insert

<insert id="saveShareFolder" parameterClass="map">

insert into shf_shr_folder (mail_user_seq, folder_uid)

values (#userSeq#, #fUid#)

</insert>

update

<update id="modifyAuthShareTargetFolder" parameterClass="webfoldershare">

update shf_shr_target set share_auth = #auth#, modify_time = #curTime#

where folder_uid = #fuid# and share_value = #email#

</update>

delete

<delete id="deleteShareTargetFolder" parameterClass="int">

delete from shf_shr_target where folder_uid = #value#

</delete>

dynamic

  • prepend : 동적 SQL문을 생성하기 전에 추가할 문자열
  • open : 시작하기 전에 추가할 문자열
  • close : 시작 후에 추가할 문자열
  • conjunction : SQL문 생성 사이 사이에 추가할 문자열
  • compareProperty : 비교되는 property
  • compareValue : 비교할 값

<dynamic prepend="WHERE">

<iterate property="postId"

conjunction="," open="post_id IN (" close=")" >

#postId[]#

</iterate>

</dynamic>

<dynamic prepend="and">

<isGreaterThan property="userSeq" compareValue="0">

mail_user_seq = #userSeq#

</isGreaterThan>

</dynamic>

연산

  • iterate : <iterate property="postId" conjunction="," open="post_id IN (" close=")" >
  • isEqual : <isEqual property="type" compareValue="name">
  • isNotEqual : <isNotEqual property="a" compareValue="K"></isNotEqual>
  • isGreaterEqual : <isGreaterEqual property="pageLineCnt" compareValue="0" prepend=",">
  • isGreaterThan : <isGreaterThan property="userSeq" compareValue="0">
  • isLessEqual
  • isLessThan
  • isNull : <isNull property="a"></isNull>
  • isNotNull : <isNotNull property="userSeqs">
  • isEmpty : <isEmpty property="a"></isEmpty>
  • isNotEmpty : <isNotEmpty property="a"></isNotEmpty>
  • isPropertyAvailable : <isPropertyAvailable property="memberName" prepend=",">
  • isNotPropertyAvailable : <isNotPropertyAvailable property="a"></isNotPropertyAvailable>
  • isParameterPresent :
  • isNotParameterPresent :

cacheModel

<cacheModel id="decisionPathCache" type="MEMORY" readOnly="true" serialize="false">

<flushInterval hours="24"/>

<flushOnExecute statement="SystemConfig.allCacheDelete"/>

<property name="reference-type" value="SOFT"/>

</cacheModel>

sql

  • 여기서 선언한 SQL문을 include 태그로 호출하여 사용 한다.

<sql id="getMember_Where_Clause" >

</sql>

include

  • sql 태그로 선언한 SQL문을 삽입 한다.

<include refid="getMember_Where_Clause" />

procedure

 

statement

<statement id="test" parameterClass="package.ClassName" >

Collection

 

RowHandler

 

DAO 프로그램

SqlMapClient

Reader reader = Resources.getResourceAsReader("SqlMapConfig.xml");

SqlMapClient sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);

query

User user = (User) sqlMap.queryForObject(id, parameter);

sqlMap.queryForList(id, parameter);

sqlMap.queryForMap(id, parameter, key);

Map map = new HashMap();

map.put("mailUserSeq", userSeq);

map.put("agingDay", agingDay);

sqlMap.insert("~", map);

HashMap<String, Object> param = new HashMap<String, Object>();

param.put("mailDomain", domainSeq);

param.put("userSeq", userSeq);

sqlMap.update("~", param);

sqlMap.delete("~", ~);

참고 문헌

 

출처 - http://blog.naver.com/goodfe2?Redirect=Log&logNo=150121785142

'Framework & Platform > mybatis' 카테고리의 다른 글

mybatis password 암호화/복호화  (0) 2012.04.28
Mybatis selectList 예제  (0) 2012.03.28
mybatis 설치 예제  (0) 2012.03.18
ibatis vs MyBatis  (0) 2012.03.15
iBATIS(MyBatis)  (0) 2012.03.12
Posted by linuxism
,