package com.ytpm.config.datasource.xjlrl;

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.xjlrl"}, sqlSessionFactoryRef = "sqlSessionFactoryXjlrl")
public class XjlrlConfig {
    @Autowired
    @Qualifier("yt_xjlrl")
    private DataSource dataSourceXjlrl;

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