package com.nantian.cloud.query.config;

import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.core.MybatisXMLLanguageDriver;
import com.baomidou.mybatisplus.core.config.GlobalConfig;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.type.JdbcType;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;

/**
 * @author suixin
 * @date 2022/2/19
 */
@Configuration
@MapperScan(basePackages = "com.nantian.cloud.query.mapper.jljc",sqlSessionTemplateRef  = "ds3SqlSessionTemplate")
public class MybatisPlusConfigds3 {
    //主数据源 ds3数据源
    @Bean("ds3SqlSessionFactory")
    public SqlSessionFactory ds3SqlSessionFactory(@Qualifier("dataSource3") DataSource dataSource) throws Exception {
        MybatisSqlSessionFactoryBean sqlSessionFactory = new MybatisSqlSessionFactoryBean();
        //注入要使用的数据源
        sqlSessionFactory.setDataSource(dataSource);
        //创建配置类(这个类包含了mybatis的所有配置信息)
        MybatisConfiguration configuration = new MybatisConfiguration();
        //指定动态SQL生成的默认语言
        configuration.setDefaultScriptingLanguage(MybatisXMLLanguageDriver.class);
        //标注设置为空的数据
        configuration.setJdbcTypeForNull(JdbcType.NULL);
        //把配置好的新配置信息放进去
        sqlSessionFactory.setConfiguration(configuration);
        //标注sqlSession去哪里扫描mapper文件
        sqlSessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().
                getResources("classpath:mapper/jljc/*.xml"));
        //给sqlSession装插件，这里加装的是分页插件
        sqlSessionFactory.setPlugins(new Interceptor[]{
                new PaginationInterceptor()
//                new PerformanceInterceptor()
//                        .setFormat(false),
        });
        //标注不开启 LOGO
        sqlSessionFactory.setGlobalConfig(new GlobalConfig().setBanner(false));
        return sqlSessionFactory.getObject();
    }

    @Primary
    @Bean(name = "ds3TransactionManager")
    public DataSourceTransactionManager ds3TransactionManager(@Qualifier("dataSource3") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    @Primary
    @Bean(name = "ds3SqlSessionTemplate")
    public SqlSessionTemplate ds3SqlSessionTemplate(@Qualifier("ds3SqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}
