package com.mortals.xhx.base.framework.config;

import com.mortals.framework.springcloud.config.mybatis.AbstractMybatisConfiguration;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

@Configuration
//@AutoConfigureAfter(DataSourceAutoConfiguration.class)
public class MybatisConfiguration extends AbstractMybatisConfiguration {
    private static Log logger = LogFactory.getLog(MybatisConfiguration.class);

    // 配置类型别名
    @Value("${spring.application.name}")
    private String name;
    // 配置类型别名
    @Value("${mybatis.root-path}")
    private String rootPath;

    // 配置类型别名
    @Value("${mybatis.type-aliases-package}")
    private String typeAliasesPackage;

    // 配置mapper的扫描，找到所有的mapper.xml映射文件
    @Value("${mybatis.mapper-locations}")
    private String mapperLocations;

    // 加载全局的配置文件
    @Value("${mybatis.config-location}")
    private String configLocation;

    // 提供SqlSeesion
    @Bean(name = "sqlSessionFactory")
    public SqlSessionFactory sqlSessionFactoryBean(@Qualifier("dataSource") DataSource dataSource) {
        return super.getSqlSessionFactoryBean(dataSource);
    }

    @Override
    public String getTypeAliasesRootPackage() {
        return rootPath;
    }

    @Override
    public String getSqlMappers() {
        return mapperLocations;
    }

    @Override
    public String getMybatisConfig() {
        return configLocation;
    }

    @Override
    public String getTypeAliasesPackage() {
        logger.info("typeAliasesPackage:"+typeAliasesPackage);
        return typeAliasesPackage;
    }

}