[B -2-5] 마이바티스와 스프링 연동
2019. 9. 28. 22:04ㆍProject B (SPMS)/Project B 파트1
반응형
SQL 세션 준비
src/main/java
com.spms.config
RootConfig.java
sqlSessionFactory()
...더보기
package com.spms.config;
import javax.sql.DataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
@Configuration
public class RootConfig {
@Bean
public DataSource dataSource() {
HikariConfig hikariConfig = new HikariConfig();
hikariConfig.setDriverClassName("oracle.jdbc.driver.OracleDriver");
hikariConfig.setJdbcUrl("jdbc:oracle:thin:@호스트 주소:포트번호:SID");
hikariConfig.setUsername("아이디");
hikariConfig.setPassword("패스워드");
HikariDataSource dataSource = new HikariDataSource(hikariConfig);
return dataSource;
}
@Bean
public SqlSessionFactory sqlSessionFactory() throws Exception {
SqlSessionFactoryBean sqlSessionFactory = new SqlSessionFactoryBean();
sqlSessionFactory.setDataSource(dataSource());
return sqlSessionFactory.getObject();
}
}
마이바티스는 아래와 같은 절차로 접근을 해서 세션을 얻고, 그 세션을 가지고 DB에 접속한다.
SQL 세션 테스트 작성
src/test/java
com.spms.persistence
DataSourceTests.java
testMyBatis()
...더보기
package com.spms.persistence;
import static org.junit.Assert.fail;
import java.sql.Connection;
import javax.sql.DataSource;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.spms.config.RootConfig;
import lombok.Setter;
import lombok.extern.log4j.Log4j;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes= {RootConfig.class})
@Log4j
public class DataSourceTests {
@Setter(onMethod_ = { @Autowired })
private DataSource dataSource;
@Setter(onMethod_ = { @Autowired })
private SqlSessionFactory sqlSessionFactory;
@Test
public void testMyBatis() {
try {
SqlSession sqlSession = sqlSessionFactory.openSession();
Connection conn = sqlSession.getConnection();
log.info(sqlSession);
log.info(conn);
}catch(Exception e) {
fail(e.getMessage());
}
}
//@Test
public void testConnection() {
try (Connection conn = dataSource.getConnection()){
log.info(conn);
}catch(Exception e) {
fail(e.getMessage());
}
}
}
testConnection() 는 앞서 테스트한 부분이기때문에 테스트 대상에서 제외를 하고,
testMyBatis() 를 추가했다.
세션 팩토리에서 세션을 꺼내고, 세션에서 연결 객체를 얻는다.
연결 객체를 가지고 쿼리문을 보내게 된다.
SQL 세션 테스트 실행 결과
...더보기
INFO : org.springframework.test.context.support.DefaultTestContextBootstrapper - Loaded default TestExecutionListener class names from location [META-INF/spring.factories]: [org.springframework.test.context.web.ServletTestExecutionListener, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener, org.springframework.test.context.support.DependencyInjectionTestExecutionListener, org.springframework.test.context.support.DirtiesContextTestExecutionListener, org.springframework.test.context.transaction.TransactionalTestExecutionListener, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener]
INFO : org.springframework.test.context.support.DefaultTestContextBootstrapper - Using TestExecutionListeners: [org.springframework.test.context.web.ServletTestExecutionListener@343f4d3d, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener@53b32d7, org.springframework.test.context.support.DependencyInjectionTestExecutionListener@5442a311, org.springframework.test.context.support.DirtiesContextTestExecutionListener@548e7350, org.springframework.test.context.transaction.TransactionalTestExecutionListener@1a968a59, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener@4667ae56]
INFO : org.springframework.context.support.GenericApplicationContext - Refreshing org.springframework.context.support.GenericApplicationContext@18eed359: startup date [Sat Sep 28 21:19:40 JST 2019]; root of context hierarchy
INFO : org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor - JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
INFO : com.zaxxer.hikari.HikariDataSource - HikariPool-1 - Starting...
WARN : com.zaxxer.hikari.util.DriverDataSource - Registered driver with driverClassName=oracle.jdbc.driver.OracleDriver was not found, trying direct instantiation.
INFO : com.zaxxer.hikari.HikariDataSource - HikariPool-1 - Start completed.
INFO : com.spms.persistence.DataSourceTests - org.apache.ibatis.session.defaults.DefaultSqlSession@36cda2c2
INFO : com.spms.persistence.DataSourceTests - HikariProxyConnection@1530295320 wrapping oracle.jdbc.driver.T4CConnection@c03cf28
INFO : org.springframework.context.support.GenericApplicationContext - Closing org.springframework.context.support.GenericApplicationContext@18eed359: startup date [Sat Sep 28 21:19:40 JST 2019]; root of context hierarchy
INFO : com.zaxxer.hikari.HikariDataSource - HikariPool-1 - Shutdown initiated...
INFO : com.zaxxer.hikari.HikariDataSource - HikariPool-1 - Shutdown completed.
설정된 SqlSessionFactory 인터페이스 타입의 SqlSessionFactoryBean을 이용해 생성하고, 이것을 가지고 Connection까지의 테스트결과이다.
반응형
'Project B (SPMS) > Project B 파트1' 카테고리의 다른 글
[B -2-6] 스프링과의 연동 처리 (0) | 2019.09.28 |
---|---|
[B -2-4] 스프링과 오라클 데이터베이스의 연동 (0) | 2019.09.28 |
[B -2-3] Java 기반 스프링 환경 설정 (0) | 2019.09.27 |
[B -2-2] 롬복 라이브러리 설치 (0) | 2019.09.27 |
[B -2-1] 스프링 프로젝트 생성 (0) | 2019.09.27 |