zjh
2025-03-20 b57c0f8b28c5cfbf66986f7e73cf3d1f601bc60e
ltkj-framework/src/main/java/com/ltkj/framework/config/DruidConfig.java
@@ -42,6 +42,7 @@
@Slf4j
public class DruidConfig {
    @Value ("${config.properties}")
    private  String url;
@@ -259,13 +260,63 @@
    }
    @Bean
//    @ConfigurationProperties("spring.datasource.druid.slavepacs")
//    @ConditionalOnProperty(prefix = "spring.datasource.druid.slavepacs", name = "enabled", havingValue = "true")
    public DataSource slaveDataWsSource(DruidProperties druidProperties) {
        DruidDataSource dataSource = DruidDataSourceBuilder.create().build();
        Properties props = new Properties();
        try {
            // 从文件中读取配置信息
            FileInputStream fis = null;
            try {
                fis = new FileInputStream(url);
            } catch (FileNotFoundException e) {
                log.info("数据库连接文件找不到");
            }
            props.load(fis);
            fis.close();
            // 这里是测试写法,具体的value可以通过请求参数传递过来
            String pacsenabled = props.getProperty("wsenabled");
            if (pacsenabled.equals("false"))
                return null;
            String pacsdbtype = props.getProperty("wsdbtype");
            switch (pacsdbtype){
                case "sqlserver":
                    dataSource = creatSqlServer(pacsenabled, props.getProperty("wsip"),props.getProperty("wsprot"),
                            props.getProperty("wsname"),props.getProperty("wsusername"),props.getProperty("wspassword"));
                    break;
                case "mysql":
                    dataSource = creatMysql(pacsenabled, props.getProperty("wsip"),
                            props.getProperty("wsprot"),props.getProperty("wsname"),props.getProperty("wsusername"),props.getProperty("wspassword"));
                    break;
                case "oracle":
                    dataSource = creatOracle(pacsenabled, props.getProperty("wsip"),
                            props.getProperty("wsprot"),props.getProperty("wsname"),props.getProperty("wsusername"),props.getProperty("wspassword"));
                    break;
                default:
                    dataSource = creatOracle(pacsenabled, props.getProperty("wsip"),props.getProperty("wsprot"),props.getProperty("wsname"),props.getProperty("wsusername")
                            ,props.getProperty("wspassword"));
                    break;
            }
            log.info("数据库连接成功!!!");
        } catch (Exception e) {
            log.info("数据库连接失败  请联系管理员!");
            e.printStackTrace();
        }
        return druidProperties.dataSource(dataSource);
    }
    private DruidDataSource creatSqlServer(String enabled, String ip,String port,String db,String user,String password) throws SQLException {
        // -Djdk.tls.client.protocols=TLSv1.2 JVM启动参数
        DruidDataSource dataSource = DruidDataSourceBuilder.create().build();
        Properties properties = new Properties();
        properties.setProperty("druid.enabled", enabled);
        properties.setProperty("druid.driverClassName","com.microsoft.sqlserver.jdbc.SQLServerDriver");
        properties.setProperty("druid.url","jdbc:sqlserver://"+ ip+":"+ port+";DatabaseName="+ db+
                ";&characterEncoding=utf8");
                ";trustServerCertificate=true;encrypt=false;characterEncoding=utf8");
        properties.setProperty("druid.username", user);
        properties.setProperty("druid.password", password);
        dataSource.restart(properties);
@@ -305,6 +356,7 @@
        setDataSource(targetDataSources, DataSourceType.SLAVE_HIS.name(), "slaveHisDataSource");
        setDataSource(targetDataSources, DataSourceType.SLAVE_LIS.name(), "slaveDataLisSource");
        setDataSource(targetDataSources, DataSourceType.SLAVE_PACS.name(), "slaveDataPacsSource");
        setDataSource(targetDataSources, DataSourceType.SLAVE_WS.name(), "slaveDataWsSource");
        return new DynamicDataSource(masterDataSource, targetDataSources);
    }