数据源切换
1.定义AbstractRoutingDataSource,使其能够支持多个数据源
@Component("dataSource")
@Primary
public class DynamicDataSource extends AbstractRoutingDataSource {
@Autowired
@Qualifier("readDataSource")
private DataSource readDataSource;
@Autowired
@Qualifier("writeDataSource")
private DataSource writeDataSource;
@Override
protected Object determineCurrentLookupKey() {
return DataSourceContextHolder.getDbType();
}
@Override
public void afterPropertiesSet() {
Map<Object,Object> map = new HashMap<>();
map.put("readDataSource",readDataSource);
map.put("writeDataSource",writeDataSource);
setTargetDataSources(map);
setDefaultTargetDataSource(writeDataSource);
super.afterPropertiesSet();
}
}
2.数据库切换类
@Component
@Lazy(fals