이번에 다루고자 하는 Spring + Mybatis 포스팅은 다중DB 설정에 대하여 작성해보도록 하겠습니다.
SqlSession 객체에 "@Resource" 어노테이션을 이용하여 Datasource 구분을 지어 여러개의 DB 호출이 가능합니다.
이전 Mybatis 연동설정법은 다음 포스팅을 참고해주세요
2014/11/14 - [개발에필요한연동법/스프링연동] - Spring3 Maven을 이용하여 pom.xml에 oracle,mysql,mssql jdbc 라이브러리 등록하기
2014/11/14 - [개발에필요한연동법/스프링연동] - Spring3 + MyBatis 기본설정 + 연동테스트 후 쿼리로그 확인해보기
2014/11/14 - [개발에필요한연동법/스프링연동] - Spring3 + Mybatis연동에 추가로 트랜잭션 설정 하여 실패시 Rollback 처리하기
@Resource 어노테이션을 위해서는 pom.xml에 하단 dependency를 추가해주도록 합니다.
1 2 3 4 5 | < dependency > < groupId >javax.annotation</ groupId > < artifactId >javax.annotation-api</ artifactId > < version >1.2</ version > </ dependency > |
기존 application-config.xml 내에 Datasource 설정코드가 하나 설정 되어있었습니다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | < bean id = "dataSource" class = "org.apache.commons.dbcp.BasicDataSource" destroy-method = "close" > < property name = "driverClassName" value = "드라이버클래스" /> < property name = "username" value = "계정" /> < property name = "password" value = "패스워드" /> </ bean > < bean id = "sqlSessionFactory" class = "org.mybatis.spring.SqlSessionFactoryBean" > < property name = "dataSource" ref = "dataSource" /> < property name = "mapperLocations" value = "classpath*:query/**" /> </ bean > < bean id = "sqlSession" class = "org.mybatis.spring.SqlSessionTemplate" > < constructor-arg ref = "sqlSessionFactory" /> </ bean > < bean id = "transactionManager" class = "org.springframework.jdbc.datasource.DataSourceTransactionManager" > < property name = "dataSource" ref = "dataSource" /> </ bean > < tx:advice id = "txAdvice" transaction-manager = "transactionManager" > < tx:attributes > < tx:method name = "save*" rollback-for = "Exception" /> < tx:method name = "update*" rollback-for = "Exception" /> < tx:method name = "delete*" rollback-for = "Exception" /> </ tx:attributes > </ tx:advice > |
기존 설정되어있는 위 4가지 bean 태그와 tx:advice 태그를 복사하여 붙여넣기를 합니다.
각 태그들의 id / ref를 변경해주도록 합니다.
추가로 tx:advice 의 transaction-manager속성같을 변경합니다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | < bean id = "dataSource2" class = "org.apache.commons.dbcp.BasicDataSource" destroy-method = "close" > < property name = "driverClassName" value = "드라이버클래스" /> < property name = "username" value = "계정" /> < property name = "password" value = "패스워드" /> </ bean > < bean id = "sqlSessionFactory2" class = "org.mybatis.spring.SqlSessionFactoryBean" > < property name = "dataSource" ref = "dataSource2" /> < property name = "mapperLocations" value = "classpath*:query/**" /> </ bean > < bean id = "sqlSession2" class = "org.mybatis.spring.SqlSessionTemplate" > < constructor-arg ref = "sqlSessionFactory2" /> </ bean > < bean id = "transactionManager2" class = "org.springframework.jdbc.datasource.DataSourceTransactionManager" > < property name = "dataSource" ref = "dataSource2" /> </ bean > < tx:advice id = "txAdvice2" transaction-manager = "transactionManager2" > < tx:attributes > < tx:method name = "save*" rollback-for = "Exception" /> < tx:method name = "update*" rollback-for = "Exception" /> < tx:method name = "delete*" rollback-for = "Exception" /> </ tx:attributes > </ tx:advice > |
기존 aop:config 태그내에 다음 태그 추가를 해주도록 합니다.
1 | < aop:advisor id = "transactionAdvisor2" pointcut-ref = "serviceOperation" advice-ref = "txAdvice2" /> |
본인이 사용하고자 하는 db의 가상호출 테이블은 다음과 같습니다.
위 테이블의 COUNT 쿼리를 실행하여 콘솔창에 카운트 수를 출력해보고자 합니다.
위처럼 각 테이블마다 카운트가 이클립스 콘솔창에 동일하게 출력된다면 mybatis 다중 db 연동이 성공한 것일 겁니다.
Controller/Service/Dao 부분 샘플코드를 작성해보도록 합니다.
1 2 3 4 5 6 7 8 | @RequestMapping ( "/multipledb" ) public void multipledb(){ try { studyService.multipledb(); } catch (SQLException e) { e.printStackTrace(); } } |
1 | public void multipledb() throws SQLException; |
1 2 3 4 | public void multipledb() throws SQLException { dao.multipledb1(); dao.multipledb2(); } |
1 2 | public void multipledb1() throws SQLException; public void multipledb2() throws SQLException; |
1 2 3 4 5 6 7 8 | public void multipledb1() throws SQLException { int count = query.selectOne( "query.multipledb1" ); System.out.println( "test.mybatis_table COUNT 결과:" +count); } public void multipledb2() throws SQLException { int count = query2.selectOne( "query.multipledb2" ); System.out.println( "sample.grid_sample COUNT 결과:" +count); } |
DaoImpl 기존 Sqlsession 인터페이스부분을 다음처럼 변경을 해줍니다.
1 2 3 4 5 6 7 | @Autowired @Resource (name= "sqlSession" ) private SqlSession query; @Autowired @Resource (name= "sqlSession2" ) private SqlSession query2; |
마지막 query.xml에 다음 코드를 추가로 결과 확인을 해보도록 하겠습니다.
1 2 3 4 5 6 7 8 | < select id = "multipledb1" resultType = "Integer" > SELECT COUNT(*) FROM mybatis_table </ select > < select id = "multipledb2" resultType = "Integer" > SELECT COUNT(*) FROM grid_sample </ select > |
위처럼 @Resource 어노테이션별로 실행한 결과를 콘솔상에서 확인이 되었습니다.
실행결과, 상단 DB툴에서 조회한 COUNT 갯수와 동일한 결과가 나온것을 확인 할 수 있었습니다.
by 개발로짜
Spring3 spring-task를 이용하여 스케줄러 연동 및 동작시켜보기 + DB연동(MySQL기준) (0) | 2014.11.24 |
---|---|
Spring3 + ibatis(아이바티스) 연동해보고 쿼리결과값 콘솔에 출력해보기 (5) | 2014.11.17 |
Spring3 + Mybatis연동에 추가로 트랜잭션 설정 하여 실패시 Rollback 처리하기 (2) | 2014.11.14 |
Spring3 + MyBatis 기본설정 + 연동테스트 후 쿼리로그 확인해보기 (8) | 2014.11.14 |
Spring3 Maven을 이용하여 pom.xml에 oracle,mysql,mssql jdbc 라이브러리 등록하기 (0) | 2014.11.14 |