java. ibatis 간단요약



  • 사용예


파라미터 주입 및 기본적인 쿼리 형식

<!--
## 와 달리 $$는 파라미터를 단순히 문자열로 삽입한다는 것에 주의하자
-->
<select 
 id="selectListForPaging" 
 parameterClass="com.gomdolinara.PagingListInfo" 
 resultClass="com.gomdolinara.SomeVo"
 >
 select * from some_table
 <dynamic prepend=" where ">
  <isEqual property="searchModeYN" compareValue="Y" prepend=" and ">
   $searchTarget$ like concat( '%', #searchKeyword#, '%' )
  </isEqual>
  <isEqual property="fromModeYN" compareValue="Y" prepend=" and ">
   $dateTarget$ >= #fromDate#
  </isEqual>  
  <isEqual property="toModeYN" compareValue="Y" prepend=" and ">
   #toDate# >= $dateTarget$
  </isEqual>    
 </dynamic>
 order by $orderTarget$ $orderType$
 limit $firstRecordIndex$, $recordCountPerPage$
</select>


in 조건문을 위한 iterate 형식

<!-- 
ArrayList< String > 타입을 파라미터로 넘기고 있다. 
VO List를 넘길 경우 참조할 파라미터를 #value[]# 대신에 입력해주면 된다
-->
<select
 id="selectListWithSeqList"
 parameterClass="java.util.List"
 resultClass="com.gomdolinara.SomeVo"
 >
 select *
 from some_tbl
 where 1 = 1
 <dynamic prepend=" and ">
 <iterate open=" tgt_field in ( " conjunction=", " close=" ) ">
  #value[]#
 </iterate>
 </dynamic> 
</select>


BLOB 인서트
<insert
 id="testHandleBlob"
 parameterClass="HashMap">
 insert into test_blob ( test ) values ( #bytes:BLOB# )
</insert>
public void testHandleBlob( byte[] ba ){
 HashMap< String, byte[] > m = new HashMap< String, byte[] >();
 m.put( "bytes", ba );
 sqlMapClient.insert( "testHandleBlob", m );
}

참고 - Dynamic SQL


  • 설정


src/configure/sql-map-config.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>
 
 <settings useStatementNamespaces="true"/>
 
  <sqlMap resource="sqlmap/a-a-sql-map.xml"/> 
  <sqlMap resource="sqlmap/a-b-sql-map.xml"/> 
  <sqlMap resource="sqlmap/b-a-sql-map.xml"/> 
</sqlMapConfig>

src/sqlmap/a-a.sql-map.xml

 
<?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="a.a">
 
</sqlMap>

참고 - iBATIS Configuration