본문 바로가기
DB

Spring 프로젝트에 Oracle DB 연동하는 방법

by sliora 2017. 3. 10.

Spring Project에 설정파일 3개를 수정해야 합니다.( root-context.xml , pom.xml , web.xml )



1. root-context.xml (root로 되어있는곳에 사용할 id와 password를 입력해야 합니다.)

<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:DB명" />

<property name="username" value="root"/>

<property name="password" value="root"/>

</bean>


1-1. sqlSessionFactory를 넣어주고, mapper 경로를 지정해줍니다.

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

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

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

</bean>


1-2. SqlSession을 구현하고 코드에서 SqlSession를 대체하는 역할을 하는 MyBatis 연동 모듈을 추가해준다. 

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

<constructor-arg index="0" ref="sqlSessionFactory" />

</bean>


2. pom.xml (메이븐 설정)

<repositories>

<repository>

<id>mesir-repo</id>

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

</repository>

</repositories>


<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-orm</artifactId>

<version>${org.springframework-version}</version>

<exclusions>


<exclusion>

<groupId>commons-logging</groupId>

<artifactId>commons-logging</artifactId>

 </exclusion>

</exclusions>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-webmvc</artifactId>

<version>${org.springframework-version}</version>

</dependency>

<dependency>

<groupId>org.mybatis</groupId>

<artifactId>mybatis</artifactId>

<version>3.2.8</version>

</dependency>

<dependency>

<groupId>org.mybatis</groupId>

<artifactId>mybatis-spring</artifactId>

<version>1.2.2</version>

</dependency>

<dependency>

<groupId>com.oracle</groupId>

<artifactId>ojdbc14</artifactId>

<version>10.2.0.4.0</version>

</dependency>


3. web.xml (Servlet맵핑을 위한 설정)

<servlet-mapping>

<servlet-name>appServlet</servlet-name>

<url-pattern>/</url-pattern>

</servlet-mapping>