package com.style24.admin.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.ShopDs;
import com.style24.core.support.env.TscConstants;

/**
 * shopDs용 Mybatis Configuration
 * 
 * @author gagamel
 * @since 2020. 10. 5
 */
@Configuration
@MapperScan(basePackages = TscConstants.BASE_PACKAGE, annotationClass = ShopDs.class, sqlSessionFactoryRef = "shopSqlSessionFactory")
public class TsaMybatisShopConfig {

	@Autowired
	private ApplicationContext applicationContext;

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

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

		return sessionFactoryBean.getObject();
	}

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

}
