by-nc-sa     개발자, DBA가 함께 만들어가는 구루비 지식창고!

Spring과 iBatis의 연동




Spring과 iBatis의 연동

iBatis maven Dependency 설정

  • ibatis와 spring-orm의 dependency 설정을 pom.xml 에서 확인한다.
  • ibatis 버전이 2.3.4.726 인지 확인한다.
pom.xml

<dependency>
   <oupId>org.apache.ibatis</groupId>
   <artifactId>ibatis-sqlmap</artifactId>
   <version>2.3.4.726</version>
</dependency>

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-orm</artifactId>
    <version>${spring-core-version}</version>
</dependency>

Spring datasource 설정

  • applicationContext-datasource.xml 파일에 DataSource를 추가한다.
  • SqlMapClientFactoryBean빈을 sqlMapClient로 생성한다.
    • dataSource와 sqlMapConfig XML 파일의 의존성주입을 추가한다.
  • SqlMapClientTemplate빈을 생성하고, sqlMapClient를 의존성 주입한다.
/src/main/resources/spring/applicationContext-datasource.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"
    default-autowire="byName">

    <!-- annotation 기반 트랜잭션 설정 -->
    <tx:annotation-driven transaction-manager="transactionManager"/>
             
    <!-- Local Apache Commons DBCP DataSource -->
    <bean id="defaultDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="${JDBC.Driver}"/>
        <property name="url" value="${JDBC.ConnectionURL}"/>
        <property name="username" value="${JDBC.Username}"/>
        <property name="password" value="${JDBC.Password}"/>
        <property name="maxActive" value="15" />
        <property name="initialSize" value="15" />
        <property name="maxIdle" value="15" />
        <property name="minIdle" value="15" />
        <property name="testOnBorrow" value="false" />
        <property name="validationQuery" value="SELECT 1 FROM DUAL" />
        <property name="timeBetweenEvictionRunsMillis" value="10000" />
        <property name="testWhileIdle" value="true" />  
        <property name="numTestsPerEvictionRun" value="3" />        
        <property name="minEvictableIdleTimeMillis" value="-1" />
    </bean>

    
    <!-- Transaction Manager for a single JDBC DataSource -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="defaultDataSource"/>
    </bean>

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

</beans>

sqlMapConfig, sqlMap XML 파일 설정

  • sqlMapConfig, sqlMap XML 파일을 생성한다.
  • /src/main/resources/datasource/sql-map-config.xml 파일로 생성한 예이다.
/src/main/resources/datasource/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
		cacheModelsEnabled="true"
		enhancementEnabled="true"
		lazyLoadingEnabled="true"
		useStatementNamespaces="true"
	/>
	
	<sqlMap resource="sqlmap/Emp.xml"/> 
	
</sqlMapConfig>

JDBC Driver 접속정보 설정

  • /filter/build-local.filter 파일을 열어서 접속할 오라클 DB 정보를 설정한다.
/filter/build-local.filter

{code:title=/filter/build-local.filter|borderStyle=solid}
JDBC.Driver=oracle.jdbc.driver.OracleDriver
JDBC.ConnectionURL=jdbc:oracle:thin:@127.0.0.1:1521:ORCL
JDBC.Username=scott
JDBC.Password=tiger

web.xml 수정

  • web.xml 파일에서 applicationContext-datasource.xml 파일을 로딩하도록 context-param 설정을 추가한다.
/WEB-INF/web.xml
..
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
        	classpath:spring/applicationContext-datasource.xml
        	classpath:spring/applicationContext.xml			
		</param-value>
    </context-param>
..
 

문서정보

Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.