728x90
반응형
Gradle 설정
implementation group: 'org.mybatis.spring.boot', name: 'mybatis-spring-boot-starter', version: '2.2.2'
implementation group: 'org.mariadb.jdbc', name: 'mariadb-java-client', version: '3.0.3'
implementation 'org.springframework.boot:spring-boot-starter-jdbc'
application.properties
spring.datasource.hikari.driver-class-name=org.mariadb.jdbc.Driver
spring.datasource.hikari.jdbc-url=jdbc:mariadb://localhost:3306/ordersvc
spring.datasource.hikari.username=pli
spring.datasource.hikari.password=1234
spring.datasource.hikari.connectionTimeout=5000
spring.datasource.hikari.validationTimeout=1000
spring.datasource.hikari.maximumPoolSize=10
mybatis-config.xml (/resource/mybatis-config.xml)
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<!-- A_Column > aColumn으로 자동 매핑 -->
<setting name="mapUnderscoreToCamelCase" value="true"/>
<!-- 데이터베이스 응답 시간 타임아웃 -->
<setting name="defaultStatementTimeout" value="60"/>
<!-- 파라미터에 null이 있으면 에러 처리 -->
<setting name="jdbcTypeForNull" value="NULL"/>
<!-- 결과가 Map타입일 경우 Null인 컬럼값을 그대로 Null 처리 -->
<setting name="callSettersOnNulls" value="true"/>
</settings>
</configuration>
이렇게 한 후 실행하면 아래 에러를 만난다.
if you want an embedded database (h2 hsql or derby) please put it on the classpath.
datasource 생성을 시도하는데 설정이 되어 있지 않아서 발생하는 에러다.
DatabaseConfig.java
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
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.boot.context.properties.ConfigurationProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import javax.sql.DataSource;
@Configuration
@MapperScan(basePackages = {"com.heypli.productsvc.mapper"}, annotationClass = ProdDataBase.class)
@Primary
@EnableTransactionManagement
public class DatabaseConfig {
@Bean(name="hikariConfig")
@ConfigurationProperties(prefix = "spring.datasource.hikari")
public HikariConfig hikariConfig() {
return new HikariConfig();
}
@Bean(name = "dataSource")
public HikariDataSource hikariDataSource(@Autowired @Qualifier("hikariConfig") HikariConfig hikariConfig) throws Exception {
HikariDataSource dataSource = new HikariDataSource(hikariConfig());
return dataSource;
}
@Bean(name = "sqlSessionFactory")
public SqlSessionFactory sqlSessionFactory(@Autowired @Qualifier("dataSource") DataSource dataSource,
ApplicationContext applicationContext) throws Exception {
SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
factoryBean.setDataSource(dataSource);
factoryBean.setTypeAliasesPackage("com.heypli.prodsvc.domain");
factoryBean.setConfigLocation(applicationContext.getResource("classpath:mybatis-config.xml"));
factoryBean.setMapperLocations(applicationContext.getResources("classpath:mapper/*.xml"));
return factoryBean.getObject();
}
@Bean(name="sqlSession")
public SqlSessionTemplate sqlSessionTemplate(@Autowired @Qualifier("sqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
mybatis의 sqlSessionFactory를 반환하는 메소드가 있는 클래스에 Configuration붙여준다.
mybatis는 sqlSession을 통해 쿼리/프로시저를 수행하고 factory가 sqlSession을 생성해준다.
그래서 sqlSessionFactory, sqlSessionTemplate 메소드가 필요하다.
sqlSessionTemplate은 쓰레드에 안전하고 여러개의 DAO나 매퍼에서 공유할 수 있다.
@EnableTransactionManagement는 mybatis트랜잭션 매니저를 어노테이션을 관리한다는 것을 뜻함.
DB 두 개 이상 설정할 때는 아래 링크를 참고
https://tweety1121.tistory.com/entry/Spring-boot-multiple-database-%EC%84%A4%EC%A0%95-mybatishikari
728x90
반응형
'Spring' 카테고리의 다른 글
Spring Cloud Gateway Global Error Handler (0) | 2022.02.03 |
---|---|
Spring @Valid 어노테이션으로 파라미터 검증하기 (0) | 2022.02.02 |
Spring @Configuration @Bean 스프링 빈 등록 방법 (0) | 2022.01.30 |
스프링 빈 라이프 사이클(Spring Bean Life Cycle) (0) | 2022.01.30 |
Spring ResourceHttpRequestHandler (0) | 2022.01.28 |