Spring configures multiple data sources
configuration file
spring: datasource: master: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql: username: password: type: com.alibaba.druid.pool.DruidDataSource #Here is the configuration of the druid connection pool. The following is the configuration information for druid filters: stat,wall,log4j maxActive: 20 initialSize: 1 maxWait: 60000 minIdle: 1 timeBetweenEvictionRunsMillis: 60000 minEvictableIdleTimeMillis: 300000 validationQuery: select 'x' testWhileIdle: true testOnBorrow: false testOnReturn: false poolPreparedStatements: true maxOpenPreparedStatements: 20 connection-properties: druid.stat.merggSql=ture;druid.stat.slowSqlMillis=5000 slave: driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver url: jdbc:sqlserver://101.102.12.12;DatabaseName=test username: password: type: com.alibaba.druid.pool.DruidDataSource #Here is the configuration of the druid connection pool. The following is the configuration information for druid filters: stat,wall,log4j maxActive: 20 initialSize: 1 maxWait: 60000 minIdle: 1 timeBetweenEvictionRunsMillis: 60000 minEvictableIdleTimeMillis: 300000 validationQuery: select 'x' testWhileIdle: true testOnBorrow: false testOnReturn: false poolPreparedStatements: true maxOpenPreparedStatements: 20 connection-properties: druid.stat.merggSql=ture;druid.stat.slowSqlMillis=5000
The first configuration class
@Configuration @MapperScan(basePackages = "dao.master(mapper Location)", sqlSessionTemplateRef = "baseSqlSessionTemplate") public class MasterDataSourceConfig { @Bean(name = "baseDataSource") @ConfigurationProperties(prefix = "spring.datasource.master") @Primary public DataSource setDataSource() { return new DruidDataSource(); } @Bean(name = "baseTransactionManager") @Primary public DataSourceTransactionManager setTransactionManager(@Qualifier("baseDataSource") DataSource dataSource) { return new DataSourceTransactionManager(dataSource); } @Bean(name = "baseSqlSessionFactory") @Primary public SqlSessionFactory setSqlSessionFactory(@Qualifier("baseDataSource") DataSource dataSource) throws Exception { SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setDataSource(dataSource); bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:daos/master/*.xml")); return bean.getObject(); } @Bean(name = "baseSqlSessionTemplate") @Primary public SqlSessionTemplate setSqlSessionTemplate(@Qualifier("baseSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception { return new SqlSessionTemplate(sqlSessionFactory); } }
The second configuration class
@Configuration @MapperScan(basePackages = "dao.slave", sqlSessionTemplateRef = "slaveSqlSessionTemplate") public class SlaveDataSourceConfig { @Bean(name = "slaveDataSource") @ConfigurationProperties(prefix = "spring.datasource.slave") public DataSource setDataSource() { return new DruidDataSource(); } @Bean(name = "slaveTransactionManager") public DataSourceTransactionManager setTransactionManager(@Qualifier("slaveDataSource") DataSource dataSource) { return new DataSourceTransactionManager(dataSource); } @Bean(name = "slaveSqlSessionFactory") public SqlSessionFactory setSqlSessionFactory(@Qualifier("slaveDataSource") DataSource dataSource) throws Exception { SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setDataSource(dataSource); bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:daos/slave/*.xml")); return bean.getObject(); } @Bean(name = "slaveSqlSessionTemplate") public SqlSessionTemplate setSqlSessionTemplate(@Qualifier("slaveSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception { return new SqlSessionTemplate(sqlSessionFactory); } }
Catalog display
Explanatory notes
@ Configuration Properties (prefix = xxx x) specifies the prefix of the configuration item.
@ Beans (name = "xxx") are other beans of the same type, each of which needs to be identified by @Bean(name = "xxx").
@ Primary: When multiple Bean candidates appear in automatic assembly, the Bean annotated @Primary will be the preferred one, otherwise an exception will be thrown