package com.ytpm.config.datasource.qnmjz;

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.qnmjz"}, sqlSessionFactoryRef = "sqlSessionFactoryQnmjz")
public class QnmjzConfig {
    @Autowired
    @Qualifier("yt_qnmjz")
    private DataSource dataSourceQnmjz;

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