/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ifish.config;

import javax.sql.DataSource;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

/**
 *
 * @author Administrator
 */
@Configuration
@EnableTransactionManagement
public class MybatisConfig {

    @Bean
    public DataSource dataSource() {
//        try {
//            ComboPooledDataSource datasourc = new ComboPooledDataSource();
//            datasourc.setDriverClass(JdbcProperties.driverClassName);
//            datasourc.setJdbcUrl(JdbcProperties.url);
//            datasourc.setUser(JdbcProperties.username);
//            datasourc.setPassword(JdbcProperties.password);
//            datasourc.setAutoCommitOnClose(JdbcProperties.autoCommitOnClose);
//            datasourc.setInitialPoolSize(JdbcProperties.initialPoolSize);
//            datasourc.setMinPoolSize(JdbcProperties.minPoolSize);
//            datasourc.setMaxPoolSize(JdbcProperties.maxPoolSize);
//            datasourc.setAcquireIncrement(JdbcProperties.acquireIncrement);
//            datasourc.setCheckoutTimeout(JdbcProperties.checkoutTimeout);
//            datasourc.setMaxIdleTime(JdbcProperties.maxIdleTime);
//            datasourc.setIdleConnectionTestPeriod(JdbcProperties.idleConnectionTestPeriod);
//            return datasourc;
//        } catch (Exception e) {
//             
//        }
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        //线上生产数据库连接
        dataSource.setUsername("ifish");
        dataSource.setPassword("ifish7pwd");
        dataSource.setUrl("jdbc:mysql://localhost:3306/myfishdbenglish?characterEncoding=UTF-8");
        return dataSource;
    }

    @Bean(name = "sqlSessionFactory")
    public SqlSessionFactoryBean sqlSessionFactory() {
        SqlSessionFactoryBean ssfb = new SqlSessionFactoryBean();
        ssfb.setDataSource(dataSource());
        ssfb.setTypeAliasesPackage("com.ifish.bean");
        try {
            org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();
            configuration.setMapUnderscoreToCamelCase(true);
            ssfb.setConfiguration(configuration);

        } catch (Exception e) {
        }

        return ssfb;
    }

    @Bean
    public MapperScannerConfigurer mpperScannnerConfigurer() {
        MapperScannerConfigurer msc = new MapperScannerConfigurer();
        msc.setSqlSessionFactoryBeanName("sqlSessionFactory");
        msc.setBasePackage("com.ifish.mapper");
        return msc;
    }

    @Bean
    PlatformTransactionManager transactionManager() {
        DataSourceTransactionManager transactionManager = new DataSourceTransactionManager();
        transactionManager.setDataSource(dataSource());
        return transactionManager;
    }

}
