parent
34ec675661
commit
5c72f37b8b
@ -1,27 +1,88 @@
|
||||
package egovframework.config;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import net.ttddyy.dsproxy.listener.logging.SLF4JLogLevel;
|
||||
import net.ttddyy.dsproxy.support.ProxyDataSourceBuilder;
|
||||
import org.mybatis.spring.boot.autoconfigure.ConfigurationCustomizer;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Profile;
|
||||
import org.springframework.context.annotation.Primary;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
/**
|
||||
* MyBatis Configuration
|
||||
* MyBatis Configuration with DataSource Proxy
|
||||
*
|
||||
* This class configures MyBatis by adding custom interceptors.
|
||||
* datasource-proxy를 이용하여 동적 쿼리와 복잡한 include된 쿼리의
|
||||
* 파라미터 바인딩된 결과를 로깅하도록 설정
|
||||
*/
|
||||
@Slf4j
|
||||
@Configuration
|
||||
@Profile({"local", "dev"}) // local과 dev 프로파일에서만 활성화
|
||||
//@Profile({"local", "dev"}) // local과 dev 프로파일에서만 활성화
|
||||
public class MyBatisConfig {
|
||||
|
||||
/**
|
||||
* Customizes the MyBatis configuration by adding the query interceptor.
|
||||
* 기본 DataSource Properties 설정
|
||||
*/
|
||||
@Bean
|
||||
@Primary
|
||||
@ConfigurationProperties("spring.datasource")
|
||||
public DataSourceProperties dataSourceProperties() {
|
||||
return new DataSourceProperties();
|
||||
}
|
||||
|
||||
/**
|
||||
* 실제 DataSource 생성 (프록시되지 않은 원본)
|
||||
*/
|
||||
@Bean
|
||||
@ConfigurationProperties("spring.datasource.hikari")
|
||||
public DataSource actualDataSource(DataSourceProperties properties) {
|
||||
return properties.initializeDataSourceBuilder().build();
|
||||
}
|
||||
|
||||
/**
|
||||
* DataSource Proxy 설정
|
||||
*
|
||||
* datasource-proxy를 사용하여 실제 쿼리와 파라미터 바인딩 결과를 로깅
|
||||
* - 동적 쿼리 (if, choose 등) 처리된 결과 로깅
|
||||
* - include된 복잡한 쿼리 결과 로깅
|
||||
* - 파라미터 바인딩된 최종 쿼리 로깅
|
||||
*/
|
||||
@Bean
|
||||
@Primary
|
||||
public DataSource dataSource(@Qualifier("actualDataSource") DataSource actualDataSource) {
|
||||
return ProxyDataSourceBuilder
|
||||
.create(actualDataSource)
|
||||
.name("XIT-Framework-DataSource")
|
||||
// 쿼리 로깅 리스너 설정 - 실행된 모든 쿼리와 파라미터 바인딩 결과 로깅
|
||||
.logQueryBySlf4j(SLF4JLogLevel.INFO, "go.kr.project.sql.query")
|
||||
// 멀티라인으로 쿼리 포맷팅하여 가독성 향상
|
||||
.multiline()
|
||||
.build();
|
||||
}
|
||||
|
||||
/**
|
||||
* MyBatis Query Interceptor 주입
|
||||
*/
|
||||
@Autowired
|
||||
private MyBatisQueryInterceptor myBatisQueryInterceptor;
|
||||
|
||||
/**
|
||||
* MyBatis Configuration Customizer
|
||||
*
|
||||
* @param interceptor The MyBatis query interceptor
|
||||
* @return A ConfigurationCustomizer that adds the interceptor to MyBatis
|
||||
* MyBatis에 커스텀 인터셉터를 추가하여
|
||||
* datasource-proxy와 함께 상세한 쿼리 분석 제공
|
||||
*/
|
||||
@Bean
|
||||
public ConfigurationCustomizer mybatisConfigurationCustomizer(MyBatisQueryInterceptor interceptor) {
|
||||
return configuration -> configuration.addInterceptor(interceptor);
|
||||
public ConfigurationCustomizer mybatisConfigurationCustomizer() {
|
||||
return configuration -> {
|
||||
// 커스텀 쿼리 인터셉터 추가
|
||||
configuration.addInterceptor(myBatisQueryInterceptor);
|
||||
log.info("MyBatis Query Interceptor가 등록되었습니다 - 상세 쿼리 분석 활성화");
|
||||
};
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue