zjh
2025-02-07 59972ce8f9128858866c17655fc2d034d023b976
zjh20250207
6个文件已修改
127 ■■■■ 已修改文件
ltkj-framework/src/main/java/com/ltkj/framework/config/DruidConfig.java 92 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
ltkj-framework/src/main/java/com/ltkj/framework/datasource/DynamicDataSource.java 19 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
ltkj-framework/src/main/java/com/ltkj/framework/interceptor/DBChangeInterceptor.java 8 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
ltkj-hosp/src/main/java/com/ltkj/db/DataSourceConfig.java 2 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
ltkj-hosp/src/main/java/com/ltkj/db/HospDynamicDataSource.java 4 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
ltkj-hosp/src/main/java/com/ltkj/hosp/service/impl/LtkjExamJcsqdServiceImpl.java 2 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
ltkj-framework/src/main/java/com/ltkj/framework/config/DruidConfig.java
@@ -5,7 +5,6 @@
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import javax.annotation.Resource;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
@@ -14,14 +13,12 @@
import javax.servlet.ServletResponse;
import javax.sql.DataSource;
import com.ltkj.db.HospDynamicDataSource;
import com.ltkj.framework.config.properties.DruidProperties;
import com.ltkj.framework.datasource.DynamicDataSource;
import com.ltkj.system.service.ISysConfigService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@@ -48,6 +45,28 @@
    @Value ("${config.path}")
    private  String path;
    private static final String DEFAULT_DATA_SOURCE_KEY = "default"; // 主库的标识
    private final Map<String, DataSource> dataSourceCache = new HashMap<>(); // 数据源缓存
    // 从 application.yml 中读取主库的配置
//    @Value("${spring.datasource.url}")
    private String primaryUrl;
    private String primaryPort;
    //    @Value("${spring.datasource.username}")
    private String primaryUsername;
    //    @Value("${spring.datasource.password}")
    private String primaryPassword;
    //    @Value("${dbUrl}")
    private String dbUrl;
    private String dbName;
    @Bean
@@ -259,8 +278,6 @@
        return druidProperties.dataSource(dataSource);
    }
    @Bean
//    @ConfigurationProperties("spring.datasource.druid.slavepacs")
//    @ConditionalOnProperty(prefix = "spring.datasource.druid.slavepacs", name = "enabled", havingValue = "true")
@@ -346,6 +363,69 @@
        return dataSource;
    }
//    @Bean(name = "hospDynamicDataSources")
//    public DataSource hospDynamicDataSources() {
//        HospDynamicDataSource dynamicDataSource = new HospDynamicDataSource();
//        FileInputStream fis = null;
//        Properties props = new Properties();
//        try {
//            fis = new FileInputStream(url);
//            props.load(fis);
//            fis.close();
//            dbUrl = props.getProperty("ip");
//            dbName = props.getProperty("name");
//            primaryPassword = props.getProperty("password");
//            primaryPort = props.getProperty("prot");
//            primaryUsername = props.getProperty("username");
//        } catch (IOException e) {
//            throw new RuntimeException("读取配置文件失败", e);
//        }
//        String url = "jdbc:mysql://" + dbUrl + ":" + primaryPort + "/" + dbName;
//        // 初始化默认数据源为主库
//        dynamicDataSource.addTargetDataSource(DEFAULT_DATA_SOURCE_KEY, createDataSource(url, primaryUsername, primaryPassword));
//
//        dynamicDataSource.setDefaultTargetDataSource(dynamicDataSource.getTargetDataSources().get(DEFAULT_DATA_SOURCE_KEY)); // 设置默认数据源
//        return dynamicDataSource;
//    }
    // 根据用户 ID 动态获取数据源
    public void addDataSource(String dbName) {
        FileInputStream fis = null;
        Properties props = new Properties();
        try {
            fis = new FileInputStream(url);
            props.load(fis);
            fis.close();
            dbUrl = props.getProperty("ip");
            primaryPassword = props.getProperty("password");
            primaryPort = props.getProperty("prot");
            primaryUsername = props.getProperty("username");
        } catch (IOException e) {
            throw new RuntimeException("读取配置文件失败", e);
        }
        // 检查缓存中是否已经存在该数据源
        if (!dataSourceCache.containsKey(dbName)) {
            synchronized (this) {
                String url = "jdbc:mysql://" + dbUrl + ":" + primaryPort + "/" + dbName;
                DataSource dataSource = createDataSource(url, primaryUsername, primaryPassword);
                dataSourceCache.put(dbName, dataSource);
                DynamicDataSource dynamicDataSource = dataSource(dataSource);
                dynamicDataSource.addTargetDataSource(dbName, dataSource);
            }
        }
    }
    // 动态创建数据源
    private DataSource createDataSource(String url, String username, String password) {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setUrl(url);
        dataSource.setUsername(username);
        dataSource.setPassword(password);
        return dataSource;
    }
    @Bean(name = "dynamicDataSource")
    @Primary
