package com.changhong.autoformtest.config;
|
|
import javax.sql.DataSource;
|
|
import org.apache.ibatis.session.SqlSessionFactory;
|
import org.mybatis.spring.SqlSessionFactoryBean;
|
import org.mybatis.spring.SqlSessionTemplate;
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Configuration;
|
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
|
import org.springframework.core.io.support.ResourcePatternResolver;
|
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
|
import org.springframework.transaction.PlatformTransactionManager;
|
|
import com.alibaba.druid.pool.DruidDataSource;
|
|
@Configuration
|
public class DataSourceConfig {
|
|
@Bean(initMethod="init",destroyMethod="close",name="dataSource")
|
@ConfigurationProperties(prefix="spring.datasource")
|
public DataSource dataSource() throws Exception {
|
return new DruidDataSource();
|
}
|
|
@Bean(name="sqlSessionFactory")
|
public SqlSessionFactory sqlSessionFactoryBean() throws Exception {
|
|
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
|
sqlSessionFactoryBean.setDataSource(dataSource());
|
//添加XML目录
|
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
|
try {
|
// sqlSessionFactoryBean.setMapperLocations(resolver.getResources(""));
|
return sqlSessionFactoryBean.getObject();
|
} catch (Exception e) {
|
e.printStackTrace();
|
throw new RuntimeException(e);
|
}
|
}
|
|
|
@Bean
|
public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) {
|
return new SqlSessionTemplate(sqlSessionFactory);
|
}
|
|
@Bean
|
public DataSourceTransactionManager transactionManager() throws Exception {
|
return new DataSourceTransactionManager(dataSource());
|
}
|
}
|