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两个数据源的属性文件:
@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();}}
推荐阅读
- 彻底理解什么是同步和异步!
- K8s 多集群实践思考和探索
- 关于接口测试,你了解多少?
- 多样性视觉常识推理数据集GD-VCR
- 回顾:都说娱乐圈钱好挣,这4位明星的家底,确实让很多人羡慕不已
- 布丁多肉怎么养才长得好
- 紫牡丹多肉夏天怎么养 紫牡丹多肉植物怎么养
- TVB知名老戏骨罕见现身,满头白发烟不离手,结婚30多年坚持丁克
- 以爱为营开播在即!冲着白鹿去的,意外被20多岁小姐姐吸引
- 玛格丽特怎么养? 玛格丽特多肉怎么养才长得好