package com.style24.batch.support.config;

import javax.sql.DataSource;

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.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.style24.core.support.annotation.CjDs;
import com.style24.core.support.env.TscConstants;

/**
 * cjDs용 Mybatis Configuration
 * 
 * @author moon
 * @since 2021. 04. 09
 */
@Configuration
@MapperScan(basePackages = TscConstants.BASE_PACKAGE, annotationClass = CjDs.class, sqlSessionFactoryRef = "cjSqlSessionFactory")
public class TsbMybatisCjConfig {

	@Autowired
	private ApplicationContext applicationContext;

	@Bean(name = "cjSqlSessionFactory")
	public SqlSessionFactory cjSqlSessionFactory(@Qualifier("cjDataSource") DataSource dataSource) throws Exception {
		SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean();

		sessionFactoryBean.setDataSource(dataSource);
		sessionFactoryBean.setTypeAliasesPackage(TscConstants.DOMAIN_PACKAGE);
		sessionFactoryBean.setConfigLocation(applicationContext.getResource("classpath:persistence/mybatis-cj-config.xml"));
		sessionFactoryBean.setMapperLocations(applicationContext.getResources(TscConstants.MAPPER_LOCATION_PATH + "/cj/*.xml"));

		return sessionFactoryBean.getObject();
	}

	@Bean(name = "cjSqlSessionTemplate")
	public SqlSessionTemplate cjSqlSessionTemplate(@Qualifier("cjSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
		return new SqlSessionTemplate(sqlSessionFactory);
	}

}
