(1) Spring 설정
1) pom.xml
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>2.7.8</version>
</dependency>
2) root-context.xml
<bean id="hikariConfig" class="com.zaxxer.hikari.HikariConfig">
<property name="driverClassName"
value="net.sf.log4jdbc.sql.jdbcapi.DriverSpy"></property>
<property name="jdbcUrl"
value="jdbc:log4jdbc:oracle:thin:@localhost:1521:JAVA"></property>
<property name="username" value="scott"></property>
<property name="password" value="tiger"></property>
</bean>
<bean id="dataSource" class="com.zaxxer.hikari.HikariDataSource"
destroy-method="close">
<constructor-arg ref="hikariConfig" />
</bean>
3) pom.xml
<dependency>
<groupId>com.oracle.database.jdbc</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0.4</version>
</dependency>
4) DataSourceTests.java
5) pom.xml
<dependency>
<groupId>org.bgee.log4jdbc-log4j2</groupId>
<artifactId>log4jdbc-log4j2-jdbc4</artifactId>
<version>1.16</version>
</dependency>
6) log4jdbc.log4j2.properties
7) web.xml
<filter>
<filter-name>encoding</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encoding</filter-name>
<servlet-name>appServlet</servlet-name>
</filter-mapping>
(2) mybatis 설정
1) pom.xml
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.2</version>
</dependency>
2) root-context.xml
<bean id="sqlSessionFactory"
class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation"
value="classpath:/mybatis-config.xml"/>
</bean>
3) mybatis-config.xml
<configuration>
<typeAliases>
<typeAlias alias="Address" type="soo.md.domain.Address"/>
</typeAliases>
<mappers>
<mapper resource="soo/md/mapper/AddressMapper.xml"/>
</mappers>
</configuration>
4) AddressMapper.xml ( 참조: https://mybatis.org/mybatis-3/ko/sqlmap-xml.html )
5) AddressMapper.java
6) 컴포넌트 스켄
<context:component-scan base-package="soo.md.mapper"/>
<mybatis-spring:scan base-package="soo.md.mapper"/>
7) AddressMapperTests.java
(3) DBMS별 dataSource 설정
1) Oracle
1)-1 root-context.xml
<bean id="hikariConfig" class="com.zaxxer.hikari.HikariConfig">
<property name="driverClassName"
value="net.sf.log4jdbc.sql.jdbcapi.DriverSpy"></property>
<property name="jdbcUrl"
value="jdbc:log4jdbc:oracle:thin:@localhost:1521:JAVA"></property>
<property name="username" value="scott"></property>
<property name="password" value="tiger"></property>
</bean>
<bean id="dataSource" class="com.zaxxer.hikari.HikariDataSource"
destroy-method="close">
<constructor-arg ref="hikariConfig" />
</bean>
1)-2 pom.xml
<dependency>
<groupId>com.oracle.database.jdbc</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0.4</version>
</dependency>
미리 만든 mapper클래스와 domain을 통해 오라클db와 연결이 되는지 테스트 클래스를 작성
[테스트 클래스 작성]
1) @RunWith
-현재 테스트 코드가 스프링을 실행하는 역할을 할 것이라는 것을 의미
2) @ContextConfiguration
-단위 테스트에서 가장 중요한 어노테이션으로 속성 값이 중요
-지정된 클래스나 문자열을 통해 필요한 객체들을 스프링 내에 객체로 등록 ---> 스프링의 빈(Bean)으로 등록된다고 표현
-JAVA의 경우 'classes'를 이용해 root-context.xml의 역할을 대신하는 RootCofig 경로를 지정
--->
@ContextConfiguration(classes={RootConfig.class})
[XML]
3) @Test
JUnit에서 테스트 대상을 표시하는 어노테이션, 헤당 메서드를 선택하고 Junit Test 기능을 실행
테스트 실행 결과 (테스트는 오라클 연동 확인을 위해 insert로 실행)
console창에 데이터가 저장됐음 보여주고 테스트 코드도 문제 없이 실행
오라클 db에 저장이 되었다