数据源配置:有几个数据源就建几个类
@Configuration
//对不同数据库的mapper分包扫描
@MapperScan(basePackages = "com.mapper.census",sqlSessionFactoryRef="censusSqlSessionFactory")
public class CensusConfig {
@Value("${mybatis_config_location}")
private String mybatisConfigFilePath;
@Value("${census_mapper_locations}")
private String mapperPath;
@Value("${mybatis_entity_package}")
private String entityPackage;
@Bean(name = "censusDataSource")
public DataSource getCensusDataSource() {
BoneCPDataSource datasource = new BoneCPDataSource();
datasource.setDriverClass("com.mysql.jdbc.Driver");
//可在这里注入配置文件中属性
datasource.setJdbcUrl(url);
datasource.setUsername(username);
datasource.setPassword(password);
return datasource;
}
@Bean(name = "censusSqlSessionFactory")
public SqlSessionFactoryBean createSqlSessionFactoryBean(@Qualifier("censusDataSource")DataSource censusDataSource) throws IOException {
return SqlSessionFactoryUtil.getSqlSessionFactoryBean(mybatisConfigFilePath,mapperPath,censusDataSource,entityPackage);
}
}