SpringBoot实现多数据源配置详解( 二 )

properties = new LinkedHashMap<>();public String getDataSourceClassName() {return this.dataSourceClassName;}public void setDataSourceClassName(String dataSourceClassName) {this.dataSourceClassName = dataSourceClassName;}public Map getProperties() {return this.properties;}public void setProperties(Map properties) {this.properties = properties;}}static class DataSourceBeanCreationException extends BeanCreationException {private static final long serialVersionUID = 1L;private final BaseDataSourceProperties properties;private final EmbeddedDatabaseConnection connection;DataSourceBeanCreationException(String message, BaseDataSourceProperties properties,EmbeddedDatabaseConnection connection) {super(message);this.properties = properties;this.connection = connection;}public BaseDataSourceProperties getProperties() {return this.properties;}public EmbeddedDatabaseConnection getConnection() {return this.connection;}}}mybatis对应的base属性文件
public class BaseMybatisProperties {private String configLocation;private String[] mapperLocations;private String typeAliasesPackage;private String typeHandlersPackage;private boolean checkConfigLocation = false;private ExecutorType executorType;private Configuration configuration;public String getConfigLocation() {return this.configLocation;}public void setConfigLocation(String configLocation) {this.configLocation = configLocation;}@Deprecatedpublic String getConfig() {return this.configLocation;}@Deprecatedpublic void setConfig(String config) {this.configLocation = config;}public String[] getMapperLocations() {return this.mapperLocations;}public void setMapperLocations(String[] mapperLocations) {this.mapperLocations = mapperLocations;}public String getTypeHandlersPackage() {return this.typeHandlersPackage;}public void setTypeHandlersPackage(String typeHandlersPackage) {this.typeHandlersPackage = typeHandlersPackage;}public String getTypeAliasesPackage() {return this.typeAliasesPackage;}public void setTypeAliasesPackage(String typeAliasesPackage) {this.typeAliasesPackage = typeAliasesPackage;}public boolean isCheckConfigLocation() {return this.checkConfigLocation;}public void setCheckConfigLocation(boolean checkConfigLocation) {this.checkConfigLocation = checkConfigLocation;}public ExecutorType getExecutorType() {return this.executorType;}public void setExecutorType(ExecutorType executorType) {this.executorType = executorType;}public Configuration getConfiguration() {return configuration;}public void setConfiguration(Configuration configuration) {this.configuration = configuration;}public Resource[] resolveMapperLocations() {List<Resource> resources = new ArrayList<Resource>();if (this.mapperLocations != null) {for (String mapperLocation : this.mapperLocations) {Resource[] mappers;try {mappers = new PathMatchingResourcePatternResolver().getResources(mapperLocation);resources.addAll(Arrays.asList(mappers));} catch (IOException e) {}}}Resource[] mapperLocations = new Resource[resources.size()];mapperLocations = resources.toArray(mapperLocations);return mapperLocations;}}因为我们使用的是Hikari数据源 , 所以这里我是直接copy默认系统Hikari的属性文件 。
也就是这个文件:org.springframework.boot.autoconfigure.jdbc.DataSourceProperties 为啥我不直接继承这个类而是在自己的项目中新建这么一个类 , 是因为我发现这个类有这个注解
@ConfigurationProperties(prefix = "spring.datasource")
怕的是它的这个注解会覆盖我接下来两个类的注解(我主要是懒得测试 , 所以直接copy一份无所谓了) 。
接下来看看具体master和slave两个数据源的属性文件:

  • Properties(jpa和mybatis)文件
@Component@ConfigurationProperties(prefix = "master.datasource")public class MasterDataSourceProperties extends BaseDataSourceProperties {} @Component@ConfigurationProperties(prefix = "slave.datasource")public class SlaveDataSourceProperties extends BaseDataSourceProperties {} @Component@ConfigurationProperties(prefix = "master.mybatis")public class MasterMybatisProperties extends BaseMybatisProperties {} @Component@ConfigurationProperties(prefix = "slave.mybatis")public class SlaveMybatisProperties extends BaseMybatisProperties {}接下来是数据源的配置了 。
  • 数据源配置类
@Configurationpublic class HikariDataSourceConfig {@Bean@Primarypublic HikariDataSource masterDataSource(MasterDataSourceProperties properties) {HikariDataSource dataSource = createDataSource(properties,HikariDataSource.class);if (StringUtils.hasText(properties.getName())) {dataSource.setPoolName(properties.getName());}return dataSource;}@Beanpublic HikariDataSource slaveDataSource(SlaveDataSourceProperties properties) {HikariDataSource dataSource = createDataSource(properties,HikariDataSource.class);if (StringUtils.hasText(properties.getName())) {dataSource.setPoolName(properties.getName());}return dataSource;}@SuppressWarnings("unchecked")protected static <T> T createDataSource(BaseDataSourceProperties properties,Class<? extends DataSource> type) {return (T) properties.initializeDataSourceBuilder().type(type).build();}}


推荐阅读