核心SDK提交
This commit is contained in:
38
src/main/java/com/yjdsj/common/ds/DataSourceConfig.java
Normal file
38
src/main/java/com/yjdsj/common/ds/DataSourceConfig.java
Normal file
@ -0,0 +1,38 @@
|
||||
package com.yjdsj.common.ds;
|
||||
|
||||
import org.apache.ibatis.session.SqlSessionFactory;
|
||||
import org.mybatis.spring.SqlSessionFactoryBean;
|
||||
import org.mybatis.spring.SqlSessionTemplate;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import javax.sql.DataSource;
|
||||
|
||||
@Configuration
|
||||
public class DataSourceConfig {
|
||||
|
||||
@Resource
|
||||
private DynamicDataSourceManager dynamicDataSourceManager;
|
||||
|
||||
@Bean(name = "dynamicDataSource")
|
||||
public DataSource dynamicDataSource() {
|
||||
DynamicRoutingDataSource dynamicRoutingDataSource = new DynamicRoutingDataSource(dynamicDataSourceManager);
|
||||
return dynamicRoutingDataSource;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public SqlSessionFactory sqlSessionFactory(@Autowired DataSource dynamicDataSource) throws Exception {
|
||||
SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean();
|
||||
sessionFactoryBean.setDataSource(dynamicDataSource);
|
||||
sessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/*.xml"));
|
||||
return sessionFactoryBean.getObject();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public SqlSessionTemplate sqlSessionTemplate(@Autowired SqlSessionFactory sqlSessionFactory) {
|
||||
return new SqlSessionTemplate(sqlSessionFactory);
|
||||
}
|
||||
}
|
@ -0,0 +1,18 @@
|
||||
package com.yjdsj.common.ds;
|
||||
|
||||
public class DynamicDataSourceContextHolder {
|
||||
|
||||
private static final ThreadLocal<String> CONTEXT_HOLDER = new ThreadLocal<>();
|
||||
|
||||
public static void setDataSourceKey(String key) {
|
||||
CONTEXT_HOLDER.set(key);
|
||||
}
|
||||
|
||||
public static String getDataSourceKey() {
|
||||
return CONTEXT_HOLDER.get();
|
||||
}
|
||||
|
||||
public static void clearDataSourceKey() {
|
||||
CONTEXT_HOLDER.remove();
|
||||
}
|
||||
}
|
@ -0,0 +1,46 @@
|
||||
package com.yjdsj.common.ds;
|
||||
|
||||
import com.yjdsj.common.map.SourceMap;
|
||||
import com.zaxxer.hikari.HikariDataSource;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@Component
|
||||
public class DynamicDataSourceManager {
|
||||
|
||||
private final Map<String, DataSource> dataSourceCache = new HashMap<>();
|
||||
private final SourceMap sourceMap;
|
||||
|
||||
@Autowired
|
||||
public DynamicDataSourceManager(SourceMap sourceMap) {
|
||||
this.sourceMap = sourceMap;
|
||||
}
|
||||
|
||||
public DataSource getDataSource(String dbName) {
|
||||
// 检查缓存中是否已有数据源
|
||||
if (dataSourceCache.containsKey(dbName)) {
|
||||
return dataSourceCache.get(dbName);
|
||||
} else {
|
||||
// 动态创建数据源
|
||||
String dbPath = sourceMap.getSourceMapping(dbName);
|
||||
if (dbPath != null) {
|
||||
DataSource dataSource = createDataSource(dbPath);
|
||||
dataSourceCache.put(dbName, dataSource);
|
||||
return dataSource;
|
||||
} else {
|
||||
throw new IllegalArgumentException("Data source not found for name: " + dbName);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private DataSource createDataSource(String dbPath) {
|
||||
HikariDataSource dataSource = new HikariDataSource();
|
||||
dataSource.setJdbcUrl("jdbc:sqlite:" + dbPath);
|
||||
dataSource.setDriverClassName("org.sqlite.JDBC");
|
||||
return dataSource;
|
||||
}
|
||||
}
|
@ -0,0 +1,28 @@
|
||||
package com.yjdsj.common.ds;
|
||||
|
||||
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import java.util.HashMap;
|
||||
|
||||
public class DynamicRoutingDataSource extends AbstractRoutingDataSource {
|
||||
|
||||
private final DynamicDataSourceManager dataSourceManager;
|
||||
|
||||
public DynamicRoutingDataSource(DynamicDataSourceManager dataSourceManager) {
|
||||
this.dataSourceManager = dataSourceManager;
|
||||
this.setTargetDataSources(new HashMap<>());
|
||||
this.afterPropertiesSet();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Object determineCurrentLookupKey() {
|
||||
return DynamicDataSourceContextHolder.getDataSourceKey();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected DataSource determineTargetDataSource() {
|
||||
String dataSourceKey = (String) determineCurrentLookupKey();
|
||||
return dataSourceManager.getDataSource(dataSourceKey);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user