package com.ytpm.config.datasource.nofeeds;

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;
/**
 * NoFeeds数据源
 */
@Configuration
@MapperScan(basePackages = {"com.ytpm.dao.nofeeds"}, sqlSessionFactoryRef = "sqlSessionFactoryNoFeeds")
public class NoFeedsConfig {

    @Autowired
    @Qualifier("yt_nofeeds")
    private DataSource dataSourceNofeeds;

    /**
     * 自定义Sql会话，启动类配置不生效，子类分别配置mapperLocation和驼峰映射
     */
    @Bean
    public SqlSessionFactory sqlSessionFactoryNoFeeds() throws Exception {
        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
        factoryBean.setDataSource(dataSourceNofeeds);
        factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/nofeeds/*.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 sqlSessionTemplateFeeds() throws Exception {
        return new SqlSessionTemplate(sqlSessionFactoryNoFeeds());
    }
}
