@Configuration
@MapperScan(basePackages = {"com.pppp.tool"})
public class DataSourceConfig {
protected final Logger logger = LoggerFactory.getLogger(this.getClass());
/** DataSource Main 생성 */
@Bean
@Primary
@ConfigurationProperties(prefix = "spring.datasource1")
public DataSource mysql1DataSource() {
return DataSourceBuilder.create()
.type(HikariDataSource.class)
.build();
}
/** DataSource Sub 생성 */
@Bean
@ConfigurationProperties(prefix = "spring.datasource2")
public DataSource mysql2DataSource() {
return DataSourceBuilder.create()
.type(HikariDataSource.class)
.build();
}
/** sqlSessionFactory Main 생성 */
@Bean
@Primary
public SqlSessionFactory sqlSessionFactoryMain(@Autowired @Qualifier("mysql1DataSource") DataSource dataSource) throws Exception {
logger.info("SqlSessionFactory Main Start");
org.apache.ibatis.session.Configuration configuration = this.getMybatisConfig();
SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
factoryBean.setDataSource(dataSource);
factoryBean.setVfs(SpringBootVFS.class);
factoryBean.setConfiguration(configuration);
factoryBean.setTypeHandlersPackage("com.commax.tool.framework.mybatis.typehandler");
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
Resource[] resource = resolver.getResources("mybatis/**/*.xml");
factoryBean.setMapperLocations(resource);
return factoryBean.getObject();
}
/** sqlSessionFactory Sub 생성 */
@Bean
public SqlSessionFactory sqlSessionFactory(@Autowired @Qualifier("mysql2DataSource") DataSource dataSource) throws Exception {
logger.info("SqlSessionFactory SUB Start");
org.apache.ibatis.session.Configuration configuration = this.getMybatisConfig();
SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
factoryBean.setDataSource(dataSource);
factoryBean.setVfs(SpringBootVFS.class);
factoryBean.setConfiguration(configuration);
factoryBean.setTypeHandlersPackage("com.commax.tool.framework.mybatis.typehandler");
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
Resource[] resource = resolver.getResources("mybatis/**/*.xml");
factoryBean.setMapperLocations(resource);
return factoryBean.getObject();
}
/** sqlSession Main 생성 */
@Bean
@Primary
public SqlSession sqlSessionMain(@Autowired @Qualifier("sqlSessionFactoryMain") SqlSessionFactory factory) {
return new SqlSessionTemplate(factory);
}
/** sqlSession Sub 생성 */
@Bean
public SqlSession sqlSessionSub(@Autowired @Qualifier("sqlSessionFactory") SqlSessionFactory factory) {
return new SqlSessionTemplate(factory);
}
/** MybatisConfig 설정정보 */
private org.apache.ibatis.session.Configuration getMybatisConfig() {
org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();
configuration.setCacheEnabled(true);
configuration.setLazyLoadingEnabled(false);
configuration.setAggressiveLazyLoading(false);
configuration.setMultipleResultSetsEnabled(true);
configuration.setUseColumnLabel(true);
configuration.setAutoMappingBehavior(AutoMappingBehavior.PARTIAL);
configuration.setDefaultExecutorType(ExecutorType.SIMPLE);
configuration.setDefaultStatementTimeout(25000);
configuration.setMapUnderscoreToCamelCase(true);
configuration.setJdbcTypeForNull(JdbcType.NVARCHAR);
configuration.setLazyLoadTriggerMethods(new HashSet<>(Arrays.asList("equals", "clone", "hashCode", "toString")));
configuration.setLogPrefix("[SQL]");
return configuration;
}
}