| | |
| | | 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; |
| | |
| | | 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; |
| | |
| | | * @author ltkj |
| | | */ |
| | | @Configuration |
| | | @Slf4j |
| | | public class DruidConfig { |
| | | |
| | | @Bean |
| | |
| | | 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("数据库连接文件找不到 系统正在创建!"); |
| | | |
| | | 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 = 你的数据库连接ip地址\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(); |
| | | // 这里是测试写法,具体的value可以通过请求参数传递过来 |
| | | 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); |