본문 바로가기

Programming/Spring

[Spring] Spring + MyBatis(iBatis) + MySQL(Oracle) 연동

Spring + MyBatis(iBatis) + MySQL 연동



Spring jdbc, MySQL Connector Maven dependency

Spring JDBC Library

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-jdbc</artifactId>

<version>3.2.3.RELEASE</version>

</dependency>


MySQL Connector Library

<dependency>

<groupId>mysql</groupId>

<artifactId>mysql-connector-java</artifactId>

<version>5.1.25</version>

</dependency>


Oracle Connector Library

 <dependency>

<groupId>com.oracle</groupId>

<artifactId>ojdbc14</artifactId>

<version>10.2.0.4.0</version>

</dependency>

오라클의 경우 빨간줄 에러가 나면 상단에 아래와 같이 명시

<repositories>

<repository>

<id>mesir-repo</id>

<url>http://mesir.googlecode.com/svn/trunk/mavenrepo</url>

</repository>

</repositories>


MyBatis Maven dependency

<!-- MyBatis Library -->

<dependency>

<groupId>org.mybatis</groupId>

<artifactId>mybatis</artifactId>

<version>3.2.2</version>

</dependency>


<dependency>

<groupId>org.mybatis</groupId>

<artifactId>mybatis-spring</artifactId>

<version>1.2.0</version>

</dependency>


iBatis Maven dependency

<dependency> 

    <groupId>org.springframework</groupId> 

    <artifactId>spring-orm</artifactId> 

    <version>3.0.4.RELEASE</version> 

    <scope>compile</scope> 

</dependency>


<dependency> 

    <groupId>org.apache.ibatis</groupId> 

    <artifactId>ibatis-sqlmap</artifactId> 

    <version>2.3.4.726</version> 

</dependency>



config.xml (src/main/resources/conf)

<?xml version="1.0" encoding="UTF-8" standalone="no"?>

<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">

<properties>

<comment>Local configure</comment>


<!-- dataSource config -->

<!-- Oracle -->

<!-- <entry key="db.jdbc.driver">oracle.jdbc.driver.OracleDriver</entry> -->

<!-- MySQL -->

<entry key="db.jdbc.driver">com.mysql.jdbc.Driver</entry>

<!-- Oracle -->

<!-- <entry key="db.jdbc.url">jdbc:oracle:thin:@</entry>  -->

<!-- MySQL -->

<entry key="db.jdbc.url">jdbc:mysql://URL</entry> 

<entry key="db.jdbc.username">username</entry>

<entry key="db.jdbc.password">password</entry>

<entry key="db.jdbc.characterset">utf-8</entry>

<entry key="db.jdbc.initialSize">10</entry>

</properties>


sqlMapConfig.xml (src/main/resources/sql)

<?xml version="1.0" encoding="UTF-8"?>

<!-- Valid 한 설정 xml을 만들기 위해 DTD를 추가해 줍니다. -->

<!DOCTYPE configuration

PUBLIC "-//mybatis.org//DTD Config 3.0//EN"

"http://mybatis.org/dtd/mybatis-3-config.dtd">

<configuration>


<settings>

<setting name="cacheEnabled" value="false" />

<setting name="useGeneratedKeys" value="true" />

<setting name="defaultExecutorType" value="REUSE" />

</settings>

    

<!-- VO Type Aliases -->

<!-- <typeAliases>

<typeAlias alias="userInfoVO" type="web.user.vo.UserInfoVO"/>

<typeAlias alias="zipCodeAddrVO" type="web.zipcode.vo.ZipCodeAddrVO"/>

</typeAliases> -->

    

</configuration>


*_.xml (src/main/resources/sql)

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"

"http://mybatis.org/dtd/mybatis-3-mapper.dtd" >

<mapper namespace="userinfo" >

<insert id="insert">

</insert>

  <select id="select">

 

  </select>

  <delete id="delete">


  </delete>


  <update id="update">


  </update>

</mapper>



context.xml (MyBatis의 경우)

<!-- config.xml --> 

<util:properties id="config" location="classpath:conf/config.xml" />

<context:property-placeholder properties-ref="config" />


<!-- dataSource -->

<bean id="dataSource"     class="org.springframework.jdbc.datasource.DriverManagerDataSource">

<property name="driverClassName" value="#{config['db.jdbc.driver']}" />

<property name="url" value="#{config['db.jdbc.url']}" />

<property name="username" value="#{config['db.jdbc.username']}" />

<property name="password" value="#{config['db.jdbc.password']}" />

</bean>

<!-- commons DBCP 사용시 -->

<!-- 

<bean id="dataSource"     class="org.springframework.jdbc.datasource.DriverManagerDataSource">

<property name="driverClassName" value="#{config['db.jdbc.driver']}" />

<property name="url" value="#{config['db.jdbc.url']}" />

<property name="username" value="#{config['db.jdbc.username']}" />

<property name="password" value="#{config['db.jdbc.password']}" />

</bean> -->


<!-- SqlSessionFactory -->

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">

<property name="dataSource" ref="dataSource"/>

<!-- MyBatis config 설정이 있는 xml -->

<property name="configLocation" value="classpath:sql/SqlMapConfig.xml"/>

<!-- mapper xml 경로 정의 이름_sql.xml 형식을 참조 -->

<property name="mapperLocations" value="classpath:sql/*_sql.xml"/>

</bean>


<!-- SqlSessionTemplate -->

<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">

<constructor-arg ref="sqlSessionFactory"/>

</bean>


<!-- transactionManager -->

<bean id="transactionManager"

class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

<property name="dataSource" ref="dataSource"></property>

</bean>



context.xml (MyBatis의 경우)

<!-- config.xml --> 

<util:properties id="config" location="classpath:conf/config.xml" />

<context:property-placeholder properties-ref="config" />


<!-- dataSource -->

<bean id="dataSource" 

class="org.springframework.jdbc.datasource.DriverManagerDataSource">

<property name="driverClassName" value="#{config['db.jdbc.driver']}" />

<property name="url" value="#{config['db.jdbc.url']}" />

<property name="username" value="#{config['db.jdbc.username']}" />

<property name="password" value="#{config['db.jdbc.password']}" />

</bean>


<!-- ibatis -->
 <bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
  <property name="configLocation">
   <value>classpath:sql/SqlMapConfig.xml</value>
  </property>
  <property name="dataSource" ref="dataSource"></property>
 </bean>
 
 <bean id="sqlMapClientTemplate" class="org.springframework.orm.ibatis.SqlMapClientTemplate">
  <property name="sqlMapClient" ref="sqlMapClient"/>
 </bean>

<!-- transactionManager -->

<bean id="transactionManager"

class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

<property name="dataSource" ref="dataSource"></property>

</bean>