ltkj-framework/src/main/java/com/ltkj/framework/datasource/DynamicDataSource.java
@@ -1,6 +1,7 @@
package com.ltkj.framework.datasource;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import javax.sql.DataSource;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
@@ -11,6 +12,7 @@
 * @author ltkj
 */
public class DynamicDataSource extends AbstractRoutingDataSource {
    private final Map<Object, Object> targetDataSources = new ConcurrentHashMap<>(); // 存储所有数据源
    public DynamicDataSource(DataSource defaultTargetDataSource, Map<Object, Object> targetDataSources) {
        super.setDefaultTargetDataSource(defaultTargetDataSource);
        super.setTargetDataSources(targetDataSources);
@@ -19,6 +21,23 @@
    @Override
    protected Object determineCurrentLookupKey() {
        System.out.println("当前数据源 -> "+ DynamicDataSourceContextHolder.getDataSourceType());
        return DynamicDataSourceContextHolder.getDataSourceType();
    }
    public void addTargetDataSource(String key, DataSource dataSource) {
        targetDataSources.put(key, dataSource);
        super.setTargetDataSources(new ConcurrentHashMap<>(targetDataSources)); // 更新目标数据源
        super.afterPropertiesSet(); // 重新初始化数据源
    }
    // 允许外部访问所有目标数据源
    @Override
    public void setTargetDataSources(Map<Object, Object> targetDataSources) {
        super.setTargetDataSources(targetDataSources);
    }
    public Map<Object, Object> getTargetDataSources() {
        return targetDataSources; // 获取所有目标数据源
    }
}
ltkj-framework/src/main/java/com/ltkj/framework/interceptor/DBChangeInterceptor.java
@@ -6,6 +6,7 @@
import com.google.gson.Gson;
import com.ltkj.db.DataSourceConfig;
import com.ltkj.db.DataSourceContextHolder;
import com.ltkj.framework.config.DruidConfig;
import com.ltkj.hosp.domain.DictHosp;
import com.ltkj.hosp.service.IDictHospService;
import org.springframework.beans.factory.annotation.Autowired;
@@ -28,8 +29,10 @@
    @Autowired
    private IDictHospService dictHospService;
//    @Autowired
//    private DataSourceConfig dataSourceConfig;
    @Autowired
    private DataSourceConfig dataSourceConfig;
    private DruidConfig druidConfig;
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws IOException {
@@ -52,7 +55,8 @@
                response.getWriter().write("{\"message\":\"401:找不到院区数据\"}");
                return false;
            }
            dataSourceConfig.addDataSource(hosp.getDbname());
//            dataSourceConfig.addDataSource(hosp.getDbname());
            druidConfig.addDataSource(hosp.getDbname());
            DataSourceContextHolder.setDataSourceKey(hosp.getDbname());
        } catch (IOException e) {
            return false;
ltkj-hosp/src/main/java/com/ltkj/db/DataSourceConfig.java
@@ -14,7 +14,7 @@
import java.util.Map;
import java.util.Properties;
@Configuration
//@Configuration
public class DataSourceConfig {
    private static final String DEFAULT_DATA_SOURCE_KEY = "default"; // 主库的标识
ltkj-hosp/src/main/java/com/ltkj/db/HospDynamicDataSource.java
@@ -1,11 +1,7 @@
package com.ltkj.db;
import org.springframework.context.annotation.Bean;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
import org.springframework.stereotype.Component;
import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
ltkj-hosp/src/main/java/com/ltkj/hosp/service/impl/LtkjExamJcsqdServiceImpl.java
@@ -37,8 +37,6 @@
    private LtkjExamJcsqdMapper mapper;
    @Qualifier(value = "slaveHisDataSource")
    @Autowired
    private DataSource dataSource;
    @Override