@Configuration
@MapperScan(basePackages = “com.asiainfo.mapper”,
sqlSessionTemplateRef = “db1SqlSessionTemplate”)
//是否使用数据库
@ConditionalOnExpression("’${cmsspring.datasource.enable}’ == ‘true’")
public class DataSource1Config {
//获取配置文件的内容
@Autowired
private JdbcSettings JdbcSettings;
@Bean(name = "db1DataSource")
@Primary
public DataSource testDataSource() {
DruidDataSource dataSource = new DruidDataSource();
dataSource.setDriverClassName(JdbcSettings.getDriverClassName());
dataSource.setUrl(JdbcSettings.getUrl());
dataSource.setUsername(JdbcSettings.userName);
dataSource.setPassword(JdbcSettings.getPassword());
return dataSource;
}
@Bean(name = "db1SqlSessionFactory")
@Primary
public SqlSessionFactory testSqlSessionFactory(@Qualifier("db1DataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
bean.setMapperLocations(new PathMatchingResourcePatternResolver()
.getResources("classpath:mapper/*.xml"));
return bean.getObject();
}
@Bean(name = "db1TransactionManager")
@Primary
public DataSourceTransactionManager testTransactionManager(@Qualifier("db1DataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean(name = "db1SqlSessionTemplate")
@Primary
public SqlSessionTemplate testSqlSessionTemplate(
@Qualifier("db1SqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}