본문 바로가기
홈페이지 만들기/Spring

iBatis 연동

by 리틀홍콩 2015. 4. 17.
728x90
반응형

 

=== business-layout.xml ====================================================

 

...

...

 

 <!-- DataSource 클래스 등록 -->
 <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
       destroy-method="close">
  <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
  <property name="url" value="jdbc:oracle:thin:@localhost:1521:xe" />
  <property name="username" value="hr" />
  <property name="password" value="hr" />
 </bean>

 

<!-- Spring과 iBatis 연동 설정 -->
 <bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
  <property name="dataSource" ref="dataSource"></property>
  <property name="configLocation" value="classpath:sql-map-config.xml"></property>
 </bean>

 <bean id="sqlMapClientTemplate" class="org.springframework.orm.ibatis.SqlMapClientTemplate">
  <property name="sqlMapClient" ref="sqlMapClient"></property>
 </bean>

..

 

============================================================================

** id값이 dataSource 인 bean의 속성값을 가져와서 연동시킨다.

    configLocationclasspath는 프로젝트 경로 내에 위치한 Java Resources를 말한다.

 

 

 

=== 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>
 <!-- SQL Mapping 파일 위치 등록 -->
 <sqlMap resource="mappings/board-mapping.xml"/>
 <sqlMap resource="mappings/user-mapping.xml"/>
</sqlMapConfig>

 

============================================================================

** 쿼리문이 작성된 xml경로를 작성한다.

 

 

=== board-mapping.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>

 ...

 ...

 <select id="getBoard" resultMap="boardResult">
  <![CDATA[
  SELECT *
  FROM BOARD
  WHERE SEQ=#seq#
  ]]>
 </select>

 <select id="getBoardList" resultMap="boardResult">
  select *
  from board
  where 1=1
  <isEqual prepend="AND" property="searchCondition" compareValue="TITLE">
  title like '%'||#searchKeyword#||'%'
  </isEqual>
  <isEqual prepend="AND" property="searchCondition" compareValue="CONTENT">
  content like '%'||#searchKeyword#||'%'
  </isEqual>
  order by seq desc
 </select>

</sqlMap>

 

============================================================================

** 작성된 쿼리문// 이건 뭐 설명할게 없다.

 

== BoardDao.java ===========================================================

...

... 

 

@Autowired
 private SqlMapClientTemplate ibatis;

 

 // 글 삭제
 public void deleteBoard(BoardVO vo) {
  ibatis.delete("deleteBoard",vo);
 }
 
 // 글 상세 조회
 public BoardVO getBoard(BoardVO vo) { 
  return (BoardVO) ibatis.queryForObject("getBoard",vo);
 }

...

...

============================================================================

** 예제문

 

 

반응형

댓글