From d0fb64d4f70c6fbf782018c928555be517bf7066 Mon Sep 17 00:00:00 2001 From: lige <bestlige@outlook.com> Date: 星期二, 12 十二月 2023 09:46:46 +0800 Subject: [PATCH] sqlserver拿视图数据存储mysql表 同时配置三个从库 --- ltkj-framework/src/main/java/com/ltkj/framework/config/DruidConfig.java | 64 +++++++++++++++++++++++++++---- 1 files changed, 55 insertions(+), 9 deletions(-) diff --git a/ltkj-framework/src/main/java/com/ltkj/framework/config/DruidConfig.java b/ltkj-framework/src/main/java/com/ltkj/framework/config/DruidConfig.java index 1742f44..f967e4e 100644 --- a/ltkj-framework/src/main/java/com/ltkj/framework/config/DruidConfig.java +++ b/ltkj-framework/src/main/java/com/ltkj/framework/config/DruidConfig.java @@ -1,8 +1,6 @@ package com.ltkj.framework.config; -import java.io.FileInputStream; -import java.io.FileOutputStream; -import java.io.IOException; +import java.io.*; import java.util.HashMap; import java.util.Map; import java.util.Properties; @@ -17,6 +15,7 @@ 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.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.context.properties.ConfigurationProperties; @@ -37,6 +36,7 @@ * @author ltkj */ @Configuration +@Slf4j public class DruidConfig { @Bean @@ -46,26 +46,70 @@ Properties props = new Properties(); try { // 浠庢枃浠朵腑璇诲彇閰嶇疆淇℃伅 - FileInputStream fis = new FileInputStream("D:\\ltkjprojectconf\\config.properties"); + FileInputStream fis = null; + try { + fis = new FileInputStream("D:\\ltkjprojectconf\\config.properties"); + } catch (FileNotFoundException e) { + log.info("鏁版嵁搴撹繛鎺ユ枃浠舵壘涓嶅埌 绯荤粺姝e湪鍒涘缓锛�"); + + File f = new File("D:\\ltkjprojectconf"); + if(!f.exists()){ + f.mkdirs(); + } + File file = new File("D:\\ltkjprojectconf\\config.properties"); + try { + FileWriter fileWriter = new FileWriter(file); + fileWriter.write("ip = 浣犵殑鏁版嵁搴撹繛鎺p鍦板潃\n"); + fileWriter.write("prot = 浣犵殑鏁版嵁搴撹繛鎺ョ鍙n"); + fileWriter.write("name = 浣犵殑鏁版嵁搴撹繛鎺ュ悕绉癨n"); + fileWriter.write("username = 浣犵殑鏁版嵁搴撹繛鎺ョ敤鎴峰悕\n"); + fileWriter.write("password = 浣犵殑鏁版嵁搴撹繛鎺ュ瘑鐮乗n"); + fileWriter.close(); + log.info("鏁版嵁搴撹繛鎺ユ枃浠跺垱寤烘垚鍔燂紒"); + } catch (IOException ioException) { + log.info("鏁版嵁搴撹繛鎺ユ枃浠跺垱寤哄け璐� 璇疯仈绯荤鐞嗗憳鎵嬪姩鍒涘缓锛�"); + ioException.printStackTrace(); + } + e.printStackTrace(); + } props.load(fis); fis.close(); // 鑾峰彇灞炴�у�煎苟璧嬪�� Properties properties = new Properties(); // 杩欓噷鏄祴璇曞啓娉曪紝鍏蜂綋鐨剉alue鍙互閫氳繃璇锋眰鍙傛暟浼犻�掕繃鏉� - properties.setProperty("druid.url",props.getProperty("url")); + properties.setProperty("druid.url","jdbc:mysql://"+props.getProperty("ip")+":"+props.getProperty("prot")+"/"+props.getProperty("name")+"" + + "?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8"); properties.setProperty("druid.username",props.getProperty("username")); properties.setProperty("druid.password",props.getProperty("password")); dataSource.restart(properties); + log.info("鏁版嵁搴撹繛鎺ユ垚鍔�!!!"); } catch (Exception e) { + log.info("鏁版嵁搴撹繛鎺ュけ璐� 璇疯仈绯荤鐞嗗憳锛�"); e.printStackTrace(); } return druidProperties.dataSource(dataSource); } @Bean - @ConfigurationProperties("spring.datasource.druid.slave") - @ConditionalOnProperty(prefix = "spring.datasource.druid.slave", name = "enabled", havingValue = "true") - public DataSource slaveDataSource(DruidProperties druidProperties) { + @ConfigurationProperties("spring.datasource.druid.slavehis") + @ConditionalOnProperty(prefix = "spring.datasource.druid.slavehis", name = "enabled", havingValue = "true") + public DataSource slaveHisDataSource(DruidProperties druidProperties) { + DruidDataSource dataSource = DruidDataSourceBuilder.create().build(); + return druidProperties.dataSource(dataSource); + } + + @Bean + @ConfigurationProperties("spring.datasource.druid.slavelis") + @ConditionalOnProperty(prefix = "spring.datasource.druid.slavelis", name = "enabled", havingValue = "true") + public DataSource slaveDataLisSource(DruidProperties druidProperties) { + DruidDataSource dataSource = DruidDataSourceBuilder.create().build(); + return druidProperties.dataSource(dataSource); + } + + @Bean + @ConfigurationProperties("spring.datasource.druid.slavepacs") + @ConditionalOnProperty(prefix = "spring.datasource.druid.slavepacs", name = "enabled", havingValue = "true") + public DataSource slaveDataPacsSource(DruidProperties druidProperties) { DruidDataSource dataSource = DruidDataSourceBuilder.create().build(); return druidProperties.dataSource(dataSource); } @@ -75,7 +119,9 @@ public DynamicDataSource dataSource(DataSource masterDataSource) { Map<Object, Object> targetDataSources = new HashMap<>(); targetDataSources.put(DataSourceType.MASTER.name(), masterDataSource); - setDataSource(targetDataSources, DataSourceType.SLAVE.name(), "slaveDataSource"); + setDataSource(targetDataSources, DataSourceType.SLAVE_HIS.name(), "slaveHisDataSource"); + setDataSource(targetDataSources, DataSourceType.SLAVE_LIS.name(), "slaveDataLisSource"); + setDataSource(targetDataSources, DataSourceType.SLAVE_PACS.name(), "slaveDataPacsSource"); return new DynamicDataSource(masterDataSource, targetDataSources); } -- Gitblit v1.8.0