Mybatis로 connection을 1개 연동했다.
이제 개발서버 DB 2개, 운영서버 DB 2개 총 4개의 DB Connection을 연동해서 간단한 SELECT문을 긁어오게끔 해보자
1개를 연동했을 때 보다 폴더와 패키지가 추가되었다.
원래 realdb / devdb 이렇게만 폴더나 패키지를 나눴는데 connection이 꼬이는 일이 발생해서
저렇게 세부적으로 나눠서 mapper파일을 넣어놓았다.
DevDbAdminConfig.java
package com.smartplus.connect.judy.config;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.env.Environment;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import javax.sql.DataSource;
@Configuration
@MapperScan(value="com.smartplus.connect.judy.mapper.devdb.admindb", sqlSessionFactoryRef="devdbadminSqlSessionFactory")
//@EnableTransactionManagement
@Slf4j
public class DevDbAdminConfig {
@Autowired
private Environment env;
private static final String prefix = "spring.devdbadmin.datasource.hikari.";
@Bean(name = "devdbadminSource", destroyMethod = "close")
public HikariDataSource devdbadminSource() {
HikariConfig config = new HikariConfig();
config.setUsername(env.getProperty(prefix+"username"));
config.setPassword(env.getProperty(prefix+"password"));
// config.setDriverClassName(env.getProperty(prefix+"driverClassName"));
config.setJdbcUrl( env.getProperty(prefix+"jdbc-url") );
config.setMaxLifetime( Long.parseLong(env.getProperty(prefix+"max-lifetime")) );
config.setConnectionTimeout(Long.parseLong( env.getProperty(prefix+"connection-timeout")));
config.setValidationTimeout(Long.parseLong( env.getProperty(prefix+"validation-timeout")));
config.addDataSourceProperty( "cachePrepStmts" , env.getProperty(prefix+"data-source-properties.cachePrepStmts"));
config.addDataSourceProperty( "prepStmtCacheSize" , env.getProperty(prefix+"data-source-properties.prepStmtCacheSize"));
config.addDataSourceProperty( "prepStmtCacheSqlLimit" , env.getProperty(prefix+"data-source-properties.prepStmtCacheSqlLimit") );
config.addDataSourceProperty( "useServerPrepStmts" , env.getProperty(prefix+"data-source-properties.useServerPrepStmts") );
config = new HikariDataSource( config );
HikariDataSource dataSource = new HikariDataSource( config );
return dataSource;
}
@Bean(name = "devdbadminSqlSessionFactory")
public SqlSessionFactory dbadminSqlSessionFactory(@Qualifier("devdbadminSource") DataSource devdbadminSource) throws Exception {
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(devdbadminSource);
sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:devdb/admindb/*.xml"));
return sqlSessionFactoryBean.getObject();
}
@Bean(name = "devdbadminSqlSessionTemplate")
public SqlSessionTemplate dbadminSqlSessionTemplate(SqlSessionFactory devdbadminSqlSessionFactory) throws Exception {
return new SqlSessionTemplate(devdbadminSqlSessionFactory);
}
@Bean(name="devdbadminTransactionManager")
public PlatformTransactionManager dbadminTransactionManager(@Qualifier("devdbadminSource") DataSource devdbadminSource) {
DataSourceTransactionManager transactionManager = new DataSourceTransactionManager();
transactionManager.setDataSource(devdbadminSource);
return transactionManager;
}
}
config 파일 중 하나인 DevDbAdminConfig를 보면
@MapperScan(value="com.smartplus.connect.judy.mapper.devdb.admindb", sqlSessionFactoryRef="devdbadminSqlSessionFactory")
MapperScan에서 하기 경로로 스캔하도록 설정하면 그 경로에 있는 Mapper를 사용하는 SQL은 내가 DevDbAdminConfig.java에서 설정한 connection을 이용한다.
이렇게 connection을 구분해줘서 DB와 연동하면 끝!
connection이 구분되서 잘 들어온거 확인 완료!
반응형
'작업일지 > 신규업체 자동연동' 카테고리의 다른 글
#4 ) log4j 이용해서 로그에 쿼리 남기기 (1) | 2021.03.26 |
---|---|
#2 ) MyBatis와 SpringBoot 연동하기 (0) | 2021.03.25 |
#1 ) 신규업체 자동연동 프로그램을 만들어보자 (0) | 2021.03.25 |