| | |
| | | package com.ltkj.db; |
| | | |
| | | import com.alibaba.druid.pool.DruidDataSource; |
| | | import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder; |
| | | import com.ltkj.common.enums.DataSourceType; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.beans.factory.annotation.Value; |
| | | import org.springframework.context.annotation.Bean; |
| | | import org.springframework.context.annotation.Configuration; |
| | | |
| | | import javax.sql.DataSource; |
| | | import java.io.FileInputStream; |
| | | import java.io.FileNotFoundException; |
| | | import java.io.IOException; |
| | | import java.io.*; |
| | | import java.sql.SQLException; |
| | | import java.util.HashMap; |
| | | import java.util.Map; |
| | | import java.util.Properties; |
| | | |
| | | //@Configuration |
| | | @Slf4j |
| | | @Configuration |
| | | public class DataSourceConfig { |
| | | |
| | | private static final String DEFAULT_DATA_SOURCE_KEY = "default"; // 主库的标识 |
| | |
| | | // 初始化默认数据源为主库 |
| | | dynamicDataSource.addTargetDataSource(DEFAULT_DATA_SOURCE_KEY, createDataSource(url, primaryUsername, primaryPassword)); |
| | | |
| | | DruidProperties properties = new DruidProperties(); |
| | | dynamicDataSource.addTargetDataSource(DataSourceType.MASTER.name(),masterDataSource(properties)); |
| | | dynamicDataSource.addTargetDataSource(DataSourceType.SLAVE_HIS.name(),slaveHisDataSource(properties)); |
| | | dynamicDataSource.addTargetDataSource(DataSourceType.SLAVE_LIS.name(),slaveDataLisSource(properties)); |
| | | dynamicDataSource.addTargetDataSource(DataSourceType.SLAVE_PACS.name(),slaveDataPacsSource(properties)); |
| | | dynamicDataSource.addTargetDataSource(DataSourceType.SLAVE_WS.name(),slaveDataWsSource(properties)); |
| | | |
| | | dynamicDataSource.setDefaultTargetDataSource(dynamicDataSource.getTargetDataSources().get(DEFAULT_DATA_SOURCE_KEY)); // 设置默认数据源 |
| | | return dynamicDataSource; |
| | | } |
| | |
| | | } |
| | | } |
| | | } |
| | | |
| | | private DataSource masterDataSource(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(); |
| | | // 获取属性值并赋值 |
| | | String hisenabled = props.getProperty("hisenabled"); |
| | | if (hisenabled.equals("false")) |
| | | return null; |
| | | dataSource = creatMysql(hisenabled, props.getProperty("ip"),props.getProperty("prot"),props.getProperty("name"),props.getProperty("username"),props.getProperty("password")); |
| | | log.info("his数据库连接成功!!!"); |
| | | } catch (Exception e) { |
| | | log.info("数据库连接失败 请联系管理员!"); |
| | | e.printStackTrace(); |
| | | } |
| | | return druidProperties.dataSource(dataSource); |
| | | } |
| | | |
| | | private DataSource slaveHisDataSource(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(); |
| | | // 获取属性值并赋值 |
| | | String hisenabled = props.getProperty("hisenabled"); |
| | | if (hisenabled.equals("false")) |
| | | return null; |
| | | String hisdbtype = props.getProperty("hisdbtype"); |
| | | switch (hisdbtype){ |
| | | case "sqlserver": |
| | | dataSource = creatSqlServer(hisenabled, props.getProperty("hisip"),props.getProperty("hisprot"),props.getProperty("hisname"),props.getProperty("hisusername"),props.getProperty("hispassword")); |
| | | break; |
| | | case "mysql": |
| | | dataSource = creatMysql(hisenabled, props.getProperty("hisip"),props.getProperty("hisprot"),props.getProperty("hisname"),props.getProperty("hisusername"),props.getProperty("hispassword")); |
| | | break; |
| | | case "oracle": |
| | | dataSource = creatOracle(hisenabled, props.getProperty("hisip"),props.getProperty("hisprot"),props.getProperty("hisname"),props.getProperty("hisusername"),props.getProperty("hispassword")); |
| | | break; |
| | | default: |
| | | dataSource = creatSqlServer(hisenabled, props.getProperty("hisip"),props.getProperty("hisprot"),props.getProperty("hisname"),props.getProperty("hisusername"),props.getProperty("hispassword")); |
| | | break; |
| | | } |
| | | log.info("his数据库连接成功!!!"); |
| | | } catch (Exception e) { |
| | | log.info("数据库连接失败 请联系管理员!"); |
| | | e.printStackTrace(); |
| | | } |
| | | return druidProperties.dataSource(dataSource); |
| | | } |
| | | |
| | | private DataSource slaveDataLisSource(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(); |
| | | String lisenabled = props.getProperty("lisenabled"); |
| | | if (lisenabled.equals("false")) |
| | | return null; |
| | | String lisdbtype = props.getProperty("lisdbtype"); |
| | | switch (lisdbtype){ |
| | | case "sqlserver": |
| | | dataSource = creatSqlServer(lisenabled, props.getProperty("lisip"),props.getProperty("lisprot"),props.getProperty("lisname"),props.getProperty("lisusername"),props.getProperty("lispassword")); |
| | | break; |
| | | case "mysql": |
| | | dataSource = creatMysql(lisenabled, props.getProperty("lisip"),props.getProperty("lisprot"),props.getProperty("lisname"),props.getProperty("lisusername"),props.getProperty("lispassword")); |
| | | break; |
| | | case "oracle": |
| | | dataSource = creatOracle(lisenabled, props.getProperty("lisip"),props.getProperty("lisprot"),props.getProperty("lisname"),props.getProperty("lisusername"),props.getProperty("lispassword")); |
| | | break; |
| | | default: |
| | | dataSource = creatMysql(lisenabled, props.getProperty("lisip"),props.getProperty("lisprot"),props.getProperty("lisname"),props.getProperty("lisusername"),props.getProperty("lispassword")); |
| | | break; |
| | | } |
| | | log.info("Lis数据库连接成功!!!"); |
| | | } catch (Exception e) { |
| | | log.info("数据库连接失败 请联系管理员!"); |
| | | e.printStackTrace(); |
| | | } |
| | | return druidProperties.dataSource(dataSource); |
| | | } |
| | | |
| | | private DataSource slaveDataPacsSource(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("pacsenabled"); |
| | | if (pacsenabled.equals("false")) |
| | | return null; |
| | | String pacsdbtype = props.getProperty("pacsdbtype"); |
| | | switch (pacsdbtype){ |
| | | case "sqlserver": |
| | | dataSource = creatSqlServer(pacsenabled, props.getProperty("pacsip"),props.getProperty("pacsprot"),props.getProperty("pacsname"),props.getProperty("pacsusername"),props.getProperty("pacspassword")); |
| | | break; |
| | | case "mysql": |
| | | dataSource = creatMysql(pacsenabled, props.getProperty("pacsip"),props.getProperty("pacsprot"),props.getProperty("pacsname"),props.getProperty("pacsusername"),props.getProperty("pacspassword")); |
| | | break; |
| | | case "oracle": |
| | | dataSource = creatOracle(pacsenabled, props.getProperty("pacsip"),props.getProperty("pacsprot"),props.getProperty("pacsname"),props.getProperty("pacsusername"),props.getProperty("pacspassword")); |
| | | break; |
| | | default: |
| | | dataSource = creatOracle(pacsenabled, props.getProperty("pacsip"),props.getProperty("pacsprot"),props.getProperty("pacsname"),props.getProperty("pacsusername"),props.getProperty("pacspassword")); |
| | | break; |
| | | } |
| | | log.info("数据库连接成功!!!"); |
| | | } catch (Exception e) { |
| | | log.info("数据库连接失败 请联系管理员!"); |
| | | e.printStackTrace(); |
| | | } |
| | | return druidProperties.dataSource(dataSource); |
| | | } |
| | | |
| | | private 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 { |
| | | 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"); |
| | | properties.setProperty("druid.username", user); |
| | | properties.setProperty("druid.password", password); |
| | | dataSource.restart(properties); |
| | | return dataSource; |
| | | } |
| | | |
| | | private DruidDataSource creatMysql(String enabled, String ip,String port,String db,String user,String password) throws SQLException { |
| | | DruidDataSource dataSource = DruidDataSourceBuilder.create().build(); |
| | | Properties properties = new Properties(); |
| | | properties.setProperty("druid.enabled",enabled); |
| | | properties.setProperty("druid.url","jdbc:mysql://"+ip+":"+port+"/"+db+"" + |
| | | "?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=GMT%2B8"); |
| | | properties.setProperty("druid.username",user); |
| | | properties.setProperty("druid.password",password); |
| | | dataSource.restart(properties); |
| | | return dataSource; |
| | | } |
| | | |
| | | private DruidDataSource creatOracle(String enabled, String ip,String port,String db,String user,String password) throws SQLException { |
| | | DruidDataSource dataSource = DruidDataSourceBuilder.create().build(); |
| | | Properties properties = new Properties(); |
| | | properties.setProperty("druid.enabled",enabled); |
| | | properties.setProperty("druid.driverClassName","oracle.jdbc.OracleDriver"); |
| | | properties.setProperty("druid.url","jdbc:oracle:thin:@//"+ip+"/"+db); |
| | | properties.setProperty("druid.username",user); |
| | | properties.setProperty("druid.password",password); |
| | | dataSource.restart(properties); |
| | | return dataSource; |
| | | } |
| | | |
| | | } |