본문 바로가기

개발중/Spring

DB 여러계정 연동

728x90
반응형

PROJECT > src > resources > application.properties 

 

이 파일은 스프링 부트가 애플리케이션을 구동할 때 자동으로 로딩하는 파일이다.

ley / value 형식으로 값을 정의하면 애플리케이션에서 참조하여 사용할 수 있다.


내가 신기한 부분은 이렇게 여러 DB 를 Conection 하는 것

#--- DB
spring.aaaa.hikari.driver-class-name=XXXXXXX
spring.aaaa.hikari.jdbc-url=XXXXXXX
spring.aaaa.hikari.username=XXXXXXX
spring.aaaa.hikari.password=XXXXXXX
spring.aaaa.hikari.maximumPoolSize=XXXXXXX
spring.aaaa.hikari.minimumIdle=XXXXXXX
spring.aaaa.hikari.connectionTimeout=XXXXXXX

spring.bbb.hikari.driver-class-name=XXXXXXX
spring.bbb.hikari.jdbc-url=XXXXXXX
spring.bbb.hikari.username=XXXXXXX
spring.bbb.hikari.password=XXXXXXX
spring.bbb.hikari.maximumPoolSize=XXXXXXX
spring.bbb.hikari.minimumIdle=XXXXXXX
spring.bbb.hikari.connectionTimeout=XXXXXXX

spring.ccc.hikari.driver-class-name=XXXXXXX
spring.ccc.hikari.jdbc-url=XXXXXXX
spring.ccc.hikari.username=XXXXXXX
spring.ccc.hikari.password=XXXXXXX
spring.ccc.hikari.maximumPoolSize=XXXXXXX
spring.ccc.hikari.minimumIdle=XXXXXXX0
spring.ccc.hikari.connectionTimeout=XXXXXXX

 

hikari 참조

https://woowabros.github.io/experience/2020/02/06/hikaricp-avoid-dead-lock.html

만약에 내가 aaaa DB 를 연결하고 싶다면 

아래 처럼 aaaa 의 DB 정보를 연결 해준다.

 

package com.soobin.aaaa.db.config;

import javax.sql.DataSource;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import com.zaxxer.hikari.HikariDataSource;

@Configuration
@EnableConfigurationProperties	
public class aaaaDataSourceConfig {
	
	@Bean(name = "aaaaDataSource")
	@Qualifier("aaaaDataSource")
	@ConfigurationProperties(prefix = "spring.aaaa.hikari")
	
	public DataSource aaaaDataSource() {
		return DataSourceBuilder.create().type(HikariDataSource.class).build();
	}
	 
	@Bean(name = "aaaaSqlSessionFactory")
	public SqlSessionFactory pomsSqlSessionFactory(@Autowired @Qualifier("aaaaDataSource") DataSource aaaaDataSource,
			ApplicationContext applicationContext) throws Exception{
		
		SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
		sqlSessionFactoryBean.setDataSource(pomsDataSource);
		sqlSessionFactoryBean.setMapperLocations(applicationContext.getResources("classpath:mybatis/aaaa/**/*.xml"));
		
		return sqlSessionFactoryBean.getObject();
	}
	
	@Bean(name = "aaaaSqlSession")
	public SqlSession pomsSqlSession(@Autowired @Qualifier("aaaaSqlSessionFactory") SqlSessionFactory aaaaSqlSessionFactory) throws Exception{
		
		return new SqlSessionTemplate(aaaaSqlSessionFactory);
	}

}

 그리고 이제 아래 경로에 xml 파일을 생성해준다.

 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="type">
	<select id="getsoobinType" resultType="~~~~~~~">
		 
	</select>
</mapper>

 

classpath:mybatis/aaaa/**/*.xml


dao 에서 mabatis 에 접근 하려면 아래처럼

package com.~~.~~.api.~~.~~.dao;

import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import com.~~.~~.api.~~.~~.vo.soobinVo;

public class soobinnDao {

	private static final String NAME_SPACE = "type.";
    
	@Autowired @Qualifier("aaaaqlSession")
	private SqlSession aaaaSqlSession;
	
	public List<soobinVo> getSooBinVoList() {

		List<soobinVo> list = aaaaSqlSession.selectList(NAME_SPACE + "getsoobinType");
		return list;
	}
}

728x90
반응형

'개발중 > Spring' 카테고리의 다른 글

@RestController - @Api 에서 데이터 받는 법  (0) 2021.05.18
mysql / my batis 여러개 업데이트  (0) 2021.05.18
Demon 이란  (0) 2021.02.19
Data → Excel 변환  (0) 2021.02.15
이클립스 Vue 프로젝트 만들기  (0) 2021.01.30