스프링부트 2.0 에서 멀티 db를 사용하고 mybaits와 mysql 을 사용해보자..
/main/resources/application.properties
파일을 열고 환경 설정을 한다.
#db순서
spring.first.datasource.type=org.apache.tomcat.jdbc.pool.DataSource
spring.first.datasource.driverClassName=net.sf.log4jdbc.sql.jdbcapi.DriverSpy
spring.first.datasource.jdbc-url=db주소
spring.first.datasource.username=아디
spring.first.datasource.password=패스워드
spring.first.datasource.connectionProperties=useSSL=false;useUnicode=yes;characterEncoding=UTF-8;serverTimezone=UTC
#db순서
spring.second.datasource.type=org.apache.tomcat.jdbc.pool.DataSource
spring.second.datasource.driverClassName=com.mysql.jdbc.Driver
spring.second.datasource.jdbc-url=db주소
spring.second.datasource.username=아디.
spring.second.datasource.password=패스워드.
spring.second.datasource.connectionProperties=useSSL=false;useUnicode=yes;characterEncoding=UTF-8;serverTimezone=UTC
JSP 페이지 사용을 위해서
#JSP를 사용하겠다.
spring.mvc.view.prefix=/WEB-INF/views/
spring.mvc.view.suffix=.jsp
문자를 UTF-8사용 설정
spring.http.encoding.charset=UTF-8
첫번째 DB파일 설정
import javax.sql.DataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class FirstDataSourceConfig {
@Bean
@ConfigurationProperties(prefix = "spring.first.datasource")
public DataSource firstDataSource()
{
return DataSourceBuilder.create().build();
}
@Bean public SqlSessionFactory firstSqlSessionFactory(DataSource firstDataSource, ApplicationContext applicationContext) throws Exception
{
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(firstDataSource);
sqlSessionFactoryBean.setMapperLocations(applicationContext.getResources("classpath:mapper/first/*.xml"));
return sqlSessionFactoryBean.getObject();
}
@Bean public
SqlSessionTemplate firstSqlSessionTemplate(SqlSessionFactory firstSqlSessionFactory) throws Exception
{
return new SqlSessionTemplate(firstSqlSessionFactory);
}
}
2번째 DB파일 설정
import javax.sql.DataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class SecondDataSourceConfig {
@Bean
@ConfigurationProperties(prefix = "spring.second.datasource")
public DataSource secondDataSource()
{
return DataSourceBuilder.create().build();
}
@Bean public SqlSessionFactory secondSqlSessionFactory(DataSource secondDataSource, ApplicationContext applicationContext) throws Exception
{
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(secondDataSource);
sqlSessionFactoryBean.setMapperLocations(applicationContext.getResources("classpath:mapper/second/*.xml"));
return sqlSessionFactoryBean.getObject();
}
@Bean public
SqlSessionTemplate secondSqlSessionTemplate(SqlSessionFactory secondSqlSessionFactory) throws Exception
{
return new SqlSessionTemplate(secondSqlSessionFactory);
}
}
SQL파일은 여기에 위치한다.
JSP파일은 여기에 위치한다.
서비스를 만들고
DAO역시 만들어준다.
컨트롤러 뷰 화면 입니다.
정리완료...
'SPRING FRAMEWORK' 카테고리의 다른 글
카페24 SMS 스프링에서 보내기 restTemplate 활용하기 (0) | 2019.12.13 |
---|---|
스프링 모바일 웹 제작하기 (0) | 2019.11.04 |
GOOGLE Chrome (0) | 2018.09.18 |
스프링 시큐리티 REST 안드로이드 세팅하기 (0) | 2018.02.11 |
SPRING FRAMEWORK 시작하기 스프링 시작하기 1 (0) | 2016.05.03 |