package com.blueland.ebpm.config;

import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import javax.annotation.Resource;
import javax.sql.DataSource;
import java.io.IOException;

/**
 * Session工厂的配置文件
 *
 *@author  fishkk
 *@version  1.0.0
 *@since
 *
 * 修改人信息
 *@author
 *@version
 *@since
 *
 */
public class SessionFactoryConfiguration {
    /**
     *  自动装在数据库资源bean
     */
   // @Resource
    private DataSource dataSource;

   // @Bean(name = "sqlSessionFactory")
    public SqlSessionFactoryBean createSqlSessionFactory() throws IOException {
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        //Mybatis的参数配置
        sqlSessionFactoryBean.setConfigLocation(new ClassPathResource("configuration.xml"));
        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        //启用Mybatis的全部xml文件，就不需要一个个去打开
        String packageSerchPath = "classpath*:/com/**/model/**/*Mapper.xml";
        sqlSessionFactoryBean.setMapperLocations(resolver.getResources(packageSerchPath));
        sqlSessionFactoryBean.setDataSource(dataSource);
        //实体类所在的包
        sqlSessionFactoryBean.setTypeAliasesPackage("com.**.dao,com.**.**.dao");
        return sqlSessionFactoryBean;
    }
}