spring: datasource: default-db-key: voidme multi-db: - voidme: driver-class-name: com.mysql.cj.jdbc.Driver username: root password: root url: jdbc:mysql://192.168.42.153:3306/voidme?characterEncoding=utf8&autoReconnect=true&failOverReadOnly=false&maxReconnects=10&useSSL=false - xcdef: driver-class-name: com.mysql.cj.jdbc.Driver username: root password: root url: jdbc:mysql://192.168.42.153:3306/xcdef?characterEncoding=utf8&autoReconnect=true&failOverReadOnly=false&maxReconnects=10&useSSL=false mybatis: #1.classpath:只会到你的classes路径中查找找文件。 #2.classpath*:不仅会到classes路径,还包括jar文件中(classes路径)进行查找。 mapper-locations: classpath*:/mapper/**/*Mapper.xml # mapper映射文件位置 type-aliases-package: com.**.entity # 实体类所在的位置 configuration: log-impl: org.apache.ibatis.logging.stdout.StdOutImpl #用于控制台打印sql语句 map-underscore-to-camel-case: true #开启将带有下划线的表字段 映射为驼峰格式的实体类属性
#🎜 🎜 #
DynamicDataSourceDiese Klasse wird verwendet, um den (Kern) der Datenquelle zu erhalten.package com.dynamicdatadource.dynamic; import org.springframework.beans.factory.annotation.Value; import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource; public class DynamicDataSource extends AbstractRoutingDataSource { @Value("${spring.datasource.default-db-key}") private String defaultDbKey; @Override protected Object determineCurrentLookupKey() { String currentDb = DynamicDataSourceService.currentDb(); if (currentDb == null) { return defaultDbKey; } return currentDb; } }
package com.dynamicdatadource.dynamic; import com.application.ApplicationContextProvider; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.boot.jdbc.DataSourceBuilder; import javax.sql.DataSource; import java.util.HashMap; import java.util.Map; public class DynamicDataSourceService { private static final Logger log = LoggerFactory.getLogger(DynamicDataSourceService.class); private static final Map<Object, Object> dataSources = new HashMap<>(); private static final ThreadLocal<String> dbKeys = ThreadLocal.withInitial(() -> null); /** * 动态添加一个数据源 * * @param name 数据源的key * @param dataSource 数据源对象 */ public static void addDataSource(String name, DataSource dataSource) { DynamicDataSource dynamicDataSource = ApplicationContextProvider.getApplicationContext().getBean(DynamicDataSource.class); dataSources.put(name, dataSource); dynamicDataSource.setTargetDataSources(dataSources); dynamicDataSource.afterPropertiesSet(); log.info("添加了数据源:{}",name); } /** * @param name 数据源的key * @param driverClassName 驱动 * @param url 数据库连接地址 * @param username 数据库账户 * @param password 数据库密码 */ public static void addDataSource(String name, String driverClassName,String url,String username,String password) { DataSourceBuilder<?> builder = DataSourceBuilder.create(); builder.driverClassName(driverClassName); builder.username(username); builder.password(password); builder.url(url); addDataSource(name,builder.build()); log.info("添加了数据源:{}",name); } /** * 切换数据源 */ public static void switchDb(String dbKey) { dbKeys.set(dbKey); } /** * 重置数据源(切换为默认的数据源) */ public static void resetDb() { dbKeys.remove(); } /** * 获取当前数据源的key */ public static String currentDb() { return dbKeys.get(); } }
package com.dynamicdatadource.dynamic; import lombok.Data; import org.apache.ibatis.logging.Log; import org.mybatis.spring.SqlSessionFactoryBean; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.io.support.PathMatchingResourcePatternResolver; import org.springframework.jdbc.datasource.DataSourceTransactionManager; import org.springframework.transaction.PlatformTransactionManager; import java.io.IOException; import java.util.HashMap; import java.util.Map; @Configuration @ConfigurationProperties(prefix = "mybatis") @Data public class DynamicDataSourceConfig { private String mapperLocations; private String typeAliasesPackage; @Data public class MybatisConfiguration{ private String logImpl; private boolean mapUnderscoreToCamelCase; } private MybatisConfiguration configuration=new MybatisConfiguration(); /** * 动态数据源 */ @Bean public DynamicDataSource dynamicDataSource() { DynamicDataSource dataSource = new DynamicDataSource(); Map<Object, Object> targetDataSources = new HashMap<>(); dataSource.setTargetDataSources(targetDataSources); return dataSource; } /** * 会话工厂Mybaitis */ @Bean public SqlSessionFactoryBean sqlSessionFactoryBean() throws IOException, ClassNotFoundException { org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration(); configuration.setMapUnderscoreToCamelCase(this.configuration.isMapUnderscoreToCamelCase()); //开启驼峰命名 configuration.setLogImpl((Class<? extends Log>) Class.forName(this.configuration.getLogImpl())); //控制台打印sql日志 SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean(); sqlSessionFactoryBean.setDataSource(dynamicDataSource()); sqlSessionFactoryBean.setConfiguration(configuration); PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); sqlSessionFactoryBean.setMapperLocations(resolver.getResources(mapperLocations)); sqlSessionFactoryBean.setTypeAliasesPackage(typeAliasesPackage); return sqlSessionFactoryBean; } /** * 事务管理器 */ @Bean public PlatformTransactionManager transactionManager() { return new DataSourceTransactionManager(dynamicDataSource()); } }
package com.dynamicdatadource.config; import com.dynamicdatadource.dynamic.DynamicDataSourceService; import lombok.Data; import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.jdbc.DataSourceBuilder; import org.springframework.stereotype.Component; import javax.annotation.PostConstruct; import javax.sql.DataSource; import java.util.List; import java.util.Map; import java.util.Set; @Component @Data @ConfigurationProperties(prefix = "spring.datasource") public class YmlDataSourceProvider { private List<Map<String, DataSourceProperties>> multiDb; private DataSource buildDataSource(DataSourceProperties prop) { DataSourceBuilder<?> builder = DataSourceBuilder.create(); builder.driverClassName(prop.getDriverClassName()); builder.username(prop.getUsername()); builder.password(prop.getPassword()); builder.url(prop.getUrl()); return builder.build(); } public void initDataSource() { multiDb.forEach(map -> { Set<String> keys = map.keySet(); keys.forEach(key -> { DataSourceProperties properties = map.get(key); DataSource dataSource = buildDataSource(properties); DynamicDataSourceService.addDataSource(key, dataSource); }); }); } //在构造函数之后执行 @PostConstruct public void init() { initDataSource(); } }
Das obige ist der detaillierte Inhalt vonSo implementieren Sie SpringBoot-Umschaltung mehrerer Datenquellen. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!