package com.ytpm.config.datasource.dyz;

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.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import javax.sql.DataSource;

/**
 * 答题王数据源
 */
@Configuration
@MapperScan(basePackages = {"com.ytpm.dao.dyz"}, sqlSessionFactoryRef = "sqlSessionFactoryDyz")
public class DyzConfig {

    @Autowired
    @Qualifier("yt_dyz")
    private DataSource dataSourceDyz;

    /**
     * 自定义Sql会话，启动类配置不生效，子类分别配置mapperLocation和驼峰映射
     */
    @Bean
    public SqlSessionFactory sqlSessionFactoryDyz() throws Exception {
        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
        factoryBean.setDataSource(dataSourceDyz);
        factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/dyz/*.xml"));
        org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();
        configuration.setMapUnderscoreToCamelCase(true);
        factoryBean.setConfiguration(configuration);
        return factoryBean.getObject();
    }

    @Bean
    public SqlSessionTemplate sqlSessionTemplateDyz() throws Exception {
        return new SqlSessionTemplate(sqlSessionFactoryDyz());
    }
}
