@Configuration
@MapperScan(basePackages = {"***dao.mapper"},sqlSessionFactoryRef = "DbSqlFactory")
public class DataSourceConfig {
@ConfigurationProperties(prefix = "spring.datasource.")
@Bean("DataSource")
@Primary
public DataSource Db(){
return DataSourceBuilder.create().build();
}
@Bean(name = "DbSqlFactory")
public SqlSessionFactory Factory(@Qualifier("DataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean=new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*://trade/dao/mapping/*.xml"));
org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();
configuration.setMapUnderscoreToCamelCase(true);
bean.setConfiguration(configuration);
System.out.println(" datasource init successfully");
return bean.getObject();
}
@Bean(name = "SqlTemplate")
public SqlSessionTemplate Template(@Qualifier("DbSqlFactory") SqlSessionFactory factory)
{
return new SqlSessionTemplate(factory);
}
@Bean(name = "TransactionManager")
public DataSourceTransactionManager transactionManager(){
return new DataSourceTransactionManager(this.Db());
}
}